auditLogList.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. eventTypes,
  63. onCursor,
  64. onEventSelect,
  65. }: Props) => {
  66. const is24Hours = shouldUse24Hours();
  67. const hasEntries = entries && entries.length > 0;
  68. const ipv4Length = 15;
  69. const eventOptions = eventTypes?.map(type => ({
  70. label: type,
  71. value: type,
  72. }));
  73. const action = (
  74. <EventSelector
  75. clearable
  76. isDisabled={isLoading}
  77. name="eventFilter"
  78. placeholder={t('Select Action: ')}
  79. options={eventOptions}
  80. onChange={options => {
  81. onEventSelect(options?.value);
  82. }}
  83. />
  84. );
  85. return (
  86. <div>
  87. <SettingsPageHeader title={t('Audit Log')} action={action} />
  88. <PanelTable
  89. headers={[t('Member'), t('Action'), t('IP'), t('Time')]}
  90. isEmpty={!hasEntries && entries?.length === 0}
  91. emptyMessage={t('No audit entries available')}
  92. isLoading={isLoading}
  93. >
  94. {entries?.map(entry => (
  95. <Fragment key={entry.id}>
  96. <UserInfo>
  97. <div>{getAvatarDisplay(entry.actor)}</div>
  98. <NameContainer>
  99. {addUsernameDisplay(entry.actor)}
  100. <Note>{entry.note}</Note>
  101. </NameContainer>
  102. </UserInfo>
  103. <FlexCenter>
  104. <MonoDetail>{entry.event}</MonoDetail>
  105. </FlexCenter>
  106. <FlexCenter>
  107. {entry.ipAddress && (
  108. <IpAddressOverflow>
  109. <Tooltip
  110. title={entry.ipAddress}
  111. disabled={entry.ipAddress.length <= ipv4Length}
  112. >
  113. <MonoDetail>{entry.ipAddress}</MonoDetail>
  114. </Tooltip>
  115. </IpAddressOverflow>
  116. )}
  117. </FlexCenter>
  118. <TimestampInfo>
  119. <DateTime dateOnly date={entry.dateCreated} />
  120. <DateTime
  121. timeOnly
  122. format={is24Hours ? 'HH:mm zz' : 'LT zz'}
  123. date={entry.dateCreated}
  124. />
  125. </TimestampInfo>
  126. </Fragment>
  127. ))}
  128. </PanelTable>
  129. {pageLinks && <Pagination pageLinks={pageLinks} onCursor={onCursor} />}
  130. </div>
  131. );
  132. };
  133. const SentryAvatar = styled(ActivityAvatar)`
  134. margin-right: ${space(1)};
  135. `;
  136. const Name = styled('strong')`
  137. font-size: ${p => p.theme.fontSizeMedium};
  138. `;
  139. const StaffTag = styled(Tag)`
  140. padding: ${space(1)};
  141. `;
  142. const EventSelector = styled(SelectControl)`
  143. width: 250px;
  144. `;
  145. const UserInfo = styled('div')`
  146. display: flex;
  147. align-items: center;
  148. line-height: 1.2;
  149. font-size: ${p => p.theme.fontSizeSmall};
  150. min-width: 250px;
  151. `;
  152. const NameContainer = styled('div')`
  153. display: flex;
  154. flex-direction: column;
  155. justify-content: center;
  156. `;
  157. const Note = styled('div')`
  158. font-size: ${p => p.theme.fontSizeSmall};
  159. word-break: break-word;
  160. `;
  161. const FlexCenter = styled('div')`
  162. display: flex;
  163. align-items: center;
  164. `;
  165. const IpAddressOverflow = styled('div')`
  166. ${p => p.theme.overflowEllipsis};
  167. min-width: 90px;
  168. `;
  169. const MonoDetail = styled('code')`
  170. font-size: ${p => p.theme.fontSizeMedium};
  171. white-space: no-wrap;
  172. `;
  173. const TimestampInfo = styled('div')`
  174. display: grid;
  175. grid-template-rows: auto auto;
  176. gap: ${space(1)};
  177. font-size: ${p => p.theme.fontSizeMedium};
  178. `;
  179. export default AuditLogList;