Files
fusion/packages/dashboard/app/hooks/useViewState.ts
gsxdsm 4fec139927 FN-6881: move stash recovery into Git Manager
Move stash recovery into the Git Manager while retiring the standalone dashboard route.

- Add a Recovery section to Git Manager that hosts the existing StashRecoveryView.
- Remove stash recovery from top-level, overflow, mobile, and lazy-loaded view registries.
- Preserve orphaned-stash counts on Git Manager entry points and migrate saved stash-recovery routes back to Board.
- Update dashboard docs, lazy-load inventory, tests, and add a published package changeset.

Files changed:
 .changeset/fn-6881-stash-recovery-git-manager.md   |  5 +++
 AGENTS.md                                          |  3 +-
 docs/dashboard-guide.md                            |  9 ++--
 packages/dashboard/app/App.tsx                     | 13 ------
 .../app/__tests__/lazy-loaded-views-docs.test.ts   | 11 ++---
 .../dashboard/app/components/GitManagerModal.tsx   | 19 +++++++-
 packages/dashboard/app/components/Header.css       |  9 ++++
 packages/dashboard/app/components/Header.tsx       | 22 +++-------
 .../dashboard/app/components/LeftSidebarNav.tsx    |  4 --
 packages/dashboard/app/components/MobileNavBar.tsx | 16 +------
 .../components/__tests__/GitManagerModal.test.tsx  | 51 ++++++++++++++++++++++
 .../app/components/__tests__/Header.test.tsx       | 21 +++++++++
 .../components/__tests__/LeftSidebarNav.test.tsx   | 10 ++---
 .../app/components/__tests__/MobileNavBar.test.tsx | 10 +++++
 .../app/components/__tests__/RightDock.test.tsx    |  4 +-
 .../__tests__/overflowViewRegistry.test.tsx        |  3 +-
 .../app/components/overflowViewRegistry.tsx        | 10 -----
 packages/dashboard/app/hooks/__tests__/useViewState.test.ts       | 28 ++++++++++++
 packages/dashboard/app/hooks/useViewState.ts       | 19 +++++++-
 packages/dashboard/src/view-chunk-manifest.ts      |  1 -
 20 files changed, 184 insertions(+), 84 deletions(-)

Fusion-Task-Id: FN-6881

Fusion-Task-Lineage: 467d34ec-8a6a-46db-8eb8-cb0216c031da
2026-06-21 20:28:33 -07:00

234 lines
7.6 KiB
TypeScript

