123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import {Fragment, useCallback, useEffect, useState} from 'react';
- import * as Sentry from '@sentry/react';
- import type {Location} from 'history';
- import {addErrorMessage} from 'sentry/actionCreators/indicator';
- import type {CursorHandler} from 'sentry/components/pagination';
- import type {AuditLog} from 'sentry/types';
- import {browserHistory} from 'sentry/utils/browserHistory';
- import {isActiveSuperuser} from 'sentry/utils/isActiveSuperuser';
- import {decodeScalar} from 'sentry/utils/queryString';
- import useApi from 'sentry/utils/useApi';
- import useOrganization from 'sentry/utils/useOrganization';
- import PermissionAlert from 'sentry/views/settings/organization/permissionAlert';
- import AuditLogList from './auditLogList';
- type Props = {
- location: Location;
- };
- type State = {
- entryList: AuditLog[] | null;
- entryListPageLinks: string | null;
- eventType: string | undefined;
- eventTypes: string[] | null;
- isLoading: boolean;
- currentCursor?: string;
- };
- function OrganizationAuditLog({location}: Props) {
- const [state, setState] = useState<State>({
- entryList: [],
- entryListPageLinks: null,
- eventType: decodeScalar(location.query.event),
- eventTypes: [],
- isLoading: true,
- });
- const organization = useOrganization();
- const api = useApi();
- const handleCursor: CursorHandler = resultsCursor => {
- setState(prevState => ({
- ...prevState,
- currentCursor: resultsCursor,
- }));
- };
- useEffect(() => {
- // Watch the location for changes so we can re-fetch data.
- const eventType = decodeScalar(location.query.event);
- setState(prevState => ({...prevState, eventType}));
- }, [location.query]);
- const fetchAuditLogData = useCallback(async () => {
- setState(prevState => ({...prevState, isLoading: true}));
- try {
- const payload = {cursor: state.currentCursor, event: state.eventType};
- if (!payload.cursor) {
- delete payload.cursor;
- }
- if (!payload.event) {
- delete payload.event;
- }
- setState(prevState => ({...prevState, isLoading: true}));
- const [data, _, response] = await api.requestPromise(
- `/organizations/${organization.slug}/audit-logs/`,
- {
- method: 'GET',
- includeAllArgs: true,
- query: payload,
- }
- );
- setState(prevState => ({
- ...prevState,
- entryList: data.rows,
- eventTypes: data.options,
- isLoading: false,
- entryListPageLinks: response?.getResponseHeader('Link') ?? null,
- }));
- } catch (err) {
- if (err.status !== 401 && err.status !== 403) {
- Sentry.captureException(err);
- }
- setState(prevState => ({
- ...prevState,
- isLoading: false,
- }));
- addErrorMessage('Unable to load audit logs.');
- }
- }, [api, organization.slug, state.currentCursor, state.eventType]);
- useEffect(() => {
- fetchAuditLogData();
- }, [fetchAuditLogData]);
- const handleEventSelect = (value: string) => {
- setState(prevState => ({
- ...prevState,
- eventType: value,
- }));
- browserHistory.push({
- pathname: location.pathname,
- query: {...location.query, event: value},
- });
- };
- return (
- <Fragment>
- {organization.access.includes('org:write') || isActiveSuperuser() ? (
- <AuditLogList
- entries={state.entryList}
- pageLinks={state.entryListPageLinks}
- eventType={state.eventType}
- eventTypes={state.eventTypes}
- onEventSelect={handleEventSelect}
- isLoading={state.isLoading}
- onCursor={handleCursor}
- />
- ) : (
- <PermissionAlert />
- )}
- </Fragment>
- );
- }
- export default OrganizationAuditLog;
|