-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathTimeToActionChart.tsx
More file actions
358 lines (337 loc) · 10.3 KB
/
TimeToActionChart.tsx
File metadata and controls
358 lines (337 loc) · 10.3 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import {
DeleteOutlined,
EditOutlined,
EllipsisOutlined,
InfoCircleOutlined,
} from '@ant-design/icons';
import { Tooltip as AntTooltip } from 'antd';
import orderBy from 'lodash/orderBy';
import { ReactNode, useEffect, useRef, useState } from 'react';
import {
Bar,
BarChart,
CartesianGrid,
Cell,
Legend,
ResponsiveContainer,
Tooltip,
TooltipProps,
XAxis,
YAxis,
} from 'recharts';
import { Payload } from 'recharts/types/component/DefaultLegendContent';
import ComponentLoading from '../../../../components/common/ComponentLoading';
import CoopButton from '../../components/CoopButton';
import {
GQLTimeToActionFilterByInput,
useGQLGetAverageTimeToReviewLazyQuery,
useGQLManualReviewDecisionInsightsOrgInfoQuery,
} from '../../../../graphql/generated';
import { safePick } from '../../../../utils/misc';
import type { TimeDivisionOptions } from '../../overview/Overview';
import { chartColors } from '../../rules/dashboard/visualization/chartColors';
import { TimeWindow } from '../../rules/dashboard/visualization/RulesDashboardInsights';
import { ManualReviewDashboardInsightsGroupByColumns } from './ManualReviewDashboardInsightsGroupBy';
interface TimeToActionByQueueChartProps {
timeWindow: TimeWindow;
title?: string;
isCustomTitle?: boolean;
initialTimeDivision?: TimeDivisionOptions;
initialFilterBy?: Partial<GQLTimeToActionFilterByInput>;
hideGroupBy?: boolean;
hideFilterBy?: boolean;
hideTotal?: boolean;
hideChartSelection?: boolean;
hideBorder?: boolean;
hideOptions?: boolean;
infoText?: string;
narrowMode?: boolean;
onEdit?: () => void;
onDelete?: () => void;
onSelectGroupBy?: (
groupBy: ManualReviewDashboardInsightsGroupByColumns | undefined,
) => void;
onUpdateFilterBy?: (filterBy: GQLTimeToActionFilterByInput) => void;
onSelectTimeDivision?: (timeDivision: TimeDivisionOptions) => void;
}
export function getEmptyFilterState(
timeWindow: TimeWindow,
): GQLTimeToActionFilterByInput {
return {
itemTypeIds: [],
queueIds: [],
startDate: timeWindow.start,
endDate: timeWindow.end,
};
}
export default function TimeToActionByQueueChart({
timeWindow,
title,
hideBorder = false,
hideOptions = false,
infoText,
narrowMode = false,
onEdit,
onDelete,
}: TimeToActionByQueueChartProps) {
const [optionsVisible, setOptionsVisible] = useState(false);
const optionsRef = useRef<HTMLDivElement>(null);
const [
getTimeToReview,
{
loading: timeToReviewLoading,
error: timeToReviewError,
data: timeToReviewData,
},
] = useGQLGetAverageTimeToReviewLazyQuery();
const [timeToReview, loading, error] = [
timeToReviewData?.getTimeToAction,
timeToReviewLoading,
timeToReviewError,
];
useEffect(() => {
getTimeToReview({
variables: {
input: {
groupBy: ['QUEUE_ID'],
filterBy: {
startDate: timeWindow.start,
endDate: timeWindow.end,
itemTypeIds: [],
queueIds: [],
},
},
},
});
}, [getTimeToReview, timeWindow]);
const { data: orgQueryData } =
useGQLManualReviewDecisionInsightsOrgInfoQuery();
useEffect(() => {
const handleOutsideClick = (event: MouseEvent) => {
if (
optionsRef.current &&
!optionsRef.current.contains(event.target as Node)
) {
setOptionsVisible(false);
}
};
if (optionsVisible) {
document.addEventListener('click', handleOutsideClick);
}
return () => {
document.removeEventListener('click', handleOutsideClick);
};
}, [optionsVisible]);
const getQueueNameFromId = (queue_id: string | undefined) => {
if (!queue_id) {
return 'Other';
}
return (
orgQueryData?.myOrg?.mrtQueues.find((it) => it.id === queue_id)?.name ??
'Other'
);
};
const formattedData = timeToReview?.map((it) => ({
timeToAction: it.timeToAction
? Number((it.timeToAction / 60 / 60).toFixed(2))
: 0,
queue: getQueueNameFromId(it.queueId ?? undefined),
}));
const renderLegend = ({ payload }: { payload?: Payload[] }) => (
<div className="flex flex-wrap gap-1 p-1 overflow-auto border border-solid rounded max-h-24 border-slate-200">
{payload
?.filter((entry) => entry.type !== 'none')
.map((entry, index) => (
<div
key={index}
className="flex font-semibold cursor-pointer text-zinc-500 hover:opacity-70 items-center gap-1.5 text-start"
>
<div
style={{
backgroundColor: chartColors[index % chartColors.length],
}}
className="flex rounded-full h-4 w-4"
/>
{entry.value}
</div>
))}
</div>
);
if (error) {
return <div>Error fetching metrics for chart</div>;
}
const customTooltip = ({
active,
payload,
label,
}: TooltipProps<number, string>) => {
if (active && payload?.length) {
const data = orderBy(
payload
.filter((it) => it.type !== 'none')
.map((it) => safePick(it, ['name', 'value'])),
'value',
'desc',
);
return (
<div className="flex flex-col max-w-sm overflow-x-scroll bg-white rounded-lg shadow text-start">
<div className="p-3 text-white rounded-tl-lg rounded-tr-lg bg-primary">
{label}
</div>
<table className="w-full m-2">
<tbody>
{data.map((it, i) =>
it.value && it.value > 0 ? (
<tr key={i}>
<td className="pr-1 font-semibold text-primary text-end">
{it.value?.toLocaleString()}
</td>
<td className="pl-1 font-medium text-slate-700">
{it.name}
</td>
</tr>
) : null,
)}
</tbody>
</table>
</div>
);
}
return null;
};
const emptyChart = (
<div className="flex flex-col items-center justify-center h-full gap-3 p-6 bg-indigo-100 rounded">
<div className="text-sm text-slate-400">No data available for the selected time period.</div>
<CoopButton
title="Reset Filters"
onClick={() => getEmptyFilterState(timeWindow)}
size="small"
/>
</div>
);
const optionButton = (
optionTitle: string,
icon: ReactNode,
onClick?: () => void,
) => (
<div
className="flex gap-2 items-center px-2 py-0.5 m-1 text-start rounded cursor-pointer text-slate-500 font-medium bg-white hover:bg-coop-lightblue-hover"
onClick={() => {
onClick?.();
setOptionsVisible(false);
}}
>
{icon}
{optionTitle}
</div>
);
const optionsMenu = (
<div
className={`relative inline-block self-center pl-2 ${
narrowMode ? 'self-center xl:self-start' : 'self-center'
}`}
>
<div
className={`${
optionsVisible ? 'bg-slate-100' : ''
} hover:bg-slate-100 text-slate-500 px-1 cursor-pointer rounded w-fit`}
onClick={() => setOptionsVisible((prev) => !prev)}
>
<EllipsisOutlined className="flex text-2xl" />
</div>
{optionsVisible && (
<div
ref={optionsRef}
className="absolute right-0 z-30 mt-2 bg-white border border-solid rounded-md shadow-lg border-slate-200"
>
{onEdit && optionButton('Edit', <EditOutlined />, onEdit)}
{onDelete && optionButton('Delete', <DeleteOutlined />, onDelete)}
</div>
)}
</div>
);
return (
<div
className={`flex flex-col rounded-lg p-6 ${
narrowMode ? 'flex flex-col justify-between grow' : 'w-full'
} ${hideBorder ? '' : 'border border-solid border-slate-200'}`}
>
<div className="flex pb-6">
<div
className={`flex justify-between gap-2 grow ${
narrowMode ? 'flex-row xl:flex-col' : 'flex-row'
}`}
>
{title && (
<div className="flex flex-col text-start">
<div className="pb-2 text-base font-medium text-slate-500">
{title}
{infoText && (
<AntTooltip
title={infoText}
placement="topRight"
color="white"
>
<InfoCircleOutlined className="pl-2 w-fit h-fit text-slate-300" />
</AntTooltip>
)}
</div>
</div>
)}
<div
className={`flex flex-wrap gap-4 ${
narrowMode ? 'justify-end xl:justify-start' : 'justify-end'
}`}
/>
</div>
{!hideOptions && optionsMenu}
</div>
<div className="z-10 flex flex-col w-full h-full min-h-[400px] pb-4">
{!loading && formattedData?.length === 0 ? (
emptyChart
) : (
<ResponsiveContainer width="100%" height={400}>
{loading ? (
<ComponentLoading />
) : (
<BarChart data={formattedData}>
<CartesianGrid vertical={false} />
<XAxis dataKey="queue" tickLine={false} />
<YAxis
tickLine={false}
stroke="#d4d4d8"
label={{
value: `Average Time in Review (hours)`,
style: { textAnchor: 'middle' },
angle: -90,
position: 'left',
offset: 0,
}}
/>
<Legend
payload={formattedData?.map((it) => ({
value: it.queue,
}))}
content={renderLegend}
/>
<Tooltip content={customTooltip} />
<Bar
name={'Hours in Review'}
type="monotone"
dataKey={'timeToAction'}
>
{formattedData?.map((_, index) => (
<Cell
key={`cell-${index}`}
fill={chartColors[index % chartColors.length]}
/>
))}
</Bar>
</BarChart>
)}
</ResponsiveContainer>
)}
</div>
</div>
);
}