"use client"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@suphark/ui/components/ui/dialog"; import { Kbd } from "@suphark/ui/components/ui/kbd"; import { Switch } from "@suphark/ui/components/ui/switch"; import { formatHotkey, setSingleKeyHotkeys, useHotkeys, useRegisteredHotkeys, } from "@suphark/ui/hooks/use-hotkeys"; import { Fragment, useId, useState } from "react"; /** * กล่องรวมคีย์ลัดของหน้า — เปิดด้วย `?` (หรือ `open`/`onOpenChange`) แสดงทุก `useHotkeys` ที่มี `label` จัดตาม `group` * พร้อมสวิตช์ปิดคีย์ลัดตัวอักษรเดี่ยว (WCAG 2.1.4) · วางครั้งเดียวที่ layout * * */ export function ShortcutsDialog({ open: controlled, onOpenChange, hotkey = "shift+?", }: { open?: boolean; onOpenChange?: (open: boolean) => void; /** คีย์ที่เปิดกล่อง (ค่าเริ่มต้น ?) — `false` = ไม่ผูกคีย์ */ hotkey?: string | false; }) { const text = useUiText().hotkeys; const [inner, setInner] = useState(false); const open = controlled ?? inner; const setOpen = (next: boolean) => { if (controlled === undefined) setInner(next); onOpenChange?.(next); }; useHotkeys(hotkey || "", () => setOpen(true), { enabled: hotkey !== false }); const { hotkeys, singleKeyEnabled } = useRegisteredHotkeys(); const switchId = useId(); const groups = new Map(); for (const h of hotkeys) { const g = h.group ?? text?.general ?? "ทั่วไป"; groups.set(g, [...(groups.get(g) ?? []), h]); } return ( {text?.title ?? "คีย์ลัด"} {text?.title ?? "คีย์ลัด"}
{hotkeys.length === 0 && (

{text?.empty ?? "หน้านี้ไม่มีคีย์ลัด"}

)} {[...groups.entries()].map(([group, items]) => (

{group}

    {items.map((h) => (
  • {h.label} {formatHotkey(h.keys).map((chord, i) => ( // biome-ignore lint/suspicious/noArrayIndexKey: ลำดับคีย์คงที่ และซ้ำกันได้ (เช่น "g g") {i > 0 && {text?.sequenceThen ?? "แล้ว"}} {chord.map((k) => ( {k} ))} ))}
  • ))}
))}

{text?.singleKeysHint ?? "ปิดได้ถ้าชนกับโปรแกรมอ่านจอหรือการพิมพ์ด้วยเสียง"}

setSingleKeyHotkeys(v)} />
); }