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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 9x 9x 9x 7x 9x 7x 7x 7x 7x 7x 7x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 7x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 7x 7x 7x 7x 7x 9x 9x 9x 7x 7x 7x 7x 6x 1x 7x 1x 1x 7x 9x 2x 2x 2x 2x 2x 9x 9x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 9x 9x 9x 9x 2x 2x 2x 2x 2x 9x 9x 1x | import { toast } from 'sonner';
import { useMemo } from 'react';
// Components
import Chart from '@/components/Chart';
import ErrorBoundaryChart from '@/components/Chart/ErrorBoundaryChart';
import { FilterTab } from '@/components/FilterTab';
import { LightBulbIcon } from '@/components/icons/reacts';
import { LoadingOverlay } from '@/components/common/LoadingOverlay';
import { FilterCalendar } from '@/components/FilterCalendar';
import { ComparisonModeSelector } from '@/components/ComparisonModeSelector';
import { ComparisonBoxesGroup } from '@/components/ComparisonBoxesGroup';
import { OverviewBoxes } from '@/components/OverviewBoxes';
import { ChangeTimeRangeDialog } from '@/components/ChangeTimeRangeDialog';
// Utils
import { getDefaultTimeRange } from '@/utils/date';
import {
formatLabel,
getItemsForMode,
getLegendColors,
getSelectedRangesByMode,
} from '@/utils/calendar';
// Types
import { ChartKey, ChartMode, ChartType } from '@/types/chart';
import { QUERY_STATUS, Stats } from '@/types/statistic';
// Constants
import { CONTENT_MESSAGE, ERROR_MESSAGES } from '@/constants/messages';
import { PERIODIC_MODES, PERIODIC_STATISTICS } from '@/constants/filterOptions';
import { COMPARE_CHART_COLORS, DEFAULT_CHART_COLORS } from '@/constants/chart';
import { EMPTY_STAT_ITEM } from '@/constants/stat';
// Services
import { useStatistic } from '@/providers/statistic';
const Statistic = () => {
const {
filter = {
mode: ChartMode.Week,
timeRange: getDefaultTimeRange(ChartMode.Week),
totalMode: ChartMode.Week,
comparisonKey: ChartKey.SLOT,
},
isLoading,
isFetching,
shouldSyncChangeMode,
status,
legendCount = 0,
setLegendCount,
chartData,
statsData,
selectedInvalidDates,
openCalendar,
setOpenCalendar,
openDialog,
onCloseDialog: handleCloseDialog,
onChangeMode: handleChangeMode,
onChangeTotal: handleChangeTotal,
onChangeTimeRanges: handleChangeTimeRanges,
onChangeComparisonKey: handleChangeComparisonKey,
onResetSyncChangeMode,
errorContext,
} = useStatistic();
const { stats = [] } = statsData || {};
if (errorContext) {
toast.error(errorContext);
}
const safeStats = useMemo(() => {
if (
!statsData?.stats?.length ||
(statsData.stats.length === 1 && !statsData.stats[0])
)
return [];
const empty: Stats = {
lunchRegistrations: EMPTY_STAT_ITEM,
likes: EMPTY_STAT_ITEM,
dislikes: EMPTY_STAT_ITEM,
};
return statsData.stats.map((stat) => stat ?? empty);
}, [statsData?.stats]);
const colors = getLegendColors(stats?.length ?? 0, [
'primary',
'secondary',
'gold',
]);
const messageContent = useMemo(() => {
if (!selectedInvalidDates?.length) return '';
const formattedInvalidDates = selectedInvalidDates
.flatMap((item) => {
const { weekRanges, notWeekRanges } = getSelectedRangesByMode(
item,
filter.mode,
);
const items = getItemsForMode(filter.mode, weekRanges, notWeekRanges);
return items.map((i) => formatLabel(filter.mode, i));
})
.join(', ');
return CONTENT_MESSAGE.NO_DATA_AVAILABLE(formattedInvalidDates);
}, [selectedInvalidDates, filter.mode]);
const isValidData = useMemo(
() =>
!!stats.length &&
stats.every((item) => item && !!Object.keys(item).length),
[stats],
);
return (
<div className="flex flex-col items-center flex-1">
{/* Mobile unsupported message */}
<div className="lg:hidden mt-[35%] sm:mt-[30%] px-6 mx-auto ">
<div className="flex flex-row gap-2.5 flex-center bg-white rounded-xl p-3 px-4 sm:p-5 border border-border-400 dark:bg-gray-800 dark:border-border-450">
<div className="shrink-0 flex items-center justify-center size-8 sm:size-10 rounded-full bg-secondary-600">
<LightBulbIcon className="size-5 sm:size-6" />
</div>
<p className="text-4xs sm:text-3xs text-muted-foreground dark:text-background">
{CONTENT_MESSAGE.RESPONSIVE_UNSUPPORTED}
</p>
</div>
</div>
{/* Desktop */}
<div className="hidden lg:block w-full lg:px-[60px] xl:px-[80px] 2xl:px-0 2xl:w-4/5 px-2 md:px-6 mx-auto">
<div className="flex flex-col sm:flex-row sm:justify-between mt-25 sm:mt-header-nav md:mt-xl lg:mt-2xl mb-6 sm:mb-0">
<h2 className="uppercase text-muted-foreground text-xs md:text-xl font-semibold mb-4 sm:mb-0 dark:text-background">
Statistic
</h2>
<FilterTab
selectedItem={filter.mode}
data={PERIODIC_STATISTICS}
onChange={handleChangeMode}
/>
</div>
<div className="flex flex-col md:flex-row mt-2.5 w-full">
<div className="flex flex-col flex-1 bg-white dark:bg-gray-800 w-full h-full min-h-[442px] rounded-xl px-5.75 xl:px-10.5 py-4.75 xl:py-8.5 border border-border-400 dark:border-border-450 gap-5.75 relative">
{(isFetching || isLoading) && (
<LoadingOverlay className="absolute inset-0 h-[440px] rounded-xl z-10" />
)}
<div className="flex justify-between items-center min-h-[30px]">
{legendCount > 1 ? (
<div className="flex gap-7.75 items-center">
<h3 className="uppercase font-semibold text-3xs dark:text-background text-muted-foreground">
Comparison
</h3>
{isValidData && (
<ComparisonModeSelector
selectedItem={filter.comparisonKey}
onChange={handleChangeComparisonKey}
/>
)}
</div>
) : (
<h3 className="uppercase font-semibold text-3xs text-muted-foreground dark:text-background">
Overview
</h3>
)}
<FilterCalendar
mode={filter.mode}
timeRange={filter.timeRange}
isForceOpen={openCalendar}
onOpenChange={setOpenCalendar}
onLegendCountChange={setLegendCount}
onTimeRangesChange={handleChangeTimeRanges}
shouldSyncChangeMode={shouldSyncChangeMode}
onResetSyncChangeMode={onResetSyncChangeMode}
/>
</div>
{/* Top legend boxes */}
{isValidData && (
<ComparisonBoxesGroup
stats={safeStats}
comparisonMode={filter.comparisonKey}
colors={colors}
/>
)}
{safeStats[0]?.lunchRegistrations && safeStats.length < 2 && (
<OverviewBoxes
stat={safeStats[0]}
chartMode={chartData?.mode || filter.mode}
showReactions={false}
/>
)}
{/* Chart */}
<ErrorBoundaryChart fallback={<>{ERROR_MESSAGES.FALLBACK}</>}>
{isValidData && !!chartData?.data.length && (
<Chart
{...chartData}
colors={
chartData.type === ChartType.Composed
? DEFAULT_CHART_COLORS
: COMPARE_CHART_COLORS
}
{...(chartData.type === ChartType.Line && {
modeKey: filter.comparisonKey,
})}
/>
)}
{status === QUERY_STATUS.SUCCESS && !isValidData && (
<div className="flex flex-row flex-1 gap-2.5 flex-center rounded-xl border border-border-400">
<p className="text-3xs text-muted-foreground dark:text-white">
{CONTENT_MESSAGE.NO_DATA_AVAILABLE()}
</p>
</div>
)}
</ErrorBoundaryChart>
{/* Bottom legend boxes */}
{isValidData && (
<div className="flex flex-row justify-between">
{(safeStats[0]?.likes || safeStats[0]?.dislikes) &&
safeStats.length < 2 && (
<OverviewBoxes
stat={safeStats[0]}
chartMode={chartData?.mode || filter.mode}
showLunch={false}
/>
)}
{!isFetching && (
<div className="ml-auto flex justify-end min-h-[35px]">
<FilterTab
className="sm:gap-3.5"
data={PERIODIC_MODES[filter.mode]}
selectedItem={filter.totalMode}
isOnlyIcon
isShowTooltip
onChange={handleChangeTotal}
/>
</div>
)}
</div>
)}
</div>
</div>
</div>
{openDialog && (
<ChangeTimeRangeDialog
open={openDialog}
message={messageContent}
onClose={handleCloseDialog}
/>
)}
</div>
);
};
export default Statistic;
|