Download web/src/apps/code/ConfirmDelete.tsx from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/9291c0100f4e7b8da2efcbbf1ebdea72ee1663e7/web/src/apps/code/ConfirmDelete.tsx
- Command line
-
hf download hf://spaces/fsanyoto/loopable@9291c0100f4e7b8da2efcbbf1ebdea72ee1663e7/web/src/apps/code/ConfirmDelete.tsx
-
curl -L -o ConfirmDelete.tsx https://huggingface.co/spaces/fsanyoto/loopable/resolve/9291c0100f4e7b8da2efcbbf1ebdea72ee1663e7/web/src/apps/code/ConfirmDelete.tsx
3.24 kB
| import { useCallback, useEffect, useRef, useState } from "react"; | |
| import { ConfirmDialog } from "../../ui/ConfirmDialog"; | |
| import type { DeleteRequest } from "./bridgeHost"; | |
| import type { ToolConfirmation } from "./bridgeTools"; | |
| type Confirmation = ({ kind: "delete" } & DeleteRequest) | ToolConfirmation; | |
| type PendingDelete = Confirmation & { finish: (confirmed: boolean) => void }; | |
| export function useConfirmDelete() { | |
| const pending = useRef<PendingDelete | null>(null); | |
| const [request, setRequest] = useState<PendingDelete | null>(null); | |
| const confirm = useCallback((record: Confirmation, abort: AbortSignal): Promise<boolean> => { | |
| if (abort.aborted) return Promise.resolve(false); | |
| if (pending.current) { | |
| pending.current.finish(false); | |
| return Promise.resolve(false); | |
| } | |
| return new Promise((resolve) => { | |
| const finish = (confirmed: boolean) => { | |
| if (pending.current !== next) return; | |
| abort.removeEventListener("abort", canceled); | |
| pending.current = null; | |
| setRequest(null); | |
| resolve(confirmed && !abort.aborted); | |
| }; | |
| const canceled = () => finish(false); | |
| const next = { ...record, finish }; | |
| pending.current = next; | |
| abort.addEventListener("abort", canceled, { once: true }); | |
| setRequest(next); | |
| }); | |
| }, []); | |
| const confirmDelete = useCallback((record: DeleteRequest, abort: AbortSignal) => confirm({ kind: "delete", ...record }, abort), [confirm]); | |
| const confirmTool = useCallback((tool: ToolConfirmation, abort: AbortSignal) => confirm(tool, abort), [confirm]); | |
| useEffect(() => () => pending.current?.finish(false), []); | |
| return { request, confirmDelete, confirmTool }; | |
| } | |
| export default function ConfirmDelete({ request, appName }: { request: PendingDelete | null; appName: string }) { | |
| const deleting = request?.kind === "delete"; | |
| const title = deleting ? "Delete record" : request?.kind === "request" ? "Send request" : "Create schedule"; | |
| const body = !request ? undefined : request.kind === "delete" | |
| ? <span>{appName} requests deletion of record {request.id} from {request.database}.</span> | |
| : request.kind === "request" ? <div> | |
| <p>{appName} requests a question to {request.to}.</p> | |
| <p style={{ whiteSpace: "pre-wrap", overflowWrap: "anywhere" }}>{request.title}</p> | |
| <p style={{ whiteSpace: "pre-wrap", overflowWrap: "anywhere" }}>{request.body}</p> | |
| </div> : <div> | |
| <p>{appName} requests a saved query.</p> | |
| <p style={{ overflowWrap: "anywhere" }}>Database: {request.database}</p> | |
| <p style={{ overflowWrap: "anywhere" }}>When: {request.when}{request.when === "sync" || request.onceAt ? "" : ` (${request.tz})`}</p> | |
| {request.onceAt && <p style={{ overflowWrap: "anywhere" }}>Saved time: {request.onceAt}</p>} | |
| <pre style={{ whiteSpace: "pre-wrap", overflowWrap: "anywhere", font: "inherit" }}>{JSON.stringify(request.payload, null, 2)}</pre> | |
| </div>; | |
| return <ConfirmDialog open={request !== null} title={title} tone={deleting ? "destructive" : "primary"} | |
| confirmLabel={deleting ? "Delete" : request?.kind === "request" ? "Send" : "Create"} body={body} | |
| onConfirm={() => request?.finish(true)} onCancel={() => request?.finish(false)} />; | |
| } | |