DONG-WEI-YANG
Claude Sonnet 5
refactor(frontend): unify genogram/ecomap/key-display on shared components
668ce24 Download src/components/KeyDisplay.tsx from mkc-itltc-AIOT/simple-case-management-assistant: direct link, hf CLI and curl.
- Browser
- Download file 8.65 kB
-
https://huggingface.co/spaces/mkc-itltc-AIOT/simple-case-management-assistant/resolve/main/src/components/KeyDisplay.tsx
- Command line
-
hf download hf://spaces/mkc-itltc-AIOT/simple-case-management-assistant/src/components/KeyDisplay.tsx
-
curl -L -o KeyDisplay.tsx https://huggingface.co/spaces/mkc-itltc-AIOT/simple-case-management-assistant/resolve/main/src/components/KeyDisplay.tsx
8.65 kB
| import { useState, useEffect, useCallback, useRef } from 'react' | |
| import Button from '@/components/ui/Button' | |
| import Card from '@/components/ui/Card' | |
| interface KeyDisplayProps { | |
| keyCode: string | |
| qrCodeUrl: string | |
| onDownloadWord: () => void | |
| /** Optional — when provided, shows a "建立新紀錄" button (e.g. so the same | |
| * page can start another record without navigating away). */ | |
| onReset?: () => void | |
| } | |
| /** Calculate remaining time until 7 days from now (or from a given creation time). */ | |
| function useCountdown(daysFromNow: number = 7) { | |
| const [target] = useState(() => Date.now() + daysFromNow * 24 * 60 * 60 * 1000) | |
| const [remaining, setRemaining] = useState(() => target - Date.now()) | |
| useEffect(() => { | |
| const timer = setInterval(() => { | |
| const diff = target - Date.now() | |
| setRemaining(diff > 0 ? diff : 0) | |
| }, 1000) | |
| return () => clearInterval(timer) | |
| }, [target]) | |
| const days = Math.floor(remaining / (1000 * 60 * 60 * 24)) | |
| const hours = Math.floor((remaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) | |
| const minutes = Math.floor((remaining % (1000 * 60 * 60)) / (1000 * 60)) | |
| const seconds = Math.floor((remaining % (1000 * 60)) / 1000) | |
| return { days, hours, minutes, seconds } | |
| } | |
| export default function KeyDisplay({ | |
| keyCode, | |
| qrCodeUrl, | |
| onDownloadWord, | |
| onReset, | |
| }: KeyDisplayProps) { | |
| const [copied, setCopied] = useState(false) | |
| const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null) | |
| const countdown = useCountdown(7) | |
| useEffect(() => { | |
| return () => { | |
| if (timerRef.current) clearTimeout(timerRef.current) | |
| } | |
| }, []) | |
| const handleCopy = useCallback(async () => { | |
| try { | |
| await navigator.clipboard.writeText(keyCode) | |
| setCopied(true) | |
| timerRef.current = setTimeout(() => setCopied(false), 2000) | |
| } catch { | |
| // Fallback for older browsers / insecure contexts | |
| const textarea = document.createElement('textarea') | |
| textarea.value = keyCode | |
| textarea.style.position = 'fixed' | |
| textarea.style.opacity = '0' | |
| document.body.appendChild(textarea) | |
| textarea.select() | |
| document.execCommand('copy') | |
| document.body.removeChild(textarea) | |
| setCopied(true) | |
| timerRef.current = setTimeout(() => setCopied(false), 2000) | |
| } | |
| }, [keyCode]) | |
| return ( | |
| <Card padding="lg" className="mx-auto max-w-md"> | |
| {/* Success icon */} | |
| <div className="mb-4 flex justify-center"> | |
| <div className="flex h-14 w-14 items-center justify-center rounded-full bg-emerald-100"> | |
| <svg | |
| className="h-7 w-7 text-emerald-600" | |
| fill="none" | |
| viewBox="0 0 24 24" | |
| strokeWidth={2} | |
| stroke="currentColor" | |
| > | |
| <path | |
| strokeLinecap="round" | |
| strokeLinejoin="round" | |
| d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" | |
| /> | |
| </svg> | |
| </div> | |
| </div> | |
| <h2 className="mb-1 text-center text-lg font-semibold text-slate-900"> | |
| 資料已安全儲存 | |
| </h2> | |
| <p className="mb-6 text-center text-sm text-slate-500"> | |
| 請妥善保存以下金鑰,作為日後查閱資料的唯一憑證 | |
| </p> | |
| {/* Key code display */} | |
| <div className="mb-4 rounded-xl border-2 border-dashed border-indigo-200 bg-indigo-50 px-4 py-5"> | |
| <p className="mb-1 text-center text-xs font-medium tracking-wide text-indigo-500 uppercase"> | |
| 您的專屬金鑰 | |
| </p> | |
| <p | |
| className="select-all text-center font-mono text-xl font-bold tracking-wider text-indigo-700 sm:text-2xl" | |
| aria-label="金鑰代碼" | |
| > | |
| {keyCode} | |
| </p> | |
| </div> | |
| {/* QR Code */} | |
| {qrCodeUrl && ( | |
| <div className="mb-5 flex justify-center"> | |
| <div className="rounded-lg border border-slate-200 bg-white p-3 shadow-sm"> | |
| <img | |
| src={qrCodeUrl} | |
| alt="金鑰 QR Code" | |
| className="h-40 w-40 sm:h-48 sm:w-48" | |
| draggable={false} | |
| /> | |
| </div> | |
| </div> | |
| )} | |
| {/* Action buttons */} | |
| <div className="mb-5 flex flex-col gap-2 sm:flex-row sm:gap-3"> | |
| <Button | |
| variant={copied ? 'secondary' : 'primary'} | |
| size="md" | |
| className="flex-1" | |
| onClick={handleCopy} | |
| > | |
| {copied ? ( | |
| <> | |
| <svg | |
| className="mr-1.5 h-4 w-4" | |
| fill="none" | |
| viewBox="0 0 24 24" | |
| strokeWidth={2} | |
| stroke="currentColor" | |
| > | |
| <path | |
| strokeLinecap="round" | |
| strokeLinejoin="round" | |
| d="M4.5 12.75l6 6 9-13.5" | |
| /> | |
| </svg> | |
| 已複製 | |
| </> | |
| ) : ( | |
| <> | |
| <svg | |
| className="mr-1.5 h-4 w-4" | |
| fill="none" | |
| viewBox="0 0 24 24" | |
| strokeWidth={2} | |
| stroke="currentColor" | |
| > | |
| <path | |
| strokeLinecap="round" | |
| strokeLinejoin="round" | |
| d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 01-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 011.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 00-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 01-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 00-3.375-3.375h-1.5a1.125 1.125 0 01-1.125-1.125v-1.5a3.375 3.375 0 00-3.375-3.375H9.75" | |
| /> | |
| </svg> | |
| 複製金鑰 | |
| </> | |
| )} | |
| </Button> | |
| <Button | |
| variant="outline" | |
| size="md" | |
| className="flex-1" | |
| onClick={onDownloadWord} | |
| > | |
| <svg | |
| className="mr-1.5 h-4 w-4" | |
| fill="none" | |
| viewBox="0 0 24 24" | |
| strokeWidth={2} | |
| stroke="currentColor" | |
| > | |
| <path | |
| strokeLinecap="round" | |
| strokeLinejoin="round" | |
| d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5M16.5 12L12 16.5m0 0L7.5 12m4.5 4.5V3" | |
| /> | |
| </svg> | |
| 下載 Word 檔 | |
| </Button> | |
| </div> | |
| {/* Warning */} | |
| <div className="mb-4 rounded-lg border border-amber-200 bg-amber-50 px-4 py-3"> | |
| <div className="flex items-start gap-2.5"> | |
| <svg | |
| className="mt-0.5 h-5 w-5 shrink-0 text-amber-500" | |
| fill="none" | |
| viewBox="0 0 24 24" | |
| strokeWidth={2} | |
| stroke="currentColor" | |
| > | |
| <path | |
| strokeLinecap="round" | |
| strokeLinejoin="round" | |
| d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" | |
| /> | |
| </svg> | |
| <p className="text-sm font-medium text-amber-800"> | |
| 請務必保存此金鑰,遺失將無法取回資料 | |
| </p> | |
| </div> | |
| </div> | |
| {/* Countdown */} | |
| <div className="rounded-lg border border-slate-200 bg-slate-50 px-4 py-3"> | |
| <div className="flex items-center gap-2.5"> | |
| <svg | |
| className="h-5 w-5 shrink-0 text-slate-400" | |
| fill="none" | |
| viewBox="0 0 24 24" | |
| strokeWidth={2} | |
| stroke="currentColor" | |
| > | |
| <path | |
| strokeLinecap="round" | |
| strokeLinejoin="round" | |
| d="M12 6v6h4.5m4.5 0a9 9 0 11-18 0 9 9 0 0118 0z" | |
| /> | |
| </svg> | |
| <div> | |
| <p className="text-sm font-medium text-slate-700"> | |
| 7 日後自動銷毀 | |
| </p> | |
| <p className="mt-0.5 font-mono text-xs text-slate-500"> | |
| 剩餘{' '} | |
| <span className="font-semibold text-slate-700"> | |
| {countdown.days} 天 {String(countdown.hours).padStart(2, '0')}: | |
| {String(countdown.minutes).padStart(2, '0')}: | |
| {String(countdown.seconds).padStart(2, '0')} | |
| </span> | |
| </p> | |
| </div> | |
| </div> | |
| </div> | |
| {onReset && ( | |
| <Button | |
| variant="outline" | |
| size="md" | |
| className="mt-5 w-full" | |
| onClick={onReset} | |
| > | |
| 建立新紀錄 | |
| </Button> | |
| )} | |
| </Card> | |
| ) | |
| } | |