feat(dashboard, risk): add SWR query hook and lazy load charts
This commit is contained in:
@@ -1,42 +1,28 @@
|
||||
"use client";
|
||||
import { AdminLoadingState, AdminLoadingInline, AdminTableLoadingRow } from "@/components/admin/admin-loading-state";
|
||||
import { AdminLoadingInline } from "@/components/admin/admin-loading-state";
|
||||
|
||||
import { useCallback, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useAsyncEffect } from "@/hooks/use-async-effect";
|
||||
import { useTranslationRef } from "@/hooks/use-translation-ref";
|
||||
import { useApiQuery } from "@/hooks/use-api-query";
|
||||
|
||||
import { getAdminDraw } from "@/api/admin-draws";
|
||||
import { drawStatusLabel, hallPreviewDiffersFromDbStatus } from "@/modules/draws/draw-display";
|
||||
import { DrawStatusBadge } from "@/modules/draws/draw-status-badge";
|
||||
import { LotteryApiBizError } from "@/types/api/errors";
|
||||
import type { AdminDrawShowData } from "@/types/api/admin-draws";
|
||||
|
||||
export function RiskDrawHeader({ drawId }: { drawId: number }) {
|
||||
const { t } = useTranslation(["risk", "draws"]);
|
||||
const tRef = useTranslationRef(["risk", "draws"]);
|
||||
const [draw, setDraw] = useState<AdminDrawShowData | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setError(null);
|
||||
try {
|
||||
const d = await getAdminDraw(drawId);
|
||||
setDraw(d);
|
||||
} catch (e) {
|
||||
const msg =
|
||||
e instanceof LotteryApiBizError ? e.message : tRef.current("drawInfoLoadFailed");
|
||||
setError(msg);
|
||||
setDraw(null);
|
||||
}
|
||||
}, [drawId]);
|
||||
|
||||
useAsyncEffect(() => {
|
||||
void load();
|
||||
}, [drawId]);
|
||||
const { data: draw, error } = useApiQuery(
|
||||
["admin/draws", drawId],
|
||||
() => getAdminDraw(drawId),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
return <p className="text-sm text-destructive">{error}</p>;
|
||||
const msg =
|
||||
error instanceof LotteryApiBizError ? error.message : tRef.current("drawInfoLoadFailed");
|
||||
return <p className="text-sm text-destructive">{msg}</p>;
|
||||
}
|
||||
|
||||
if (!draw) {
|
||||
|
||||
Reference in New Issue
Block a user