import { useId, useRef, useState, type RefObject } from "react"; import { tx } from "@/platform/i18n"; import { isImeKey } from "@/platform/ime"; import { Button, Checkbox, Icon, Input, Popup, T } from "@/ui"; import styles from "./Database.module.css"; interface Selection { all: boolean; names: string[]; } interface Names { available: string[]; visible: string[]; defaults?: string[]; current?: string | null; restore: () => string[]; } interface Props { title: string; kind: "databases" | "schemas"; names: Names | undefined; initial: Selection | null; tabIndex: number; protectedRules: boolean; editableName: (name: string) => boolean; loadError?: string; onLoad: () => void; onApply: (selection: Selection | null) => Promise; } /** A database row can inherit its source's schema rules; source rows edit those rules directly. */ export function IntrospectionPicker(props: Props) { const { title, names, onLoad, loadError, tabIndex } = props; const anchor = useRef(null); const [open, setOpen] = useState(false); const used = names?.visible.length ?? 0; const total = names?.available.length ?? 0; const close = () => setOpen(false); return ( e.stopPropagation()} onDoubleClick={(e) => e.stopPropagation()} onKeyDown={(e) => { if (e.key !== "Escape" && e.key !== "Tab") e.stopPropagation(); }} > {open && } ); } function PickerPopup({ title, kind, names, initial, loadError, onLoad, onApply, anchor, onClose, protectedRules, editableName, }: Props & { anchor: RefObject; onClose: () => void }) { const [query, setQuery] = useState(""); const [draft, setDraft] = useState(); const [saving, setSaving] = useState(false); const [saveError, setSaveError] = useState(); const patternHint = useId(); const selection = draft === undefined ? initial : draft; const marked = new Set( draft === undefined ? names?.visible : draft === null ? names?.defaults : draft.all ? names?.available : draft.names, ); if (names?.current) marked.add(names.current); const filter = query.trim().toLocaleLowerCase(); const matches = names?.available.filter((name) => name.toLocaleLowerCase().includes(filter)); const selectableMatches = matches?.filter((name) => name !== names?.current && editableName(name)) ?? []; const allMatchesSelected = selectableMatches.length > 0 && selectableMatches.every((name) => marked.has(name)); const apply = async () => { if (saving) return; if (draft === undefined) { onClose(); return; } try { setSaving(true); setSaveError(undefined); await onApply(draft); onClose(); } catch (e) { setSaveError(e instanceof Error ? e.message : String(e)); setSaving(false); } }; return ( { if (saving) return; if (reason === "outside") void apply(); else onClose(); }} >
{ if (e.key === "Enter" && e.target instanceof HTMLInputElement && !isImeKey(e.nativeEvent)) { e.preventDefault(); void apply(); } }} > {names ? ( <> setQuery(e.target.value)} />
{names.defaults && ( setDraft(inherit ? null : { all: false, names: [...marked] })} /> )} setDraft({ all, names: all ? [] : names.restore() })} /> {filter && matches?.length ? ( { const next = new Set(marked); for (const name of selectableMatches) { if (checked) next.add(name); else next.delete(name); } setDraft({ all: false, names: names.available.filter((name) => next.has(name)) }); }} /> ) : null}
{matches?.length ? ( matches.map((name) => ( {name} {name === names.current && ( {" "} Current database )} } checked={marked.has(name)} disabled={saving || name === names.current || !editableName(name)} onChange={(checked) => { if (checked) marked.add(name); else marked.delete(name); setDraft({ all: false, names: names.available.filter((item) => marked.has(item)) }); }} /> )) ) : ( No matching names )}
{protectedRules && ( Edit name patterns in Data Source Properties. )} ) : loadError ? (
{loadError}
) : !saveError ? ( Loading… ) : null} {saveError && {saveError}} {names && ( Enter or click outside to apply )}
); }