auditLogList.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import ActivityAvatar from 'sentry/components/activity/item/avatar';
  4. import UserAvatar from 'sentry/components/avatar/userAvatar';
  5. import DateTime from 'sentry/components/dateTime';
  6. import SelectControl from 'sentry/components/forms/selectControl';
  7. import Pagination, {CursorHandler} from 'sentry/components/pagination';
  8. import {PanelTable} from 'sentry/components/panels';
  9. import Tag from 'sentry/components/tag';
  10. import Tooltip from 'sentry/components/tooltip';
  11. import {t} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import {AuditLog, User} from 'sentry/types';
  14. import {shouldUse24Hours} from 'sentry/utils/dates';
  15. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  16. const avatarStyle = {
  17. width: 36,
  18. height: 36,
  19. marginRight: 8,
  20. };
  21. const getAvatarDisplay = (logEntryUser: User | undefined) => {
  22. // Display Sentry's avatar for system or superuser-initiated events
  23. if (
  24. logEntryUser?.isSuperuser ||
  25. (logEntryUser?.name === 'Sentry' && logEntryUser?.email === undefined)
  26. ) {
  27. return <SentryAvatar type="system" size={36} />;
  28. }
  29. // Display user's avatar for non-superusers-initiated events
  30. if (logEntryUser !== undefined) {
  31. return <UserAvatar style={avatarStyle} user={logEntryUser} />;
  32. }
  33. return null;
  34. };
  35. const addUsernameDisplay = (logEntryUser: User | undefined) => {
  36. if (logEntryUser?.isSuperuser) {
  37. return (
  38. <Name data-test-id="actor-name">
  39. {logEntryUser.name}
  40. <StaffTag>{t('Sentry Staff')}</StaffTag>
  41. </Name>
  42. );
  43. }
  44. if (logEntryUser !== undefined) {
  45. return <Name data-test-id="actor-name">{logEntryUser.name}</Name>;
  46. }
  47. return null;
  48. };
  49. type Props = {
  50. entries: AuditLog[] | null;
  51. eventType: string | undefined;
  52. eventTypes: string[] | null;
  53. isLoading: boolean;
  54. onCursor: CursorHandler | undefined;
  55. onEventSelect: (value: string) => void;
  56. pageLinks: string | null;
  57. };
  58. const AuditLogList = ({
  59. isLoading,
  60. pageLinks,
  61. entries,
  62. eventType,
  63. eventTypes,
  64. onCursor,
  65. onEventSelect,
  66. }: Props) => {
  67. const is24Hours = shouldUse24Hours();
  68. const hasEntries = entries && entries.length > 0;
  69. const ipv4Length = 15;
  70. const eventOptions = eventTypes?.map(type => ({
  71. label: type,
  72. value: type,
  73. }));
  74. const action = (
  75. <EventSelector
  76. clearable
  77. isDisabled={isLoading}
  78. name="eventFilter"
  79. value={eventType}
  80. placeholder={t('Select Action: ')}
  81. options={eventOptions}
  82. onChange={options => {
  83. onEventSelect(options?.value);
  84. }}
  85. />
  86. );
  87. return (
  88. <div>
  89. <SettingsPageHeader title={t('Audit Log')} action={action} />
  90. <PanelTable
  91. headers={[t('Member'), t('Action'), t('IP'), t('Time')]}
  92. isEmpty={!hasEntries && entries?.length === 0}
  93. emptyMessage={t('No audit entries available')}
  94. isLoading={isLoading}
  95. >
  96. {entries?.map(entry => (
  97. <Fragment key={entry.id}>
  98. <UserInfo>
  99. <div>{getAvatarDisplay(entry.actor)}</div>
  100. <NameContainer>
  101. {addUsernameDisplay(entry.actor)}
  102. <Note>{entry.note}</Note>
  103. </NameContainer>
  104. </UserInfo>
  105. <FlexCenter>
  106. <MonoDetail>{entry.event}</MonoDetail>
  107. </FlexCenter>
  108. <FlexCenter>
  109. {entry.ipAddress && (
  110. <IpAddressOverflow>
  111. <Tooltip
  112. title={entry.ipAddress}
  113. disabled={entry.ipAddress.length <= ipv4Length}
  114. >
  115. <MonoDetail>{entry.ipAddress}</MonoDetail>
  116. </Tooltip>
  117. </IpAddressOverflow>
  118. )}
  119. </FlexCenter>
  120. <TimestampInfo>
  121. <DateTime dateOnly date={entry.dateCreated} />
  122. <DateTime
  123. timeOnly
  124. format={is24Hours ? 'HH:mm zz' : 'LT zz'}
  125. date={entry.dateCreated}
  126. />
  127. </TimestampInfo>
  128. </Fragment>
  129. ))}
  130. </PanelTable>
  131. {pageLinks && <Pagination pageLinks={pageLinks} onCursor={onCursor} />}
  132. </div>
  133. );
  134. };
  135. const SentryAvatar = styled(ActivityAvatar)`
  136. margin-right: ${space(1)};
  137. `;
  138. const Name = styled('strong')`
  139. font-size: ${p => p.theme.fontSizeMedium};
  140. `;
  141. const StaffTag = styled(Tag)`
  142. padding: ${space(1)};
  143. `;
  144. const EventSelector = styled(SelectControl)`
  145. width: 250px;
  146. `;
  147. const UserInfo = styled('div')`
  148. display: flex;
  149. align-items: center;
  150. line-height: 1.2;
  151. font-size: ${p => p.theme.fontSizeSmall};
  152. min-width: 250px;
  153. `;
  154. const NameContainer = styled('div')`
  155. display: flex;
  156. flex-direction: column;
  157. justify-content: center;
  158. `;
  159. const Note = styled('div')`
  160. font-size: ${p => p.theme.fontSizeSmall};
  161. word-break: break-word;
  162. `;
  163. const FlexCenter = styled('div')`
  164. display: flex;
  165. align-items: center;
  166. `;
  167. const IpAddressOverflow = styled('div')`
  168. ${p => p.theme.overflowEllipsis};
  169. min-width: 90px;
  170. `;
  171. const MonoDetail = styled('code')`
  172. font-size: ${p => p.theme.fontSizeMedium};
  173. white-space: no-wrap;
  174. `;
  175. const TimestampInfo = styled('div')`
  176. display: grid;
  177. grid-template-rows: auto auto;
  178. gap: ${space(1)};
  179. font-size: ${p => p.theme.fontSizeMedium};
  180. `;
  181. export default AuditLogList;