Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (round 3): unhandled rejections, widget dedup, kebab validation
Adam Eivyaddb488d32e2
diff --git a/client/src/components/CmdKSearch.jsx b/client/src/components/CmdKSearch.jsxindex a14ca32b3d..ff246cac7f 100644--- a/client/src/components/CmdKSearch.jsx+++ b/client/src/components/CmdKSearch.jsx@@ -86,12 +86,16 @@ export default function CmdKSearch() {}}, [open]);- // Split into two effects so setManifest never retriggers the fetch loop:- // 1. nav + actions are static server-side; fetched once, ever.- // 2. layouts change when the user creates/renames; re-fetched on open.+ // Two effects so setManifest never retriggers a fetch loop:+ // 1. nav + actions are static server-side; fetched once per mount (guarded+ // by a ref so remounting `open` doesn't re-hit the network).+ // 2. layouts change when the user creates/renames/deletes; re-fetched on+ // every open-transition so the palette picks up changes immediately.// Each updates `manifest` functionally without depending on its value.+ const navActionsFetchedRef = useRef(false);useEffect(() => {- if (!open) return;+ if (!open || navActionsFetchedRef.current) return;+ navActionsFetchedRef.current = true;let cancelled = false;getPaletteManifest().then((data) => {if (cancelled) return;@@ -104,8 +108,6 @@ export default function CmdKSearch() {}));});return () => { cancelled = true; };- // Intentionally runs once per open-transition; `manifest` is not a dep.- // eslint-disable-next-line react-hooks/exhaustive-deps}, [open]);useEffect(() => {@@ -196,10 +198,12 @@ export default function CmdKSearch() {nav: (item) => { navigate(item.path); close(); },search: (item) => { navigate(item.url); close(); },layout: async (item) => {- await setActiveDashboardLayout(item.layoutId);- // Notify any mounted Dashboard to re-fetch — navigate('/') is a- // no-op if the user is already on the dashboard, and we otherwise- // wouldn't see the new active layout until next mount.+ // request() toasts errors centrally; swallow the rejection here so+ // clicks from sync event handlers don't bubble as unhandled. On+ // success, tell any mounted Dashboard to re-fetch because+ // navigate('/') is a no-op if the user is already there.+ const ok = await setActiveDashboardLayout(item.layoutId).then(() => true, () => false);+ if (!ok) { close(); return; }window.dispatchEvent(new CustomEvent('portos:dashboard-layout-changed'));navigate('/');toast.success(`Switched to "${item.layoutName}"`);diff --git a/client/src/components/dashboard/LayoutEditor.jsx b/client/src/components/dashboard/LayoutEditor.jsxindex 5fd519e54a..269f53c8cd 100644--- a/client/src/components/dashboard/LayoutEditor.jsx+++ b/client/src/components/dashboard/LayoutEditor.jsx@@ -65,10 +65,15 @@ export default function LayoutEditor({ layouts, activeLayoutId, onClose, onSave,setDirty(true);};+ // Each async handler awaits an API write through onSave/onDuplicate/+ // onDelete. request() toasts failures centrally; swallow here so a+ // rejection from a sync click handler doesn't surface as unhandled.+ // On failure we keep dirty/mode state so the user can retry.const save = async () => {const trimmed = name.trim();if (!trimmed) { toast.error('Name required'); return; }- await onSave({ id: editingId, name: trimmed, widgets });+ const ok = await onSave({ id: editingId, name: trimmed, widgets }).then(() => true, () => false);+ if (!ok) return;setDirty(false);toast.success('Layout saved');};@@ -82,14 +87,16 @@ export default function LayoutEditor({ layouts, activeLayoutId, onClose, onSave,let id = baseId;let n = 2;while (existingIds.has(id)) id = `${baseId}-${n++}`;- await onDuplicate({ id, name: trimmed, widgets });+ const ok = await onDuplicate({ id, name: trimmed, widgets }).then(() => true, () => false);+ if (!ok) return;setEditingId(id);setMode('idle');toast.success('New layout created');};const commitDelete = async () => {- await onDelete(editingId);+ const ok = await onDelete(editingId).then(() => true, () => false);+ if (!ok) return;const remaining = layouts.filter((l) => l.id !== editingId);setMode('idle');if (remaining.length > 0) setEditingId(remaining[0].id);diff --git a/client/src/components/dashboard/LayoutPicker.jsx b/client/src/components/dashboard/LayoutPicker.jsxindex cc29bd7374..f8d9895dfa 100644--- a/client/src/components/dashboard/LayoutPicker.jsx+++ b/client/src/components/dashboard/LayoutPicker.jsx@@ -39,7 +39,12 @@ export default function LayoutPicker({ layouts, activeLayoutId, onSelect, onEditkey={l.id}role="menuitemradio"aria-checked={l.id === activeLayoutId}- onClick={() => { onSelect(l.id); setOpen(false); }}+ onClick={() => {+ setOpen(false);+ // onSelect performs an API write; request() toasts any+ // failure, swallow here to avoid unhandled rejections.+ Promise.resolve(onSelect(l.id)).catch(() => {});+ }}className={`w-full text-left flex items-center gap-2 px-3 py-2 text-sm hover:bg-white/5 ${l.id === activeLayoutId ? 'text-white' : 'text-gray-300'}`}diff --git a/client/src/pages/Dashboard.jsx b/client/src/pages/Dashboard.jsxindex 37a0b015d0..f989962387 100644--- a/client/src/pages/Dashboard.jsx+++ b/client/src/pages/Dashboard.jsx@@ -95,12 +95,9 @@ export default function Dashboard() {const selectLayout = async (id) => {const previousId = activeLayoutId;setActiveLayoutId(id);- // Revert optimistic state on failure — request() toasts the error- // centrally, but the picker can't stay showing the wrong selection.- await api.setActiveDashboardLayout(id).catch((err) => {- setActiveLayoutId(previousId);- throw err;- });+ // Revert on failure. request() already surfaces the error via toast,+ // so swallow here to prevent an unhandled rejection from click handlers.+ await api.setActiveDashboardLayout(id).catch(() => setActiveLayoutId(previousId));};const saveLayout = async ({ id, name, widgets }) => {diff --git a/server/routes/dashboardLayouts.js b/server/routes/dashboardLayouts.jsindex 89a8e781a5..5bbbb3c4a3 100644--- a/server/routes/dashboardLayouts.js+++ b/server/routes/dashboardLayouts.js@@ -13,14 +13,19 @@ import * as svc from '../services/dashboardLayouts.js';const router = Router();+const idSchema = z.string().min(1).max(60).regex(/^[a-z0-9-]+$/, 'id must be lowercase kebab');+const layoutSchema = z.object({- id: z.string().min(1).max(60).regex(/^[a-z0-9-]+$/, 'id must be lowercase kebab'),+ id: idSchema,name: z.string().min(1).max(80),- widgets: z.array(z.string().min(1).max(80)).max(50),+ widgets: z+ .array(z.string().min(1).max(80))+ .max(50)+ .refine((w) => new Set(w).size === w.length, { message: 'widgets must be unique' }),});const setActiveSchema = z.object({- id: z.string().min(1).max(60),+ id: idSchema,});// Map service error codes to HTTP statuses. Any other error (I/O, parse,diff --git a/server/routes/dashboardLayouts.test.js b/server/routes/dashboardLayouts.test.jsindex 15cefe6059..e499392aa9 100644--- a/server/routes/dashboardLayouts.test.js+++ b/server/routes/dashboardLayouts.test.js@@ -88,6 +88,25 @@ describe('PUT /api/dashboard/layouts/:id', () => {expect(res.status).toBeGreaterThanOrEqual(400);expect(svc.saveLayout).not.toHaveBeenCalled();});++ it('rejects duplicate widget ids', async () => {+ const res = await request(makeApp())+ .put('/api/dashboard/layouts/my-custom')+ .send({ name: 'Custom', widgets: ['apps', 'cos', 'apps'] });+ expect(res.status).toBeGreaterThanOrEqual(400);+ expect(svc.saveLayout).not.toHaveBeenCalled();+ });+});++describe('PUT /api/dashboard/layouts/active — id validation', () => {+ it('rejects non-kebab ids with 400 (not 404)', async () => {+ const res = await request(makeApp())+ .put('/api/dashboard/layouts/active')+ .send({ id: 'Not_Kebab' });+ expect(res.status).toBeGreaterThanOrEqual(400);+ expect(res.status).toBeLessThan(500);+ expect(svc.setActiveLayout).not.toHaveBeenCalled();+ });});describe('DELETE /api/dashboard/layouts/:id', () => {diff --git a/server/services/dashboardLayouts.js b/server/services/dashboardLayouts.jsindex 3b0eccab9c..77aa56bf7c 100644--- a/server/services/dashboardLayouts.js+++ b/server/services/dashboardLayouts.js@@ -65,14 +65,24 @@ const DEFAULT_STATE = {const BUILTIN_IDS = new Set(DEFAULT_LAYOUTS.map((l) => l.id));// Sanitize a single layout entry — protect against hand-edits that produce-// non-object elements, missing fields, or non-array widget lists. `builtIn`-// is derived from the id, not the persisted flag, so a hand-edit flipping-// the flag can't downgrade a built-in into a deletable user layout.+// non-object elements, missing fields, non-array widget lists, or duplicate+// widget ids (duplicates would collide on React keys in the grid).+// `builtIn` is derived from the id, not the persisted flag, so flipping the+// flag can't downgrade a built-in into a deletable user layout.const sanitizeLayout = (l) => {if (!l || typeof l !== 'object') return null;if (typeof l.id !== 'string' || !l.id) return null;if (typeof l.name !== 'string' || !l.name) return null;- const widgets = Array.isArray(l.widgets) ? l.widgets.filter((w) => typeof w === 'string') : [];+ const widgets = [];+ const seen = new Set();+ if (Array.isArray(l.widgets)) {+ for (const w of l.widgets) {+ if (typeof w === 'string' && !seen.has(w)) {+ seen.add(w);+ widgets.push(w);+ }+ }+ }return { id: l.id, name: l.name, builtIn: BUILTIN_IDS.has(l.id), widgets };};