Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | /*
* Copyright 2021 Harness Inc. All rights reserved.
* Use of this source code is governed by the PolyForm Shield 1.0.0 license
* that can be found in the licenses directory at the root of this repository, also available at
* https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
*/
import React, { useMemo, useState } from 'react'
import { useParams } from 'react-router-dom'
import type { CellProps, Column, Renderer } from 'react-table'
import { Button, Container, Layout, Text, PageError, TableV2 } from '@wings-software/uicore'
import type { IconName } from '@blueprintjs/core'
import type { HarnessIconName } from '@wings-software/uicore/dist/icons/HarnessIcons'
import type { ContainerProps } from '@wings-software/uicore/dist/components/Container/Container'
import { ContainerSpinner } from '@common/components/ContainerSpinner/ContainerSpinner'
import { AuditTrail, Feature, useGetAuditByParams } from 'services/cf'
import { formatDate, formatTime, AuditLogAction, CF_DEFAULT_PAGE_SIZE, getErrorMessage } from '@cf/utils/CFUtils'
import useActiveEnvironment from '@cf/hooks/useActiveEnvironment'
import { useStrings } from 'framework/strings'
import { EventSummary } from './EventSummary'
import { translateEvents } from './AuditLogsUtils'
const RenderCellTime: Renderer<CellProps<AuditTrail>> = ({ row }) => {
const data = row.original
return (
<Text flex style={{ flexDirection: 'column', alignItems: 'baseline' }} padding={{ left: 'xsmall' }}>
<span>{formatTime(data.executedOn)}</span>
<span>{formatDate(data.executedOn)}</span>
</Text>
)
}
const RenderCellUser: Renderer<CellProps<AuditTrail>> = ({ row }) => {
const data = row.original
return (
<Text icon="person" padding={{ right: 'small' }} lineClamp={1}>
{data.actor}
</Text>
)
}
const RenderCellAction: Renderer<CellProps<AuditTrail>> = ({ row }) => {
const { getString } = useStrings()
const data = row.original
let text = getString('cf.auditLogs.unknown')
let icon: IconName | HarnessIconName = 'audit-log-created'
let color = 'var(--purple-500)'
const eventStrings = translateEvents(data.instructionSet, getString)
switch (data.action) {
case AuditLogAction.FeatureActivationCreated:
text = getString('cf.auditLogs.flagCreated')
break
case AuditLogAction.SegmentCreated:
text = getString('cf.auditLogs.segmentCreated')
break
case AuditLogAction.FeatureActivationPatched:
text = getString('cf.auditLogs.flagUpdated')
icon = 'symbol-triangle-up'
color = 'var(--sea-green-500)'
break
}
return (
<Layout.Vertical spacing="xsmall" padding={{ right: 'small' }}>
{!eventStrings.length && (
<Text icon={icon} lineClamp={1} iconProps={{ style: { color } }}>
{text}
</Text>
)}
{eventStrings.map(message => (
<Text
key={message}
icon="symbol-triangle-up"
lineClamp={1}
iconProps={{ style: { color: 'var(--sea-green-500)' } }}
>
{message}
</Text>
))}
</Layout.Vertical>
)
}
const RenderCellDetails = (onViewButtonClick: (data: AuditTrail) => void) => {
return (({ row }) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { getString } = useStrings()
const auditItem = row.original
return (
<Container flex>
{/*<Layout.Vertical spacing="xsmall" padding={{ left: 'small', right: 'small' }}>
Note: These below are for future use
<Text font={{ weight: 'bold' }}>
<span
style={{
display: 'inline-block',
padding: '3px 5px',
background: 'var(--grey-250)',
borderRadius: '4px',
fontSize: 'var(--font-size-xsmall)'
}}
>
Published
</span>
<Text
padding={{ left: 'xxxlarge' }}
inline
color={Color.RED_500}
icon="warning-sign"
iconProps={{ color: Color.RED_500 }}
>
(2)
</Text>
</Text>
<Text icon="trigger-schedule">
<strong style={{ display: 'inline-block', paddingRight: 'var(--spacing-xsmall' }}>Approver:</strong>
Olivia Dunham
</Text>
</Layout.Vertical>*/}
<Button
minimal
icon="main-notes"
tooltip={getString('cf.auditLogs.viewEventSummary')}
onClick={() => onViewButtonClick(auditItem)}
/>
</Container>
)
}) as Renderer<CellProps<AuditTrail>>
}
export interface AuditLogsListProps extends ContainerProps {
flagData: Feature
startDate: Date
endDate: Date
objectType: 'FeatureActivation' | 'Segment'
loadingStyle?: React.CSSProperties
}
export const AuditLogsList: React.FC<AuditLogsListProps> = ({
startDate,
endDate,
flagData,
objectType,
loadingStyle,
...props
}) => {
const { projectIdentifier, orgIdentifier, accountId: accountIdentifier } = useParams<Record<string, string>>()
const { activeEnvironment: environmentIdentifier } = useActiveEnvironment()
const [pageNumber, setPageNumber] = useState(0)
const queryParams = useMemo(() => {
return {
accountIdentifier,
orgIdentifier,
project: projectIdentifier,
projectIdentifier,
environmentIdentifier,
objectType,
identifier: flagData.identifier,
pageSize: CF_DEFAULT_PAGE_SIZE,
pageNumber,
startTime: startDate.getTime(),
endTime: endDate.getTime()
}
}, [projectIdentifier, environmentIdentifier, accountIdentifier, orgIdentifier, pageNumber, startDate, endDate])
const { data, loading, error, refetch } = useGetAuditByParams({
queryParams
})
const { getString } = useStrings()
const [showEventSummary, setShowEventSummary] = useState(false)
const [auditItemForSummary, setAuditItemForSummary] = useState<AuditTrail>()
const columns: Column<AuditTrail>[] = useMemo(
() => [
{
Header: getString('cf.auditLogs.timePST'),
accessor: row => row.executedOn,
width: '150px',
Cell: RenderCellTime
},
{
Header: getString('cf.auditLogs.user'),
accessor: row => row.actor,
width: 'calc(20% - 50px)',
Cell: RenderCellUser
},
{
Header: getString('cf.auditLogs.action'),
accessor: row => row.action,
width: 'calc(80% - 140px)',
Cell: RenderCellAction
},
{
Header: '',
id: 'DETAILS',
accessor: row => row.objectIdentifier,
width: '40px',
Cell: RenderCellDetails(auditItem => {
setAuditItemForSummary(auditItem)
setShowEventSummary(true)
}),
disableSortBy: true
}
],
[]
)
return (
<Container padding="xlarge" {...props}>
{!!data?.data?.auditTrails?.length && (
<TableV2<AuditTrail>
columns={columns}
data={data.data.auditTrails as AuditTrail[]}
pagination={{
itemCount: data.data.itemCount || 0,
pageSize: data.data.pageSize || 0,
pageCount: data.data.pageCount || 0,
pageIndex: pageNumber,
gotoPage: index => {
setPageNumber(index)
refetch({ queryParams: { ...queryParams, pageNumber: index } })
}
}}
/>
)}
{data?.data?.auditTrails?.length === 0 && (
<Text style={{ textAlign: 'center', paddingTop: 'var(--spacing-huge)' }}>
{getString('cf.auditLogs.empty')}
</Text>
)}
{error && (
<PageError
message={getErrorMessage(error)}
onClick={() => {
refetch()
}}
/>
)}
{loading && (
<Container
style={
loadingStyle || {
position: 'fixed',
top: '200px',
right: 0,
width: 'calc(100vw - 720px)',
height: 'calc(100% - 200px)'
}
}
>
<ContainerSpinner />
</Container>
)}
{showEventSummary && auditItemForSummary && (
<EventSummary
data={auditItemForSummary}
flagData={flagData}
onClose={() => {
setShowEventSummary(false)
}}
/>
)}
</Container>
)
}
|