Files
lotteryAdmin/src/modules/draws/draw-detail-context.tsx
kang 4484a7a77a
Some checks failed
lotteryadmin CI / build (push) Has been cancelled
Refactor agents console and related components
- Simplified share rate calculation in AgentsConsole by removing unnecessary checks and directly setting the profile share rate.
- Updated the use of `profileParentCaps` to always return total share rate in the agent profile.
- Removed unused variables and memoized calculations for improved performance.
- Cleaned up imports in various files, removing unused components and optimizing the code structure.
- Added `tRef` dependency to several useEffect hooks to ensure proper reactivity to translation changes.
- Enhanced report preview tables with better label handling for various statuses and actions.
- Updated wallet filter options to align with player-side transaction types.
- Introduced new properties in types for better type safety and clarity.
2026-06-30 17:55:00 +08:00

77 lines
2.0 KiB
TypeScript

"use client";
import {
createContext,
useCallback,
useContext,
useMemo,
useState,
type ReactNode,
} from "react";
import { getAdminDraw } from "@/api/admin-draws";
import { useAsyncEffect } from "@/hooks/use-async-effect";
import { useTranslationRef } from "@/hooks/use-translation-ref";
import { LotteryApiBizError } from "@/types/api/errors";
import type { AdminDrawShowData } from "@/types/api/admin-draws";
type DrawDetailContextValue = {
drawId: number;
draw: AdminDrawShowData | null;
loading: boolean;
error: string | null;
refresh: () => Promise<void>;
};
const DrawDetailContext = createContext<DrawDetailContextValue | null>(null);
export function DrawDetailProvider({
drawId,
children,
}: {
drawId: string;
children: ReactNode;
}): React.ReactElement {
const tRef = useTranslationRef(["draws", "common"]);
const idNum = Number(drawId);
const [draw, setDraw] = useState<AdminDrawShowData | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const refresh = useCallback(async () => {
if (!Number.isFinite(idNum)) {
setError(tRef.current("invalidDrawId"));
setDraw(null);
setLoading(false);
return;
}
setLoading(true);
setError(null);
try {
setDraw(await getAdminDraw(idNum));
} catch (e) {
setDraw(null);
setError(e instanceof LotteryApiBizError ? e.message : tRef.current("errors.loadFailed", { ns: "common" }));
} finally {
setLoading(false);
}
}, [idNum, tRef]);
useAsyncEffect(() => {
void refresh();
}, [refresh]);
const value = useMemo(
() => ({ drawId: idNum, draw, loading, error, refresh }),
[draw, error, idNum, loading, refresh],
);
return <DrawDetailContext.Provider value={value}>{children}</DrawDetailContext.Provider>;
}
export function useDrawDetail(): DrawDetailContextValue {
const ctx = useContext(DrawDetailContext);
if (ctx == null) {
throw new Error("useDrawDetail must be used within DrawDetailProvider");
}
return ctx;
}