167 lines
5.5 KiB
TypeScript
167 lines
5.5 KiB
TypeScript
"use client";
|
|
|
|
import { AlertCircleIcon, CheckIcon, ClockIcon, SaveIcon } from "lucide-react";
|
|
import { useState } from "react";
|
|
|
|
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import type { SaveState } from "@/hooks/use-manual-save";
|
|
import { FloatingSavePanel } from "./floating-save-panel";
|
|
|
|
interface SaveFeedbackProps {
|
|
state: SaveState;
|
|
issues?: readonly string[];
|
|
conflictVersion?: number | null;
|
|
canOverwrite?: boolean;
|
|
onReloadLatest?: () => void;
|
|
onOverwrite?: () => void;
|
|
}
|
|
|
|
function SaveFeedback({
|
|
state,
|
|
issues = [],
|
|
conflictVersion,
|
|
canOverwrite = false,
|
|
onReloadLatest,
|
|
onOverwrite,
|
|
}: SaveFeedbackProps) {
|
|
const [showIssues, setShowIssues] = useState(false);
|
|
|
|
if (state === "saving")
|
|
return (
|
|
<Badge variant="secondary">
|
|
<Spinner data-icon="inline-start" />
|
|
กำลังบันทึก…
|
|
</Badge>
|
|
);
|
|
if (state === "unsaved")
|
|
return (
|
|
<Badge variant="outline">
|
|
<ClockIcon data-icon="inline-start" />
|
|
ยังไม่ได้บันทึก
|
|
</Badge>
|
|
);
|
|
if (state === "incomplete")
|
|
return (
|
|
<div className="flex flex-col items-end gap-2">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => setShowIssues((current) => !current)}
|
|
aria-expanded={showIssues}
|
|
>
|
|
<AlertCircleIcon data-icon="inline-start" />
|
|
{showIssues ? "ซ่อนรายการ" : "ตรวจสอบข้อมูลที่ยังไม่ครบ"}
|
|
</Button>
|
|
{showIssues ? (
|
|
<Alert variant="destructive" className="max-w-md">
|
|
<AlertCircleIcon aria-hidden="true" />
|
|
<AlertTitle>กรุณากรอกข้อมูลให้ครบ</AlertTitle>
|
|
<AlertDescription>
|
|
<div className="flex flex-col gap-2">
|
|
<p>
|
|
แก้ไขรายการด้านล่าง แล้วกดปุ่มบันทึก
|
|
ข้อมูลที่กรอกไว้ยังไม่หาย
|
|
</p>
|
|
{issues.length ? (
|
|
<ul className="flex flex-col gap-1">
|
|
{issues.map((issue, index) => (
|
|
<li key={`${issue}-${index}`}>{issue}</li>
|
|
))}
|
|
</ul>
|
|
) : null}
|
|
</div>
|
|
</AlertDescription>
|
|
</Alert>
|
|
) : null}
|
|
</div>
|
|
);
|
|
if (state === "conflict")
|
|
return (
|
|
<Alert
|
|
id="save-conflict"
|
|
variant="destructive"
|
|
className="max-w-xl scroll-m-6"
|
|
>
|
|
<AlertCircleIcon aria-hidden="true" />
|
|
<AlertTitle>มีการบันทึกข้อมูลใหม่จากผู้แก้ไขคนอื่น</AlertTitle>
|
|
<AlertDescription>
|
|
<div className="flex flex-col gap-3">
|
|
<p>
|
|
กรุณาเลือกโหลดข้อมูลล่าสุดหรือเขียนทับด้วย Draft นี้
|
|
{conflictVersion ? ` (version ${conflictVersion})` : ""}
|
|
</p>
|
|
<div className="flex flex-wrap gap-2">
|
|
{onReloadLatest ? (
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={onReloadLatest}
|
|
>
|
|
โหลดข้อมูลล่าสุด
|
|
</Button>
|
|
) : null}
|
|
{onOverwrite ? (
|
|
<Button
|
|
type="button"
|
|
variant="destructive"
|
|
size="sm"
|
|
disabled={!canOverwrite}
|
|
onClick={onOverwrite}
|
|
>
|
|
เขียนทับด้วย Draft นี้
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
</AlertDescription>
|
|
</Alert>
|
|
);
|
|
if (state === "error")
|
|
return (
|
|
<div className="flex items-center gap-2">
|
|
<Badge variant="destructive">
|
|
<AlertCircleIcon data-icon="inline-start" />
|
|
บันทึกไม่สำเร็จ
|
|
</Badge>
|
|
</div>
|
|
);
|
|
return (
|
|
<Badge variant="secondary">
|
|
<CheckIcon data-icon="inline-start" />
|
|
บันทึกแล้ว
|
|
</Badge>
|
|
);
|
|
}
|
|
|
|
export function SaveStatus({
|
|
canSave,
|
|
onSave,
|
|
...props
|
|
}: SaveFeedbackProps & { canSave: boolean; onSave: () => void }) {
|
|
return (
|
|
<>
|
|
<div className="h-28" aria-hidden="true" />
|
|
<FloatingSavePanel>
|
|
<div role="status" aria-live="polite" className="min-h-0 max-w-full shrink touch-pan-y overflow-y-auto">
|
|
<SaveFeedback {...props} />
|
|
</div>
|
|
<Button
|
|
type="button"
|
|
size="lg"
|
|
className="min-h-11 w-full flex-1"
|
|
disabled={!canSave || props.state === "saving"}
|
|
onClick={onSave}
|
|
>
|
|
{props.state === "saving" ? <Spinner data-icon="inline-start" /> : <SaveIcon data-icon="inline-start" />}
|
|
{props.state === "saving" ? "กำลังบันทึก…" : props.state === "error" ? "ลองบันทึกอีกครั้ง" : "บันทึก"}
|
|
</Button>
|
|
</FloatingSavePanel>
|
|
</>
|
|
);
|
|
}
|