Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(settings): a table row can hold a choice, a switch and a credential

yhhbad3afa6e77e
diff --git a/desktop/src/components/SettingsDialog.tsx b/desktop/src/components/SettingsDialog.tsx
index 7bae93be1..35b75ac49 100644
--- a/desktop/src/components/SettingsDialog.tsx
+++ b/desktop/src/components/SettingsDialog.tsx
@@ -11,7 +11,7 @@ import { useEffect, useRef, useState, useSyncExternalStore } from 'react';
import type { PointerEvent as ReactPointerEvent } from 'react';
import { createPortal } from 'react-dom';
import { useShallow } from 'zustand/react/shallow';
-import { Settings as SettingsIcon, X, RotateCcw, FolderSearch, Plus, Trash2, ChevronRight, Check } from 'lucide-react';
+import { Settings as SettingsIcon, X, RotateCcw, FolderSearch, Plus, Trash2, ChevronRight, Check, KeyRound } from 'lucide-react';
import { useEditorStore } from '@/store/editorStore';
import { useSettings } from '@/store/settingsStore';
import { settingsRegistry } from '@/settings/registry';
@@ -385,6 +385,77 @@ function writePath(row: Record<string, unknown>, path: string, v: unknown): Reco
return { ...row, [head]: writePath(child, rest.join('.'), v) };
}
+/**
+ * A credential belonging to one row of a list.
+ *
+ * The same bargain as {@link SecretControl} — typed once, never read back — in
+ * the width of a table cell, so what it can show is which of the two states the
+ * row is in. A row whose key is missing is the single most common reason a
+ * configured-looking provider does nothing, so the unset state is a labelled
+ * button rather than a blank field: it says what is absent, and asks for it in
+ * place when clicked.
+ */
+function ObjectListSecret({ id }: { id: string }) {
+ const status = useSyncExternalStore(subscribeSecrets, () => secretStatus(id));
+ const [entering, setEntering] = useState(false);
+ const [draft, setDraft] = useState('');
+ useEffect(() => {
+ void refreshSecret(id);
+ }, [id]);
+
+ if (status?.configured) {
+ return (
+ <span className="set-objlist-secret on">
+ <Check size={11} strokeWidth={2.4} />
+ {t('set.secret.stored')}
+ <IconButton title={t('set.secret.forget')} onClick={() => void forgetSecret(id)}>
+ <Trash2 size={12} />
+ </IconButton>
+ </span>
+ );
+ }
+
+ const blocked = status?.storage === 'unavailable';
+ if (!entering) {
+ return (
+ <button
+ type="button"
+ className="set-objlist-secret"
+ disabled={blocked}
+ onClick={() => setEntering(true)}
+ >
+ <KeyRound size={11} strokeWidth={2} />
+ {t('set.secret.needed')}
+ </button>
+ );
+ }
+ const submit = () => {
+ if (!draft.trim()) return;
+ void storeSecret(id, draft).then((s) => {
+ if (!s?.configured) return;
+ setDraft('');
+ setEntering(false);
+ });
+ };
+ return (
+ <input
+ className="set-str"
+ type="password"
+ autoFocus
+ value={draft}
+ placeholder="sk-…"
+ spellCheck={false}
+ autoComplete="off"
+ onChange={(e) => setDraft(e.target.value)}
+ onBlur={() => { if (!draft.trim()) setEntering(false); }}
+ onKeyDown={(e) => {
+ if (e.key === 'Enter') submit();
+ if (e.key === 'Escape') { setDraft(''); setEntering(false); }
+ }}
+ />
+ );
+}
+
function ObjectListField({
col, row, onChange,
}: {
@@ -392,6 +463,36 @@ function ObjectListField({
row: Record<string, unknown>;
onChange: (v: unknown) => void;
}) {
+ if (col.type === 'secret') return <ObjectListSecret id={col.secretId?.(row) ?? ''} />;
+ if (col.type === 'enum') {
+ return (
+ <Select
+ ariaLabel={col.label}
+ value={String(readPath(row, col.key) ?? col.options?.[0]?.value ?? '')}
+ options={col.options ?? []}
+ onChange={onChange}
+ />
+ );
+ }
+ if (col.type === 'boolean') {
+ const on = readPath(row, col.key) === true;
+ return (
+ <span
+ className={`toggle${on ? ' on' : ''}`}
+ role="switch"
+ aria-checked={on}
+ aria-label={col.label}
+ tabIndex={0}
+ onClick={() => onChange(!on)}
+ onKeyDown={(e) => {
+ if (e.key === ' ' || e.key === 'Enter') {
+ e.preventDefault();
+ onChange(!on);
+ }
+ }}
+ />
+ );
+ }
return (
<input
className="set-str"
@@ -400,7 +501,12 @@ function ObjectListField({
spellCheck={false}
placeholder={col.placeholder}
value={String(readPath(row, col.key) ?? '')}
- onChange={(e) => onChange(col.type === 'number' ? Number(e.target.value) : e.target.value)}
+ // A cleared number field is EMPTY, not zero — so it keeps showing the
+ // placeholder (which is where a column says what it falls back to) instead
+ // of a 0 the row never meant and the reader cannot tell from one it did.
+ onChange={(e) => onChange(
+ col.type !== 'number' ? e.target.value : (e.target.value === '' ? undefined : Number(e.target.value)),
+ )}
/>
);
}
@@ -470,6 +576,7 @@ function ObjectListControl({ setting }: { setting: ObjectListSetting }) {
title={t('set.objList.remove')}
onClick={() => {
setValue(setting.id, rows.filter((_, j) => j !== i));
+ setting.onRowRemoved?.(row);
setExpanded(new Set());
}}
>
diff --git a/desktop/src/settings/types.ts b/desktop/src/settings/types.ts
index 640872fde..923341581 100644
--- a/desktop/src/settings/types.ts
+++ b/desktop/src/settings/types.ts
@@ -178,11 +178,26 @@ export interface FlagListSetting extends BaseSetting<number[]> {
export interface ObjectListColumn {
key: string;
label: string;
- type: 'text' | 'number';
+ /**
+ * Which control the cell renders. The scalar three store their value in the
+ * row under {@link key}; `secret` does NOT — see {@link secretId}.
+ */
+ type: 'text' | 'number' | 'enum' | 'boolean' | 'secret';
/** Grid track for this column (CSS), e.g. '1fr' or '68px'. Defaults to 1fr. */
width?: string;
placeholder?: string;
min?: number;
+ /** Choices for an `enum` cell. */
+ options?: { value: string; label: string }[];
+ /**
+ * Which credential a `secret` cell holds, derived from the row it sits in.
+ *
+ * The value never enters the row, the settings file or this process — main
+ * seals it with the OS keychain, exactly as {@link SecretSetting} does. So the
+ * column's `key` names the CELL and nothing else; a row carries the id the
+ * credential is filed under, not the credential.
+ */
+ secretId?: (row: Record<string, unknown>) => string;
}
/**
@@ -210,6 +225,13 @@ export interface ObjectListSetting extends BaseSetting<Record<string, unknown>[]
newRow: () => Record<string, unknown>;
/** Per-row validation → message, or null when the row is fine. */
rowError?: (row: Record<string, unknown>, all: Record<string, unknown>[]) => string | null;
+ /**
+ * A row is on its way out, and it owned something the list does not hold — a
+ * keychain entry, a file. Without this the thing outlives every reference to
+ * it: unreachable from the UI, still on the machine, and inherited by whatever
+ * next takes the same row id.
+ */
+ onRowRemoved?: (row: Record<string, unknown>) => void;
addLabel: string;
/** Shown in place of the table when the list is empty. */
emptyHint?: string;
diff --git a/desktop/src/store/settingsStore.ts b/desktop/src/store/settingsStore.ts
index 811dbc953..5444cbf8a 100644
--- a/desktop/src/store/settingsStore.ts
+++ b/desktop/src/store/settingsStore.ts
@@ -85,6 +85,22 @@ export const useSettings = create<SettingsState>((set, get) => ({
},
}));
+/**
+ * Forget persisted values outright — for a MIGRATION that has folded them into
+ * some newer setting, where `reset` is not the operation: it writes the default
+ * back under an id nothing reads any more, leaving the old shape in the file to
+ * be puzzled over by whoever opens it next.
+ */
+export function dropPersisted(ids: readonly string[]): void {
+ useSettings.setState((s) => {
+ if (!ids.some((id) => id in s.values)) return s;
+ const values = { ...s.values };
+ for (const id of ids) delete values[id];
+ persist(values);
+ return { values };
+ });
+}
+
/**
* Replay persisted store-owned effects once the registry is populated (call from
* boot, after registering settings). Bound settings already reflect their store.
diff --git a/desktop/src/theme/settings.css b/desktop/src/theme/settings.css
index 516e9916a..cd39840a7 100644
--- a/desktop/src/theme/settings.css
+++ b/desktop/src/theme/settings.css
@@ -348,3 +348,25 @@
.set-objlist-detail-lbl { color: var(--text-mute); font-size: var(--fs-2xs); }
.set-objlist-detail-f { display: flex; align-items: center; gap: 5px; color: var(--text-dim); font-size: var(--fs-2xs); }
.set-objlist-detail-f .set-str { width: 66px; }
+/* A dropdown and a switch in a table cell: the row is 26px, and both controls
+ are sized for the dialog's right-hand column, which is taller. */
+.set-objlist-fields .sel-trigger { height: 26px; width: 100%; }
+.set-objlist-fields .toggle,
+.set-objlist-detail-f .toggle { flex: none; }
+/* The row's credential. It is the likeliest thing to be missing on a provider
+ that looks configured, so the unset state is a labelled button rather than
+ another empty box in a row of empty boxes. */
+.set-objlist-secret {
+ display: flex; align-items: center; gap: 4px;
+ height: 26px; padding: 0 6px;
+ border-radius: var(--r-sm);
+ font-size: var(--fs-2xs);
+ color: var(--text-mute);
+ background: var(--inset);
+ border: 1px solid var(--line);
+ white-space: nowrap;
+}
+.set-objlist-secret:hover:not(.on) { color: var(--text); background: var(--hover); }
+.set-objlist-secret.on { color: var(--text-dim); border-color: transparent; background: transparent; }
+.set-objlist-secret.on svg { color: var(--run); }
+.set-objlist-secret .icon-btn { margin-left: auto; }