index.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {Fragment, useCallback, useEffect, useState} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import type {Location} from 'history';
  4. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  5. import type {CursorHandler} from 'sentry/components/pagination';
  6. import type {AuditLog} from 'sentry/types';
  7. import {browserHistory} from 'sentry/utils/browserHistory';
  8. import {isActiveSuperuser} from 'sentry/utils/isActiveSuperuser';
  9. import {decodeScalar} from 'sentry/utils/queryString';
  10. import useApi from 'sentry/utils/useApi';
  11. import useOrganization from 'sentry/utils/useOrganization';
  12. import PermissionAlert from 'sentry/views/settings/organization/permissionAlert';
  13. import AuditLogList from './auditLogList';
  14. type Props = {
  15. location: Location;
  16. };
  17. type State = {
  18. entryList: AuditLog[] | null;
  19. entryListPageLinks: string | null;
  20. eventType: string | undefined;
  21. eventTypes: string[] | null;
  22. isLoading: boolean;
  23. currentCursor?: string;
  24. };
  25. function OrganizationAuditLog({location}: Props) {
  26. const [state, setState] = useState<State>({
  27. entryList: [],
  28. entryListPageLinks: null,
  29. eventType: decodeScalar(location.query.event),
  30. eventTypes: [],
  31. isLoading: true,
  32. });
  33. const organization = useOrganization();
  34. const api = useApi();
  35. const handleCursor: CursorHandler = resultsCursor => {
  36. setState(prevState => ({
  37. ...prevState,
  38. currentCursor: resultsCursor,
  39. }));
  40. };
  41. useEffect(() => {
  42. // Watch the location for changes so we can re-fetch data.
  43. const eventType = decodeScalar(location.query.event);
  44. setState(prevState => ({...prevState, eventType}));
  45. }, [location.query]);
  46. const fetchAuditLogData = useCallback(async () => {
  47. setState(prevState => ({...prevState, isLoading: true}));
  48. try {
  49. const payload = {cursor: state.currentCursor, event: state.eventType};
  50. if (!payload.cursor) {
  51. delete payload.cursor;
  52. }
  53. if (!payload.event) {
  54. delete payload.event;
  55. }
  56. setState(prevState => ({...prevState, isLoading: true}));
  57. const [data, _, response] = await api.requestPromise(
  58. `/organizations/${organization.slug}/audit-logs/`,
  59. {
  60. method: 'GET',
  61. includeAllArgs: true,
  62. query: payload,
  63. }
  64. );
  65. setState(prevState => ({
  66. ...prevState,
  67. entryList: data.rows,
  68. eventTypes: data.options,
  69. isLoading: false,
  70. entryListPageLinks: response?.getResponseHeader('Link') ?? null,
  71. }));
  72. } catch (err) {
  73. if (err.status !== 401 && err.status !== 403) {
  74. Sentry.captureException(err);
  75. }
  76. setState(prevState => ({
  77. ...prevState,
  78. isLoading: false,
  79. }));
  80. addErrorMessage('Unable to load audit logs.');
  81. }
  82. }, [api, organization.slug, state.currentCursor, state.eventType]);
  83. useEffect(() => {
  84. fetchAuditLogData();
  85. }, [fetchAuditLogData]);
  86. const handleEventSelect = (value: string) => {
  87. setState(prevState => ({
  88. ...prevState,
  89. eventType: value,
  90. }));
  91. browserHistory.push({
  92. pathname: location.pathname,
  93. query: {...location.query, event: value},
  94. });
  95. };
  96. return (
  97. <Fragment>
  98. {organization.access.includes('org:write') || isActiveSuperuser() ? (
  99. <AuditLogList
  100. entries={state.entryList}
  101. pageLinks={state.entryListPageLinks}
  102. eventType={state.eventType}
  103. eventTypes={state.eventTypes}
  104. onEventSelect={handleEventSelect}
  105. isLoading={state.isLoading}
  106. onCursor={handleCursor}
  107. />
  108. ) : (
  109. <PermissionAlert />
  110. )}
  111. </Fragment>
  112. );
  113. }
  114. export default OrganizationAuditLog;