Skip to content

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

address review (round 12): slug boundary, optimistic revert race, editor rehydrate on layouts prop

Adam Eivye0442524960c
diff --git a/client/src/components/dashboard/LayoutEditor.jsx b/client/src/components/dashboard/LayoutEditor.jsx
index ba0f8a0028..14b580e9b5 100644
--- a/client/src/components/dashboard/LayoutEditor.jsx
+++ b/client/src/components/dashboard/LayoutEditor.jsx
@@ -31,20 +31,23 @@ export default function LayoutEditor({ layouts, activeLayoutId, limits, onClose,
requestAnimationFrame(() => closeRef.current?.focus());
}, []);
- // Rehydrate draft state ONLY when the user switches layouts. Keyed on
- // `layouts` too would clobber in-flight edits every time the parent
- // refreshes the list after save — surprised the user on save-as-new.
+ // Rehydrate draft state when the user switches layouts OR when the
+ // `layouts` prop changes (e.g. "Save as new…" calls setEditingId(newId)
+ // before the parent's setLayouts has propagated; this effect re-runs
+ // once the new entry lands so the editor shows its name/widgets).
+ // The `dirty` guard preserves in-flight edits across unrelated parent
+ // refreshes (palette-triggered layout switches, socket events, etc.).
useEffect(() => {
const cur = layouts.find((l) => l.id === editingId);
if (!cur) return;
+ if (dirty) return;
setWidgets(cur.widgets);
setName(cur.name);
- setDirty(false);
setMode('idle');
setDupName('');
setPendingSwitchId(null);
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [editingId]);
+ }, [editingId, layouts]);
// Guard layout switches when there are unsaved edits: instead of silently
// discarding the draft, surface an inline confirm in the footer.
@@ -60,7 +63,8 @@ export default function LayoutEditor({ layouts, activeLayoutId, limits, onClose,
const confirmSwitch = () => {
if (!pendingSwitchId) { setMode('idle'); return; }
- setEditingId(pendingSwitchId); // rehydrate effect clears dirty/mode
+ setDirty(false); // user chose to discard; lets the rehydrate effect run
+ setEditingId(pendingSwitchId);
};
const cancelSwitch = () => {
@@ -112,13 +116,16 @@ export default function LayoutEditor({ layouts, activeLayoutId, limits, onClose,
const trimmed = dupName.trim();
if (!trimmed) { toast.error('Name required'); return; }
// Final id must fit the server's idMax. Suffix grows with collisions
- // (-2, -10, -100) so the base is trimmed per iteration.
+ // (-2, -10, -100) so the base is trimmed per iteration. Strip trailing
+ // dashes after slicing so a boundary mid-dash doesn't produce a
+ // trailing dash (`foo-`) or, combined with the suffix, a double-dash
+ // (`foo--2`) — both violate the server's ID_PATTERN.
const baseSlug = trimmed.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
if (!baseSlug) { toast.error('Use letters/numbers in the name'); return; }
const fitId = (n) => {
- if (n <= 1) return baseSlug.slice(0, idMax);
- const suffix = `-${n}`;
- return `${baseSlug.slice(0, idMax - suffix.length)}${suffix}`;
+ const suffix = n <= 1 ? '' : `-${n}`;
+ const body = baseSlug.slice(0, idMax - suffix.length).replace(/-+$/g, '');
+ return `${body}${suffix}`;
};
const existingIds = new Set(layouts.map((l) => l.id));
let n = 1;
diff --git a/client/src/pages/Dashboard.jsx b/client/src/pages/Dashboard.jsx
index 74f2e181bb..a7cf59fe95 100644
--- a/client/src/pages/Dashboard.jsx
+++ b/client/src/pages/Dashboard.jsx
@@ -104,7 +104,12 @@ export default function Dashboard() {
setActiveLayoutId(id);
// 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));
+ // Guard the revert with a functional setState — if the user has since
+ // switched to another layout, the more recent selection wins instead
+ // of snapping back to the stale `previousId`.
+ await api.setActiveDashboardLayout(id).catch(() => {
+ setActiveLayoutId((current) => (current === id ? previousId : current));
+ });
};
const saveLayout = async ({ id, name, widgets }) => {
@@ -118,8 +123,12 @@ export default function Dashboard() {
setLayouts(result.layouts);
setActiveLayoutId(id);
// Mirror selectLayout's revert-on-failure so the picker doesn't
- // diverge from server state if the active-write fails.
- await api.setActiveDashboardLayout(id).catch(() => setActiveLayoutId(previousId));
+ // diverge from server state if the active-write fails. Only revert
+ // if the UI still reflects the id we tried to set; a later selection
+ // must not be clobbered by an earlier failed request.
+ await api.setActiveDashboardLayout(id).catch(() => {
+ setActiveLayoutId((current) => (current === id ? previousId : current));
+ });
};
const deleteLayoutById = async (id) => {