Decks: save a typed tag when the bulk Tag dialog's Done is clicked
This commit is contained in:
@@ -720,13 +720,24 @@ export function TagInput({
|
||||
onChange,
|
||||
suggestions,
|
||||
placeholder,
|
||||
text: textProp,
|
||||
onTextChange,
|
||||
}: {
|
||||
values: string[];
|
||||
onChange: (values: string[]) => void;
|
||||
suggestions: string[];
|
||||
placeholder?: string;
|
||||
/** The typed, not yet added text, for a parent that wants to act on it
|
||||
* (say, add it when its dialog is confirmed). Uncontrolled if omitted. */
|
||||
text?: string;
|
||||
onTextChange?: (text: string) => void;
|
||||
}) {
|
||||
const [text, setText] = useState("");
|
||||
const [ownText, setOwnText] = useState("");
|
||||
const text = textProp ?? ownText;
|
||||
const setText = (t: string) => {
|
||||
setOwnText(t);
|
||||
onTextChange?.(t);
|
||||
};
|
||||
const [sel, setSel] = useState(0);
|
||||
|
||||
const query = fold(text.trim());
|
||||
|
||||
@@ -552,6 +552,7 @@ function TagCardsDialog({
|
||||
}) {
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [pending, setPending] = useState("");
|
||||
const counts = new Map<string, { tag: string; n: number }>();
|
||||
for (const e of entries) {
|
||||
for (const t of e.tags) {
|
||||
@@ -562,18 +563,27 @@ function TagCardsDialog({
|
||||
}
|
||||
const used = [...counts.values()].sort((a, b) => a.tag.localeCompare(b.tag));
|
||||
|
||||
async function apply(add: string[], remove: string[]) {
|
||||
async function apply(add: string[], remove: string[]): Promise<boolean> {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
await onChange(add, remove);
|
||||
return true;
|
||||
} catch (e) {
|
||||
setError(String(e));
|
||||
return false;
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
// A tag typed but not yet added with Enter is still meant for the cards.
|
||||
async function done() {
|
||||
const tag = pending.trim();
|
||||
if (tag && !(await apply([tag], []))) return;
|
||||
onClose();
|
||||
}
|
||||
|
||||
return (
|
||||
<DialogFrame title={`Tag ${entries.length} ${entries.length === 1 ? "card" : "cards"}`} onCancel={onClose}>
|
||||
{used.length > 0 && (
|
||||
@@ -602,11 +612,13 @@ function TagCardsDialog({
|
||||
onChange={(next) => next.length > 0 && apply(next, [])}
|
||||
suggestions={suggestions}
|
||||
placeholder="New tag…"
|
||||
text={pending}
|
||||
onTextChange={setPending}
|
||||
/>
|
||||
</label>
|
||||
{error && <p className="dialog-error">{error}</p>}
|
||||
<div className="dialog-actions">
|
||||
<button className="primary" onClick={onClose}>
|
||||
<button className="primary" onClick={done} disabled={busy}>
|
||||
Done
|
||||
</button>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user