douyinWEB/frontend/src/components/Toast.tsx
eric 76afafa6c9 Initial commit: Douyin live monitor with sh2-aligned probe.
Next.js + FastAPI + PocketBase stack for room/session monitoring, cookie sync from sh2, and anti-risk polling defaults.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-08 09:03:00 -05:00

50 lines
1.7 KiB
TypeScript

"use client";
import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from "react";
type ToastItem = { id: string; text: string; tone?: "ok" | "warn" | "err" };
const ToastCtx = createContext<{ push: (text: string, tone?: ToastItem["tone"]) => void } | null>(null);
export function ToastProvider({ children }: { children: ReactNode }) {
const [items, setItems] = useState<ToastItem[]>([]);
const push = useCallback((text: string, tone: ToastItem["tone"] = "ok") => {
const id = `${Date.now()}-${Math.random()}`;
setItems((prev) => [...prev.slice(-4), { id, text, tone }]);
window.setTimeout(() => {
setItems((prev) => prev.filter((x) => x.id !== id));
}, 3200);
}, []);
const value = useMemo(() => ({ push }), [push]);
return (
<ToastCtx.Provider value={value}>
{children}
<div className="pointer-events-none fixed bottom-4 right-4 z-[80] flex w-[min(92vw,22rem)] flex-col gap-2">
{items.map((t) => (
<div
key={t.id}
className={`animate-fade-up rounded-xl border px-3 py-2 text-sm shadow-lg backdrop-blur ${
t.tone === "err"
? "border-rose-500/40 bg-rose-500/20 text-rose-50"
: t.tone === "warn"
? "border-amber-500/40 bg-amber-500/20 text-amber-50"
: "border-teal-500/40 bg-teal-500/20 text-teal-50"
}`}
>
{t.text}
</div>
))}
</div>
</ToastCtx.Provider>
);
}
export function useToast() {
const ctx = useContext(ToastCtx);
if (!ctx) return { push: (_t: string, _tone?: ToastItem["tone"]) => undefined };
return ctx;
}