import { useCallback, useEffect, useRef, useState } from "react";
import type { ThemeMode } from "@fusion/core";
import type { ProjectInfo } from "../api";
import { getScopedItem, setScopedItem } from "../utils/projectStorage";
import { getPluginViewId, isPluginViewId, isPluginViewRegistered } from "../plugins/pluginViewRegistry";
export type ViewMode = "overview" | "project";
export type BuiltInTaskView = "board" | "list" | "graph" | "agents" | "missions" | "chat" | "documents" | "research" | "evals" | "goalsView" | "todos" | "planning" | "skills" | "mailbox" | "insights" | "memory" | "command-center" | "secrets" | "devserver" | "dev-server" | "pull-requests";
export type PluginTaskView = `plugin:${string}:${string}`;
export type TaskView = BuiltInTaskView | PluginTaskView;
const BUILT_IN_TASK_VIEWS: readonly BuiltInTaskView[] = [
"board",
"list",
"graph",
"agents",
"missions",
"chat",
"documents",
"research",
"evals",
"goalsView",
/*
FNXC:ViewState 2026-06-21-09:14:
FN-6829 promotes project Todos from modal-only state into the persisted built-in task-view registry so dashboard navigation can dock it in the right content area.
*/
"todos",
/*
FNXC:Navigation 2026-06-21-00:00:
FN-6886 promotes Planning Mode into a persisted top-level docked task view instead of treating it as a modal-only overlay.
*/
"planning",
"skills",
"mailbox",
"insights",
"memory",
"command-center",
"secrets",
"devserver",
"dev-server",
"pull-requests",
];
function isBuiltInTaskView(value: string | null): value is BuiltInTaskView {
return value !== null && BUILT_IN_TASK_VIEWS.includes(value as BuiltInTaskView);
}
function isTaskView(value: string | null): value is TaskView {
return value !== null && (isBuiltInTaskView(value) || isPluginViewId(value));
}
const LEGACY_ROADMAPS_PLUGIN_VIEW = getPluginViewId("fusion-plugin-roadmap", "roadmaps");
function normalizeTaskView(value: TaskView): TaskView {
return value === "devserver" ? "dev-server" : value;
}
function migrateLegacyRoadmapsView(value: string): TaskView {
if (value !== "roadmaps") {
return "board";
}
return isPluginViewRegistered("fusion-plugin-roadmap", "roadmaps") ? LEGACY_ROADMAPS_PLUGIN_VIEW : "board";
}
/*
FNXC:ViewState 2026-06-19-00:00:
FN-6702 removed the top-level Reliability task view after moving the page into Command Center. Persisted or linked legacy `reliability` values must land users on `command-center` instead of falling back to the board or becoming invalid.
*/
function migrateLegacyReliabilityView(value: string | null): TaskView | null {
return value === "reliability" ? "command-center" : null;
}
/*
FNXC:ViewState 2026-06-21-00:00:
FN-6881 removed the standalone Stash Recovery task view after moving recovery into Git Manager. Persisted or linked `stash-recovery` values must land on Board instead of restoring an orphaned route.
*/
function migrateRetiredStashRecoveryView(value: string | null): TaskView | null {
return value === "stash-recovery" ? "board" : null;
}
interface UseViewStateOptions {
projectsLoading: boolean;
projectsError: string | null;
currentProjectLoading: boolean;
currentProject: ProjectInfo | null;
projectsLength: number;
setupWizardOpen: boolean;
openSetupWizard: () => void;
themeMode: ThemeMode;
setThemeMode: (mode: ThemeMode) => void;
}
export interface UseViewStateResult {
viewMode: ViewMode;
setViewMode: (mode: ViewMode) => void;
taskView: TaskView;
setTaskView: (view: TaskView) => void;
handleChangeTaskView: (newView: TaskView) => void;
handleToggleTheme: () => void;
}
export function useViewState(options: UseViewStateOptions): UseViewStateResult {
const {
projectsLoading,
projectsError,
currentProjectLoading,
currentProject,
projectsLength,
setupWizardOpen,
openSetupWizard,
themeMode,
setThemeMode,
} = options;
const [viewMode, setViewMode] = useState<ViewMode>(() => {
if (typeof window !== "undefined") {
const saved = window.localStorage.getItem("kb-dashboard-view-mode");
if (saved === "overview" || saved === "project") return saved;
}
return "overview";
});
const [taskView, setTaskView] = useState<TaskView>(() => {
const saved = getScopedItem("kb-dashboard-task-view");
const legacyReliabilityView = migrateLegacyReliabilityView(saved);
if (legacyReliabilityView) return legacyReliabilityView;
const retiredStashRecoveryView = migrateRetiredStashRecoveryView(saved);
if (retiredStashRecoveryView) return retiredStashRecoveryView;
if (saved === "roadmaps") return migrateLegacyRoadmapsView(saved);
if (isTaskView(saved)) return saved;
return "board";
});
const hasHydratedScopedTaskViewRef = useRef(false);
useEffect(() => {
window.localStorage.setItem("kb-dashboard-view-mode", viewMode);
}, [viewMode]);
useEffect(() => {
const saved = getScopedItem("kb-dashboard-task-view", currentProject?.id);
const legacyReliabilityView = migrateLegacyReliabilityView(saved);
const retiredStashRecoveryView = migrateRetiredStashRecoveryView(saved);
if (legacyReliabilityView) {
setTaskView(legacyReliabilityView);
} else if (retiredStashRecoveryView) {
setTaskView(retiredStashRecoveryView);
} else if (saved === "roadmaps") {
setTaskView(migrateLegacyRoadmapsView(saved));
} else if (isTaskView(saved)) {
const preserveLegacyOnFirstScopedHydration =
!hasHydratedScopedTaskViewRef.current && saved === "devserver";
setTaskView(preserveLegacyOnFirstScopedHydration ? "devserver" : normalizeTaskView(saved));
} else {
setTaskView("board");
}
if (currentProject?.id) {
hasHydratedScopedTaskViewRef.current = true;
}
}, [currentProject?.id]);
useEffect(() => {
setScopedItem("kb-dashboard-task-view", taskView, currentProject?.id);
}, [currentProject?.id, taskView]);
useEffect(() => {
if (typeof window === "undefined") {
return;
}
const viewParam = new URLSearchParams(window.location.search).get("view");
const legacyReliabilityView = migrateLegacyReliabilityView(viewParam);
const retiredStashRecoveryView = migrateRetiredStashRecoveryView(viewParam);
if (legacyReliabilityView) {
setTaskView(legacyReliabilityView);
} else if (retiredStashRecoveryView) {
setTaskView(retiredStashRecoveryView);
} else if (viewParam && isTaskView(viewParam)) {
setTaskView(normalizeTaskView(viewParam));
}
}, []);
useEffect(() => {
if (projectsLoading || currentProjectLoading) return;
if (currentProject && viewMode === "overview") {
setViewMode("project");
}
}, [projectsLoading, currentProjectLoading, currentProject, viewMode]);
useEffect(() => {
if (projectsLoading || currentProjectLoading) return;
if (setupWizardOpen) return;
if (projectsError) return;
if (projectsLength > 0 || currentProject) return;
const timer = window.setTimeout(() => {
openSetupWizard();
}, 500);
return () => window.clearTimeout(timer);
}, [
projectsLoading,
projectsError,
projectsLength,
currentProjectLoading,
currentProject,
setupWizardOpen,
openSetupWizard,
]);
const handleChangeTaskView = useCallback((newView: TaskView) => {
setTaskView(newView);
}, []);
const handleToggleTheme = useCallback(() => {
const cycle: ThemeMode[] = ["dark", "light", "system"];
const currentIndex = cycle.indexOf(themeMode);
const nextMode = cycle[(currentIndex + 1) % cycle.length];
setThemeMode(nextMode);
}, [themeMode, setThemeMode]);
return {
viewMode,
setViewMode,
taskView,
setTaskView,
handleChangeTaskView,
handleToggleTheme,
};
}