index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/organization';
  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 hasPermission = organization.access.includes('org:write') || isActiveSuperuser();
  36. const handleCursor: CursorHandler = resultsCursor => {
  37. setState(prevState => ({
  38. ...prevState,
  39. currentCursor: resultsCursor,
  40. }));
  41. };
  42. useEffect(() => {
  43. // Watch the location for changes so we can re-fetch data.
  44. const eventType = decodeScalar(location.query.event);
  45. setState(prevState => ({...prevState, eventType}));
  46. }, [location.query]);
  47. const fetchAuditLogData = useCallback(async () => {
  48. if (!hasPermission) {
  49. return;
  50. }
  51. setState(prevState => ({...prevState, isLoading: true}));
  52. try {
  53. const payload = {cursor: state.currentCursor, event: state.eventType};
  54. if (!payload.cursor) {
  55. delete payload.cursor;
  56. }
  57. if (!payload.event) {
  58. delete payload.event;
  59. }
  60. setState(prevState => ({...prevState, isLoading: true}));
  61. const [data, _, response] = await api.requestPromise(
  62. `/organizations/${organization.slug}/audit-logs/`,
  63. {
  64. method: 'GET',
  65. includeAllArgs: true,
  66. query: payload,
  67. }
  68. );
  69. setState(prevState => ({
  70. ...prevState,
  71. entryList: data.rows,
  72. eventTypes: data.options,
  73. isLoading: false,
  74. entryListPageLinks: response?.getResponseHeader('Link') ?? null,
  75. }));
  76. } catch (err) {
  77. if (err.status !== 401 && err.status !== 403) {
  78. Sentry.captureException(err);
  79. }
  80. setState(prevState => ({
  81. ...prevState,
  82. isLoading: false,
  83. }));
  84. if (err.status !== 403) {
  85. addErrorMessage('Unable to load audit logs.');
  86. }
  87. }
  88. }, [api, organization.slug, state.currentCursor, state.eventType, hasPermission]);
  89. useEffect(() => {
  90. fetchAuditLogData();
  91. }, [fetchAuditLogData]);
  92. const handleEventSelect = (value: string) => {
  93. setState(prevState => ({
  94. ...prevState,
  95. eventType: value,
  96. }));
  97. browserHistory.push({
  98. pathname: location.pathname,
  99. query: {...location.query, event: value},
  100. });
  101. };
  102. return (
  103. <Fragment>
  104. {hasPermission ? (
  105. <AuditLogList
  106. entries={state.entryList}
  107. pageLinks={state.entryListPageLinks}
  108. eventType={state.eventType}
  109. eventTypes={state.eventTypes}
  110. onEventSelect={handleEventSelect}
  111. isLoading={state.isLoading}
  112. onCursor={handleCursor}
  113. />
  114. ) : (
  115. <PermissionAlert />
  116. )}
  117. </Fragment>
  118. );
  119. }
  120. export default OrganizationAuditLog;