2023-05-11 12:08:46 +00:00
|
|
|
// nivo
|
|
|
|
import { BarDatum } from "@nivo/bar";
|
|
|
|
// icons
|
|
|
|
import { getPriorityIcon } from "components/icons";
|
|
|
|
// helpers
|
|
|
|
import { addSpaceIfCamelCase } from "helpers/string.helper";
|
2023-05-15 05:52:06 +00:00
|
|
|
// helpers
|
|
|
|
import { generateBarColor, renderMonthAndYear } from "helpers/analytics.helper";
|
2023-05-11 12:08:46 +00:00
|
|
|
// types
|
|
|
|
import { IAnalyticsParams, IAnalyticsResponse } from "types";
|
|
|
|
// constants
|
2023-05-15 05:52:06 +00:00
|
|
|
import { ANALYTICS_X_AXIS_VALUES, ANALYTICS_Y_AXIS_VALUES, DATE_KEYS } from "constants/analytics";
|
|
|
|
import { MONTHS_LIST } from "constants/calendar";
|
2023-05-11 12:08:46 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
analytics: IAnalyticsResponse;
|
|
|
|
barGraphData: {
|
|
|
|
data: BarDatum[];
|
|
|
|
xAxisKeys: string[];
|
|
|
|
};
|
|
|
|
params: IAnalyticsParams;
|
|
|
|
yAxisKey: "effort" | "count";
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AnalyticsTable: React.FC<Props> = ({ analytics, barGraphData, params, yAxisKey }) => (
|
|
|
|
<div className="flow-root">
|
|
|
|
<div className="overflow-x-auto">
|
|
|
|
<div className="inline-block min-w-full align-middle">
|
|
|
|
<table className="min-w-full divide-y divide-brand-base whitespace-nowrap border-y border-brand-base">
|
|
|
|
<thead className="bg-brand-base">
|
|
|
|
<tr className="divide-x divide-brand-base text-sm text-brand-base">
|
|
|
|
<th scope="col" className="py-3 px-2.5 text-left font-medium">
|
|
|
|
{ANALYTICS_X_AXIS_VALUES.find((v) => v.value === params.x_axis)?.label}
|
|
|
|
</th>
|
|
|
|
{params.segment ? (
|
|
|
|
barGraphData.xAxisKeys.map((key) => (
|
|
|
|
<th
|
|
|
|
key={`segment-${key}`}
|
|
|
|
scope="col"
|
|
|
|
className={`px-2.5 py-3 text-left font-medium ${
|
|
|
|
params.segment === "priority" || params.segment === "state__group"
|
|
|
|
? "capitalize"
|
|
|
|
: ""
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
{params.segment === "priority" ? (
|
|
|
|
getPriorityIcon(key)
|
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
className="h-3 w-3 flex-shrink-0 rounded"
|
|
|
|
style={{
|
|
|
|
backgroundColor: generateBarColor(key, analytics, params, "segment"),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2023-05-15 05:52:06 +00:00
|
|
|
{DATE_KEYS.includes(params.segment ?? "") ? renderMonthAndYear(key) : key}
|
2023-05-11 12:08:46 +00:00
|
|
|
</div>
|
|
|
|
</th>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<th scope="col" className="py-3 px-2.5 text-left font-medium sm:pr-0">
|
|
|
|
{ANALYTICS_Y_AXIS_VALUES.find((v) => v.value === params.y_axis)?.label}
|
|
|
|
</th>
|
|
|
|
)}
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody className="divide-y divide-brand-base">
|
|
|
|
{barGraphData.data.map((item, index) => (
|
|
|
|
<tr
|
|
|
|
key={`table-row-${index}`}
|
|
|
|
className="divide-x divide-brand-base text-xs text-brand-secondary"
|
|
|
|
>
|
|
|
|
<td
|
|
|
|
className={`flex items-center gap-2 whitespace-nowrap py-2 px-2.5 font-medium ${
|
2023-05-15 05:52:06 +00:00
|
|
|
params.x_axis === "priority" || params.x_axis === "state__group"
|
|
|
|
? "capitalize"
|
|
|
|
: ""
|
2023-05-11 12:08:46 +00:00
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{params.x_axis === "priority" ? (
|
|
|
|
getPriorityIcon(`${item.name}`)
|
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
className="h-3 w-3 rounded"
|
|
|
|
style={{
|
|
|
|
backgroundColor: generateBarColor(
|
|
|
|
`${item.name}`,
|
|
|
|
analytics,
|
|
|
|
params,
|
|
|
|
"x_axis"
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{addSpaceIfCamelCase(`${item.name}`)}
|
|
|
|
</td>
|
|
|
|
{params.segment ? (
|
|
|
|
barGraphData.xAxisKeys.map((key, index) => (
|
|
|
|
<td
|
|
|
|
key={`segment-value-${index}`}
|
|
|
|
className="whitespace-nowrap py-2 px-2.5 sm:pr-0"
|
|
|
|
>
|
|
|
|
{item[key] ?? 0}
|
|
|
|
</td>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<td className="whitespace-nowrap py-2 px-2.5 sm:pr-0">{item[yAxisKey]}</td>
|
|
|
|
)}
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|