orbital-command 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +396 -0
- package/bin/orbital.js +362 -0
- package/dist/assets/WorkflowVisualizer-BZ21PIIF.js +84 -0
- package/dist/assets/WorkflowVisualizer-BZV40eAE.css +1 -0
- package/dist/assets/charts-D__PA1zp.js +72 -0
- package/dist/assets/index-D1G6i0nS.css +1 -0
- package/dist/assets/index-DpItvKpf.js +419 -0
- package/dist/assets/ui-BvF022GT.js +53 -0
- package/dist/assets/vendor-Dzv9lrRc.js +59 -0
- package/dist/index.html +19 -0
- package/dist/scanner-sweep.png +0 -0
- package/dist/server/server/adapters/index.js +34 -0
- package/dist/server/server/adapters/iterm2-adapter.js +29 -0
- package/dist/server/server/adapters/subprocess-adapter.js +21 -0
- package/dist/server/server/adapters/terminal-adapter.js +1 -0
- package/dist/server/server/config.js +156 -0
- package/dist/server/server/database.js +90 -0
- package/dist/server/server/index.js +372 -0
- package/dist/server/server/init.js +811 -0
- package/dist/server/server/parsers/event-parser.js +64 -0
- package/dist/server/server/parsers/scope-parser.js +188 -0
- package/dist/server/server/routes/config-routes.js +163 -0
- package/dist/server/server/routes/data-routes.js +461 -0
- package/dist/server/server/routes/dispatch-routes.js +215 -0
- package/dist/server/server/routes/git-routes.js +92 -0
- package/dist/server/server/routes/scope-routes.js +215 -0
- package/dist/server/server/routes/sprint-routes.js +116 -0
- package/dist/server/server/routes/version-routes.js +130 -0
- package/dist/server/server/routes/workflow-routes.js +185 -0
- package/dist/server/server/schema.js +90 -0
- package/dist/server/server/services/batch-orchestrator.js +253 -0
- package/dist/server/server/services/claude-session-service.js +352 -0
- package/dist/server/server/services/config-service.js +132 -0
- package/dist/server/server/services/deploy-service.js +51 -0
- package/dist/server/server/services/event-service.js +63 -0
- package/dist/server/server/services/gate-service.js +83 -0
- package/dist/server/server/services/git-service.js +309 -0
- package/dist/server/server/services/github-service.js +145 -0
- package/dist/server/server/services/readiness-service.js +184 -0
- package/dist/server/server/services/scope-cache.js +72 -0
- package/dist/server/server/services/scope-service.js +424 -0
- package/dist/server/server/services/sprint-orchestrator.js +312 -0
- package/dist/server/server/services/sprint-service.js +293 -0
- package/dist/server/server/services/workflow-service.js +397 -0
- package/dist/server/server/utils/cc-hooks-parser.js +49 -0
- package/dist/server/server/utils/dispatch-utils.js +305 -0
- package/dist/server/server/utils/logger.js +86 -0
- package/dist/server/server/utils/terminal-launcher.js +388 -0
- package/dist/server/server/utils/worktree-manager.js +98 -0
- package/dist/server/server/watchers/event-watcher.js +81 -0
- package/dist/server/server/watchers/scope-watcher.js +33 -0
- package/dist/server/shared/api-types.js +5 -0
- package/dist/server/shared/default-workflow.json +616 -0
- package/dist/server/shared/workflow-config.js +44 -0
- package/dist/server/shared/workflow-engine.js +353 -0
- package/index.html +15 -0
- package/package.json +110 -0
- package/postcss.config.js +6 -0
- package/schemas/orbital.config.schema.json +83 -0
- package/scripts/postinstall.js +24 -0
- package/scripts/start.sh +20 -0
- package/server/adapters/index.ts +41 -0
- package/server/adapters/iterm2-adapter.ts +37 -0
- package/server/adapters/subprocess-adapter.ts +25 -0
- package/server/adapters/terminal-adapter.ts +24 -0
- package/server/config.ts +234 -0
- package/server/database.ts +107 -0
- package/server/index.ts +452 -0
- package/server/init.ts +891 -0
- package/server/parsers/event-parser.ts +74 -0
- package/server/parsers/scope-parser.ts +240 -0
- package/server/routes/config-routes.ts +182 -0
- package/server/routes/data-routes.ts +548 -0
- package/server/routes/dispatch-routes.ts +275 -0
- package/server/routes/git-routes.ts +112 -0
- package/server/routes/scope-routes.ts +262 -0
- package/server/routes/sprint-routes.ts +142 -0
- package/server/routes/version-routes.ts +156 -0
- package/server/routes/workflow-routes.ts +198 -0
- package/server/schema.ts +90 -0
- package/server/services/batch-orchestrator.ts +286 -0
- package/server/services/claude-session-service.ts +441 -0
- package/server/services/config-service.ts +151 -0
- package/server/services/deploy-service.ts +98 -0
- package/server/services/event-service.ts +98 -0
- package/server/services/gate-service.ts +126 -0
- package/server/services/git-service.ts +391 -0
- package/server/services/github-service.ts +183 -0
- package/server/services/readiness-service.ts +250 -0
- package/server/services/scope-cache.ts +81 -0
- package/server/services/scope-service.ts +476 -0
- package/server/services/sprint-orchestrator.ts +361 -0
- package/server/services/sprint-service.ts +415 -0
- package/server/services/workflow-service.ts +461 -0
- package/server/utils/cc-hooks-parser.ts +70 -0
- package/server/utils/dispatch-utils.ts +395 -0
- package/server/utils/logger.ts +109 -0
- package/server/utils/terminal-launcher.ts +462 -0
- package/server/utils/worktree-manager.ts +104 -0
- package/server/watchers/event-watcher.ts +100 -0
- package/server/watchers/scope-watcher.ts +38 -0
- package/shared/api-types.ts +20 -0
- package/shared/default-workflow.json +616 -0
- package/shared/workflow-config.ts +170 -0
- package/shared/workflow-engine.ts +427 -0
- package/src/App.tsx +33 -0
- package/src/components/AgentBadge.tsx +40 -0
- package/src/components/BatchPreflightModal.tsx +115 -0
- package/src/components/CardDisplayToggle.tsx +74 -0
- package/src/components/ColumnHeaderActions.tsx +55 -0
- package/src/components/ColumnMenu.tsx +99 -0
- package/src/components/DeployHistory.tsx +141 -0
- package/src/components/DispatchModal.tsx +164 -0
- package/src/components/DispatchPopover.tsx +139 -0
- package/src/components/DragOverlay.tsx +25 -0
- package/src/components/DriftSidebar.tsx +140 -0
- package/src/components/EnvironmentStrip.tsx +88 -0
- package/src/components/ErrorBoundary.tsx +62 -0
- package/src/components/FilterChip.tsx +105 -0
- package/src/components/GateIndicator.tsx +33 -0
- package/src/components/IdeaDetailModal.tsx +190 -0
- package/src/components/IdeaFormDialog.tsx +113 -0
- package/src/components/KanbanColumn.tsx +201 -0
- package/src/components/MarkdownRenderer.tsx +114 -0
- package/src/components/NeonGrid.tsx +128 -0
- package/src/components/PromotionQueue.tsx +89 -0
- package/src/components/ScopeCard.tsx +234 -0
- package/src/components/ScopeDetailModal.tsx +255 -0
- package/src/components/ScopeFilterBar.tsx +152 -0
- package/src/components/SearchInput.tsx +102 -0
- package/src/components/SessionPanel.tsx +335 -0
- package/src/components/SprintContainer.tsx +303 -0
- package/src/components/SprintDependencyDialog.tsx +78 -0
- package/src/components/SprintPreflightModal.tsx +138 -0
- package/src/components/StatusBar.tsx +168 -0
- package/src/components/SwimCell.tsx +67 -0
- package/src/components/SwimLaneRow.tsx +94 -0
- package/src/components/SwimlaneBoardView.tsx +108 -0
- package/src/components/VersionBadge.tsx +139 -0
- package/src/components/ViewModeSelector.tsx +114 -0
- package/src/components/config/AgentChip.tsx +53 -0
- package/src/components/config/AgentCreateDialog.tsx +321 -0
- package/src/components/config/AgentEditor.tsx +175 -0
- package/src/components/config/DirectoryTree.tsx +582 -0
- package/src/components/config/FileEditor.tsx +550 -0
- package/src/components/config/HookChip.tsx +50 -0
- package/src/components/config/StageCard.tsx +198 -0
- package/src/components/config/TransitionZone.tsx +173 -0
- package/src/components/config/UnifiedWorkflowPipeline.tsx +216 -0
- package/src/components/config/WorkflowPipeline.tsx +161 -0
- package/src/components/source-control/BranchList.tsx +93 -0
- package/src/components/source-control/BranchPanel.tsx +105 -0
- package/src/components/source-control/CommitLog.tsx +100 -0
- package/src/components/source-control/CommitRow.tsx +47 -0
- package/src/components/source-control/GitHubPanel.tsx +110 -0
- package/src/components/source-control/GitHubSetupGuide.tsx +52 -0
- package/src/components/source-control/GitOverviewBar.tsx +101 -0
- package/src/components/source-control/PullRequestList.tsx +69 -0
- package/src/components/source-control/WorktreeList.tsx +80 -0
- package/src/components/ui/badge.tsx +41 -0
- package/src/components/ui/button.tsx +55 -0
- package/src/components/ui/card.tsx +78 -0
- package/src/components/ui/dialog.tsx +94 -0
- package/src/components/ui/popover.tsx +33 -0
- package/src/components/ui/scroll-area.tsx +54 -0
- package/src/components/ui/separator.tsx +28 -0
- package/src/components/ui/tabs.tsx +52 -0
- package/src/components/ui/toggle-switch.tsx +35 -0
- package/src/components/ui/tooltip.tsx +27 -0
- package/src/components/workflow/AddEdgeDialog.tsx +217 -0
- package/src/components/workflow/AddListDialog.tsx +201 -0
- package/src/components/workflow/ChecklistEditor.tsx +239 -0
- package/src/components/workflow/CommandPrefixManager.tsx +118 -0
- package/src/components/workflow/ConfigSettingsPanel.tsx +189 -0
- package/src/components/workflow/DirectionSelector.tsx +133 -0
- package/src/components/workflow/DispatchConfigPanel.tsx +180 -0
- package/src/components/workflow/EdgeDetailPanel.tsx +236 -0
- package/src/components/workflow/EdgePropertyEditor.tsx +251 -0
- package/src/components/workflow/EditToolbar.tsx +138 -0
- package/src/components/workflow/HookDetailPanel.tsx +250 -0
- package/src/components/workflow/HookExecutionLog.tsx +24 -0
- package/src/components/workflow/HookSourceModal.tsx +129 -0
- package/src/components/workflow/HooksDashboard.tsx +363 -0
- package/src/components/workflow/ListPropertyEditor.tsx +251 -0
- package/src/components/workflow/MigrationPreviewDialog.tsx +237 -0
- package/src/components/workflow/MovementRulesPanel.tsx +188 -0
- package/src/components/workflow/NodeDetailPanel.tsx +245 -0
- package/src/components/workflow/PresetSelector.tsx +414 -0
- package/src/components/workflow/SkillCommandBuilder.tsx +174 -0
- package/src/components/workflow/WorkflowEdgeComponent.tsx +145 -0
- package/src/components/workflow/WorkflowNode.tsx +147 -0
- package/src/components/workflow/graphLayout.ts +186 -0
- package/src/components/workflow/mergeHooks.ts +85 -0
- package/src/components/workflow/useEditHistory.ts +88 -0
- package/src/components/workflow/useWorkflowEditor.ts +262 -0
- package/src/components/workflow/validateConfig.ts +70 -0
- package/src/hooks/useActiveDispatches.ts +198 -0
- package/src/hooks/useBoardSettings.ts +170 -0
- package/src/hooks/useCardDisplay.ts +57 -0
- package/src/hooks/useCcHooks.ts +24 -0
- package/src/hooks/useConfigTree.ts +51 -0
- package/src/hooks/useEnforcementRules.ts +46 -0
- package/src/hooks/useEvents.ts +59 -0
- package/src/hooks/useFileEditor.ts +165 -0
- package/src/hooks/useGates.ts +57 -0
- package/src/hooks/useIdeaActions.ts +53 -0
- package/src/hooks/useKanbanDnd.ts +410 -0
- package/src/hooks/useOrbitalConfig.ts +54 -0
- package/src/hooks/usePipeline.ts +47 -0
- package/src/hooks/usePipelineData.ts +338 -0
- package/src/hooks/useReconnect.ts +25 -0
- package/src/hooks/useScopeFilters.ts +125 -0
- package/src/hooks/useScopeSessions.ts +44 -0
- package/src/hooks/useScopes.ts +67 -0
- package/src/hooks/useSearch.ts +67 -0
- package/src/hooks/useSettings.tsx +187 -0
- package/src/hooks/useSocket.ts +25 -0
- package/src/hooks/useSourceControl.ts +105 -0
- package/src/hooks/useSprintPreflight.ts +55 -0
- package/src/hooks/useSprints.ts +154 -0
- package/src/hooks/useStatusBarHighlight.ts +18 -0
- package/src/hooks/useSwimlaneBoardSettings.ts +104 -0
- package/src/hooks/useTheme.ts +9 -0
- package/src/hooks/useTransitionReadiness.ts +53 -0
- package/src/hooks/useVersion.ts +155 -0
- package/src/hooks/useViolations.ts +65 -0
- package/src/hooks/useWorkflow.tsx +125 -0
- package/src/hooks/useZoomModifier.ts +19 -0
- package/src/index.css +797 -0
- package/src/layouts/DashboardLayout.tsx +113 -0
- package/src/lib/collisionDetection.ts +20 -0
- package/src/lib/scope-fields.ts +61 -0
- package/src/lib/swimlane.ts +146 -0
- package/src/lib/utils.ts +15 -0
- package/src/main.tsx +19 -0
- package/src/socket.ts +11 -0
- package/src/types/index.ts +497 -0
- package/src/views/AgentFeed.tsx +339 -0
- package/src/views/DeployPipeline.tsx +59 -0
- package/src/views/EnforcementView.tsx +378 -0
- package/src/views/PrimitivesConfig.tsx +500 -0
- package/src/views/QualityGates.tsx +1012 -0
- package/src/views/ScopeBoard.tsx +454 -0
- package/src/views/SessionTimeline.tsx +516 -0
- package/src/views/Settings.tsx +183 -0
- package/src/views/SourceControl.tsx +95 -0
- package/src/views/WorkflowVisualizer.tsx +382 -0
- package/tailwind.config.js +161 -0
- package/templates/agents/AUTO-INVOKE.md +180 -0
- package/templates/agents/CONFLICT-RESOLUTION.md +128 -0
- package/templates/agents/QUICK-REFERENCE.md +122 -0
- package/templates/agents/README.md +188 -0
- package/templates/agents/SKILL-TRIGGERS.md +100 -0
- package/templates/agents/blue-team/frontend-designer.md +424 -0
- package/templates/agents/green-team/architect.md +526 -0
- package/templates/agents/green-team/rules-enforcer.md +131 -0
- package/templates/agents/red-team/attacker-learned.md +24 -0
- package/templates/agents/red-team/attacker.md +486 -0
- package/templates/agents/red-team/chaos.md +548 -0
- package/templates/agents/reference/component-registry.md +82 -0
- package/templates/agents/workflows/full-mode.md +218 -0
- package/templates/agents/workflows/quick-mode.md +118 -0
- package/templates/agents/workflows/security-mode.md +283 -0
- package/templates/anti-patterns/dangerous-shortcuts.md +427 -0
- package/templates/config/agent-triggers.json +92 -0
- package/templates/hooks/agent-team-gate.sh +31 -0
- package/templates/hooks/agent-trigger.sh +97 -0
- package/templates/hooks/block-push.sh +66 -0
- package/templates/hooks/block-workarounds.sh +61 -0
- package/templates/hooks/blocker-check.sh +28 -0
- package/templates/hooks/completion-checklist.sh +28 -0
- package/templates/hooks/decision-capture.sh +15 -0
- package/templates/hooks/dependency-check.sh +27 -0
- package/templates/hooks/end-session.sh +31 -0
- package/templates/hooks/exploration-logger.sh +37 -0
- package/templates/hooks/files-changed-summary.sh +37 -0
- package/templates/hooks/get-session-id.sh +49 -0
- package/templates/hooks/git-commit-guard.sh +34 -0
- package/templates/hooks/init-session.sh +93 -0
- package/templates/hooks/orbital-emit.sh +79 -0
- package/templates/hooks/orbital-report-deploy.sh +78 -0
- package/templates/hooks/orbital-report-gates.sh +40 -0
- package/templates/hooks/orbital-report-violation.sh +36 -0
- package/templates/hooks/orbital-scope-update.sh +53 -0
- package/templates/hooks/phase-verify-reminder.sh +26 -0
- package/templates/hooks/review-gate-check.sh +82 -0
- package/templates/hooks/scope-commit-logger.sh +37 -0
- package/templates/hooks/scope-create-cleanup.sh +36 -0
- package/templates/hooks/scope-create-gate.sh +80 -0
- package/templates/hooks/scope-create-tracker.sh +17 -0
- package/templates/hooks/scope-file-sync.sh +53 -0
- package/templates/hooks/scope-gate.sh +35 -0
- package/templates/hooks/scope-helpers.sh +188 -0
- package/templates/hooks/scope-lifecycle-gate.sh +139 -0
- package/templates/hooks/scope-prepare.sh +244 -0
- package/templates/hooks/scope-transition.sh +172 -0
- package/templates/hooks/session-enforcer.sh +143 -0
- package/templates/hooks/time-tracker.sh +33 -0
- package/templates/lessons-learned.md +15 -0
- package/templates/orbital.config.json +35 -0
- package/templates/presets/development.json +42 -0
- package/templates/presets/gitflow.json +712 -0
- package/templates/presets/minimal.json +23 -0
- package/templates/quick/rules.md +218 -0
- package/templates/scopes/_template.md +255 -0
- package/templates/settings-hooks.json +98 -0
- package/templates/skills/git-commit/SKILL.md +85 -0
- package/templates/skills/git-dev/SKILL.md +99 -0
- package/templates/skills/git-hotfix/SKILL.md +223 -0
- package/templates/skills/git-main/SKILL.md +84 -0
- package/templates/skills/git-production/SKILL.md +165 -0
- package/templates/skills/git-staging/SKILL.md +112 -0
- package/templates/skills/scope-create/SKILL.md +81 -0
- package/templates/skills/scope-fix-review/SKILL.md +168 -0
- package/templates/skills/scope-implement/SKILL.md +110 -0
- package/templates/skills/scope-post-review/SKILL.md +144 -0
- package/templates/skills/scope-pre-review/SKILL.md +211 -0
- package/templates/skills/scope-verify/SKILL.md +201 -0
- package/templates/skills/session-init/SKILL.md +62 -0
- package/templates/skills/session-resume/SKILL.md +201 -0
- package/templates/skills/test-checks/SKILL.md +171 -0
- package/templates/skills/test-code-review/SKILL.md +252 -0
- package/tsconfig.json +25 -0
- package/vite.config.ts +38 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { useState, useCallback, useMemo } from 'react';
|
|
2
|
+
import type { WorkflowConfig, WorkflowList, WorkflowEdge } from '../../../shared/workflow-config';
|
|
3
|
+
import { useEditHistory } from './useEditHistory';
|
|
4
|
+
import { validateConfig } from './validateConfig';
|
|
5
|
+
import type { ConfigValidationResult } from './validateConfig';
|
|
6
|
+
|
|
7
|
+
// ─── Types ──────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
interface MigrationPlan {
|
|
10
|
+
valid: boolean;
|
|
11
|
+
validationErrors: string[];
|
|
12
|
+
removedLists: string[];
|
|
13
|
+
addedLists: string[];
|
|
14
|
+
dirsToCreate: string[];
|
|
15
|
+
dirsToRemove: string[];
|
|
16
|
+
orphanedScopes: Array<{ listId: string; scopeFiles: string[] }>;
|
|
17
|
+
lostEdges: Array<{ from: string; to: string }>;
|
|
18
|
+
suggestedMappings: Record<string, string>;
|
|
19
|
+
impactSummary: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface WorkflowEditorState {
|
|
23
|
+
editMode: boolean;
|
|
24
|
+
editConfig: WorkflowConfig;
|
|
25
|
+
canUndo: boolean;
|
|
26
|
+
canRedo: boolean;
|
|
27
|
+
changeCount: number;
|
|
28
|
+
validation: ConfigValidationResult;
|
|
29
|
+
saving: boolean;
|
|
30
|
+
previewPlan: MigrationPlan | null;
|
|
31
|
+
previewLoading: boolean;
|
|
32
|
+
previewError: string | null;
|
|
33
|
+
showPreview: boolean;
|
|
34
|
+
showAddList: boolean;
|
|
35
|
+
showAddEdge: boolean;
|
|
36
|
+
showConfigSettings: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface WorkflowEditorActions {
|
|
40
|
+
enterEditMode: () => void;
|
|
41
|
+
exitEditMode: () => void;
|
|
42
|
+
undo: () => void;
|
|
43
|
+
redo: () => void;
|
|
44
|
+
updateList: (original: WorkflowList, updated: WorkflowList) => void;
|
|
45
|
+
deleteList: (listId: string) => void;
|
|
46
|
+
addList: (list: WorkflowList) => void;
|
|
47
|
+
updateEdge: (original: WorkflowEdge, updated: WorkflowEdge) => void;
|
|
48
|
+
deleteEdge: (from: string, to: string) => void;
|
|
49
|
+
addEdge: (edge: WorkflowEdge) => void;
|
|
50
|
+
updateConfig: (config: WorkflowConfig) => void;
|
|
51
|
+
save: () => Promise<void>;
|
|
52
|
+
discard: () => void;
|
|
53
|
+
preview: () => Promise<void>;
|
|
54
|
+
applyMigration: (orphanMappings: Record<string, string>) => Promise<void>;
|
|
55
|
+
setShowPreview: (show: boolean) => void;
|
|
56
|
+
setShowAddList: (show: boolean) => void;
|
|
57
|
+
setShowAddEdge: (show: boolean) => void;
|
|
58
|
+
setShowConfigSettings: (show: boolean) => void;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type WorkflowEditor = WorkflowEditorState & WorkflowEditorActions;
|
|
62
|
+
|
|
63
|
+
// ─── Hook ───────────────────────────────────────────────
|
|
64
|
+
|
|
65
|
+
export function useWorkflowEditor(activeConfig: WorkflowConfig): WorkflowEditor {
|
|
66
|
+
const [editMode, setEditMode] = useState(false);
|
|
67
|
+
const [saving, setSaving] = useState(false);
|
|
68
|
+
const [previewPlan, setPreviewPlan] = useState<MigrationPlan | null>(null);
|
|
69
|
+
const [previewLoading, setPreviewLoading] = useState(false);
|
|
70
|
+
const [previewError, setPreviewError] = useState<string | null>(null);
|
|
71
|
+
const [showPreview, setShowPreview] = useState(false);
|
|
72
|
+
const [showAddList, setShowAddList] = useState(false);
|
|
73
|
+
const [showAddEdge, setShowAddEdge] = useState(false);
|
|
74
|
+
const [showConfigSettings, setShowConfigSettings] = useState(false);
|
|
75
|
+
|
|
76
|
+
const history = useEditHistory(activeConfig);
|
|
77
|
+
|
|
78
|
+
const validation = useMemo(
|
|
79
|
+
() => validateConfig(history.present),
|
|
80
|
+
[history.present],
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
// ─── Mode Toggle ────────────────────────────────────
|
|
84
|
+
|
|
85
|
+
const enterEditMode = useCallback(() => {
|
|
86
|
+
history.reset(activeConfig);
|
|
87
|
+
setEditMode(true);
|
|
88
|
+
}, [activeConfig, history]);
|
|
89
|
+
|
|
90
|
+
const exitEditMode = useCallback(() => {
|
|
91
|
+
setEditMode(false);
|
|
92
|
+
setShowPreview(false);
|
|
93
|
+
setShowAddList(false);
|
|
94
|
+
setShowAddEdge(false);
|
|
95
|
+
setShowConfigSettings(false);
|
|
96
|
+
}, []);
|
|
97
|
+
|
|
98
|
+
const discard = useCallback(() => {
|
|
99
|
+
history.reset(activeConfig);
|
|
100
|
+
exitEditMode();
|
|
101
|
+
}, [activeConfig, history, exitEditMode]);
|
|
102
|
+
|
|
103
|
+
// ─── List Operations ────────────────────────────────
|
|
104
|
+
|
|
105
|
+
const addList = useCallback((list: WorkflowList) => {
|
|
106
|
+
const config = structuredClone(history.present);
|
|
107
|
+
config.lists.push(list);
|
|
108
|
+
history.pushState(config);
|
|
109
|
+
}, [history]);
|
|
110
|
+
|
|
111
|
+
const updateList = useCallback((original: WorkflowList, updated: WorkflowList) => {
|
|
112
|
+
const config = structuredClone(history.present);
|
|
113
|
+
const idx = config.lists.findIndex((l) => l.id === original.id);
|
|
114
|
+
if (idx === -1) return;
|
|
115
|
+
// If ID changed, update all edge references
|
|
116
|
+
if (original.id !== updated.id) {
|
|
117
|
+
for (const edge of config.edges) {
|
|
118
|
+
if (edge.from === original.id) edge.from = updated.id;
|
|
119
|
+
if (edge.to === original.id) edge.to = updated.id;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
config.lists[idx] = updated;
|
|
123
|
+
history.pushState(config);
|
|
124
|
+
}, [history]);
|
|
125
|
+
|
|
126
|
+
const deleteList = useCallback((listId: string) => {
|
|
127
|
+
const config = structuredClone(history.present);
|
|
128
|
+
config.lists = config.lists.filter((l) => l.id !== listId);
|
|
129
|
+
config.edges = config.edges.filter((e) => e.from !== listId && e.to !== listId);
|
|
130
|
+
history.pushState(config);
|
|
131
|
+
}, [history]);
|
|
132
|
+
|
|
133
|
+
// ─── Edge Operations ────────────────────────────────
|
|
134
|
+
|
|
135
|
+
const addEdge = useCallback((edge: WorkflowEdge) => {
|
|
136
|
+
const config = structuredClone(history.present);
|
|
137
|
+
config.edges.push(edge);
|
|
138
|
+
history.pushState(config);
|
|
139
|
+
}, [history]);
|
|
140
|
+
|
|
141
|
+
const updateConfig = useCallback((updated: WorkflowConfig) => {
|
|
142
|
+
history.pushState(structuredClone(updated));
|
|
143
|
+
}, [history]);
|
|
144
|
+
|
|
145
|
+
const updateEdge = useCallback((original: WorkflowEdge, updated: WorkflowEdge) => {
|
|
146
|
+
const config = structuredClone(history.present);
|
|
147
|
+
const key = `${original.from}:${original.to}`;
|
|
148
|
+
const idx = config.edges.findIndex((e) => `${e.from}:${e.to}` === key);
|
|
149
|
+
if (idx === -1) return;
|
|
150
|
+
config.edges[idx] = updated;
|
|
151
|
+
history.pushState(config);
|
|
152
|
+
}, [history]);
|
|
153
|
+
|
|
154
|
+
const deleteEdge = useCallback((from: string, to: string) => {
|
|
155
|
+
const config = structuredClone(history.present);
|
|
156
|
+
config.edges = config.edges.filter((e) => !(e.from === from && e.to === to));
|
|
157
|
+
history.pushState(config);
|
|
158
|
+
}, [history]);
|
|
159
|
+
|
|
160
|
+
// ─── Save ───────────────────────────────────────────
|
|
161
|
+
|
|
162
|
+
const save = useCallback(async () => {
|
|
163
|
+
if (!validation.valid || saving) return;
|
|
164
|
+
setSaving(true);
|
|
165
|
+
try {
|
|
166
|
+
const res = await fetch('/api/orbital/workflow', {
|
|
167
|
+
method: 'PUT',
|
|
168
|
+
headers: { 'Content-Type': 'application/json' },
|
|
169
|
+
body: JSON.stringify(history.present),
|
|
170
|
+
});
|
|
171
|
+
const json: { success: boolean; error?: string } = await res.json();
|
|
172
|
+
if (!json.success) throw new Error(json.error ?? 'Save failed');
|
|
173
|
+
exitEditMode();
|
|
174
|
+
} catch (err) {
|
|
175
|
+
setPreviewError(err instanceof Error ? err.message : 'Save failed');
|
|
176
|
+
} finally {
|
|
177
|
+
setSaving(false);
|
|
178
|
+
}
|
|
179
|
+
}, [validation.valid, saving, history.present, exitEditMode]);
|
|
180
|
+
|
|
181
|
+
// ─── Preview ────────────────────────────────────────
|
|
182
|
+
|
|
183
|
+
const preview = useCallback(async () => {
|
|
184
|
+
setPreviewLoading(true);
|
|
185
|
+
setPreviewError(null);
|
|
186
|
+
setPreviewPlan(null);
|
|
187
|
+
setShowPreview(true);
|
|
188
|
+
try {
|
|
189
|
+
const res = await fetch('/api/orbital/workflow/preview', {
|
|
190
|
+
method: 'POST',
|
|
191
|
+
headers: { 'Content-Type': 'application/json' },
|
|
192
|
+
body: JSON.stringify(history.present),
|
|
193
|
+
});
|
|
194
|
+
const json: { success: boolean; data?: MigrationPlan; error?: string } = await res.json();
|
|
195
|
+
if (!json.success) throw new Error(json.error ?? 'Preview failed');
|
|
196
|
+
setPreviewPlan(json.data ?? null);
|
|
197
|
+
} catch (err) {
|
|
198
|
+
setPreviewError(err instanceof Error ? err.message : 'Preview failed');
|
|
199
|
+
} finally {
|
|
200
|
+
setPreviewLoading(false);
|
|
201
|
+
}
|
|
202
|
+
}, [history.present]);
|
|
203
|
+
|
|
204
|
+
// ─── Apply Migration ────────────────────────────────
|
|
205
|
+
|
|
206
|
+
const applyMigration = useCallback(async (orphanMappings: Record<string, string>) => {
|
|
207
|
+
setSaving(true);
|
|
208
|
+
try {
|
|
209
|
+
const res = await fetch('/api/orbital/workflow/apply', {
|
|
210
|
+
method: 'POST',
|
|
211
|
+
headers: { 'Content-Type': 'application/json' },
|
|
212
|
+
body: JSON.stringify({ config: history.present, orphanMappings }),
|
|
213
|
+
});
|
|
214
|
+
const json: { success: boolean; error?: string } = await res.json();
|
|
215
|
+
if (!json.success) throw new Error(json.error ?? 'Migration failed');
|
|
216
|
+
setShowPreview(false);
|
|
217
|
+
exitEditMode();
|
|
218
|
+
} catch (err) {
|
|
219
|
+
setPreviewError(err instanceof Error ? err.message : 'Migration failed');
|
|
220
|
+
} finally {
|
|
221
|
+
setSaving(false);
|
|
222
|
+
}
|
|
223
|
+
}, [history.present, exitEditMode]);
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
// State
|
|
227
|
+
editMode,
|
|
228
|
+
editConfig: history.present,
|
|
229
|
+
canUndo: history.canUndo,
|
|
230
|
+
canRedo: history.canRedo,
|
|
231
|
+
changeCount: history.changeCount,
|
|
232
|
+
validation,
|
|
233
|
+
saving,
|
|
234
|
+
previewPlan,
|
|
235
|
+
previewLoading,
|
|
236
|
+
previewError,
|
|
237
|
+
showPreview,
|
|
238
|
+
showAddList,
|
|
239
|
+
showAddEdge,
|
|
240
|
+
showConfigSettings,
|
|
241
|
+
// Actions
|
|
242
|
+
enterEditMode,
|
|
243
|
+
exitEditMode,
|
|
244
|
+
undo: history.undo,
|
|
245
|
+
redo: history.redo,
|
|
246
|
+
updateList,
|
|
247
|
+
deleteList,
|
|
248
|
+
addList,
|
|
249
|
+
updateEdge,
|
|
250
|
+
deleteEdge,
|
|
251
|
+
addEdge,
|
|
252
|
+
updateConfig,
|
|
253
|
+
save,
|
|
254
|
+
discard,
|
|
255
|
+
preview,
|
|
256
|
+
applyMigration,
|
|
257
|
+
setShowPreview,
|
|
258
|
+
setShowAddList,
|
|
259
|
+
setShowAddEdge,
|
|
260
|
+
setShowConfigSettings,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { WorkflowConfig } from '../../../shared/workflow-config';
|
|
2
|
+
import { isWorkflowConfig } from '../../../shared/workflow-config';
|
|
3
|
+
|
|
4
|
+
// ─── Types ──────────────────────────────────────────────
|
|
5
|
+
|
|
6
|
+
export interface ConfigValidationResult {
|
|
7
|
+
valid: boolean;
|
|
8
|
+
errors: string[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// ─── Validation ─────────────────────────────────────────
|
|
12
|
+
// Mirrors server-side validation in workflow-service.ts
|
|
13
|
+
|
|
14
|
+
export function validateConfig(config: WorkflowConfig): ConfigValidationResult {
|
|
15
|
+
const errors: string[] = [];
|
|
16
|
+
|
|
17
|
+
if (!isWorkflowConfig(config)) {
|
|
18
|
+
errors.push('Invalid config shape: must have version=1, name, lists[], edges[]');
|
|
19
|
+
return { valid: false, errors };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (config.branchingMode !== undefined && config.branchingMode !== 'trunk' && config.branchingMode !== 'worktree') {
|
|
23
|
+
errors.push(`Invalid branchingMode: "${config.branchingMode}" (must be "trunk" or "worktree")`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Unique list IDs
|
|
27
|
+
const listIds = new Set<string>();
|
|
28
|
+
for (const list of config.lists) {
|
|
29
|
+
if (listIds.has(list.id)) errors.push(`Duplicate list ID: "${list.id}"`);
|
|
30
|
+
listIds.add(list.id);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Valid edge references + no duplicates
|
|
34
|
+
const edgeKeys = new Set<string>();
|
|
35
|
+
for (const edge of config.edges) {
|
|
36
|
+
if (!listIds.has(edge.from)) errors.push(`Edge references unknown list: from="${edge.from}"`);
|
|
37
|
+
if (!listIds.has(edge.to)) errors.push(`Edge references unknown list: to="${edge.to}"`);
|
|
38
|
+
if (edge.from === edge.to) errors.push(`Self-referencing edge: "${edge.from}" → "${edge.to}"`);
|
|
39
|
+
const key = `${edge.from}:${edge.to}`;
|
|
40
|
+
if (edgeKeys.has(key)) errors.push(`Duplicate edge: ${key}`);
|
|
41
|
+
edgeKeys.add(key);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Exactly 1 entry point
|
|
45
|
+
const entryPoints = config.lists.filter((l) => l.isEntryPoint);
|
|
46
|
+
if (entryPoints.length === 0) errors.push('No entry point defined (isEntryPoint=true)');
|
|
47
|
+
if (entryPoints.length > 1) errors.push(`Multiple entry points: ${entryPoints.map((l) => l.id).join(', ')}`);
|
|
48
|
+
|
|
49
|
+
// Graph connectivity — all non-terminal lists reachable from entry point
|
|
50
|
+
if (entryPoints.length === 1 && errors.length === 0) {
|
|
51
|
+
const terminal = new Set(config.terminalStatuses ?? []);
|
|
52
|
+
const reachable = new Set<string>();
|
|
53
|
+
const queue = [entryPoints[0].id];
|
|
54
|
+
while (queue.length > 0) {
|
|
55
|
+
const current = queue.shift()!;
|
|
56
|
+
if (reachable.has(current)) continue;
|
|
57
|
+
reachable.add(current);
|
|
58
|
+
for (const edge of config.edges) {
|
|
59
|
+
if (edge.from === current && !reachable.has(edge.to)) queue.push(edge.to);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
for (const list of config.lists) {
|
|
63
|
+
if (!terminal.has(list.id) && !reachable.has(list.id)) {
|
|
64
|
+
errors.push(`List "${list.id}" is not reachable from entry point`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return { valid: errors.length === 0, errors };
|
|
70
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { createContext, useContext, useState, useEffect, useCallback, useRef, useMemo } from 'react';
|
|
2
|
+
import { socket } from '../socket';
|
|
3
|
+
import type { OrbitalEvent, Scope, DispatchResolvedPayload } from '@/types';
|
|
4
|
+
import { useWorkflow } from './useWorkflow';
|
|
5
|
+
|
|
6
|
+
export interface AbandonedInfo {
|
|
7
|
+
from_status: string | null;
|
|
8
|
+
abandoned_at: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ActiveDispatchContextValue {
|
|
12
|
+
activeScopes: Set<number>;
|
|
13
|
+
abandonedScopes: Map<number, AbandonedInfo>;
|
|
14
|
+
recoverScope: (scopeId: number, fromStatus: string) => Promise<void>;
|
|
15
|
+
dismissAbandoned: (scopeId: number) => Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const DEFAULT_VALUE: ActiveDispatchContextValue = {
|
|
19
|
+
activeScopes: new Set(),
|
|
20
|
+
abandonedScopes: new Map(),
|
|
21
|
+
recoverScope: async () => {},
|
|
22
|
+
dismissAbandoned: async () => {},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const ActiveDispatchContext = createContext<ActiveDispatchContextValue>(DEFAULT_VALUE);
|
|
26
|
+
|
|
27
|
+
/** Provider hook — call once at ScopeBoard level.
|
|
28
|
+
* Fetches initial set from REST, then maintains via socket events. */
|
|
29
|
+
export function useActiveDispatchProvider(): ActiveDispatchContextValue {
|
|
30
|
+
const { engine } = useWorkflow();
|
|
31
|
+
const terminalStatuses = useMemo(
|
|
32
|
+
() => new Set(engine.getConfig().terminalStatuses ?? []),
|
|
33
|
+
[engine],
|
|
34
|
+
);
|
|
35
|
+
const [activeScopes, setActiveScopes] = useState<Set<number>>(new Set());
|
|
36
|
+
const [abandonedScopes, setAbandonedScopes] = useState<Map<number, AbandonedInfo>>(new Map());
|
|
37
|
+
const mountedRef = useRef(true);
|
|
38
|
+
|
|
39
|
+
const removeFromAbandoned = useCallback((scopeId: number) => {
|
|
40
|
+
setAbandonedScopes((prev) => {
|
|
41
|
+
if (!prev.has(scopeId)) return prev;
|
|
42
|
+
const next = new Map(prev);
|
|
43
|
+
next.delete(scopeId);
|
|
44
|
+
return next;
|
|
45
|
+
});
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
48
|
+
const fetchActiveScopes = useCallback(async () => {
|
|
49
|
+
try {
|
|
50
|
+
const res = await fetch('/api/orbital/dispatch/active-scopes');
|
|
51
|
+
if (!res.ok) {
|
|
52
|
+
console.warn('[Orbital] Failed to fetch active scopes:', res.status, res.statusText);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const data = await res.json() as {
|
|
56
|
+
scope_ids: number[];
|
|
57
|
+
abandoned_scopes?: Array<{ scope_id: number; from_status: string | null; abandoned_at: string }>;
|
|
58
|
+
};
|
|
59
|
+
if (!mountedRef.current) return;
|
|
60
|
+
setActiveScopes(new Set(data.scope_ids));
|
|
61
|
+
|
|
62
|
+
if (data.abandoned_scopes) {
|
|
63
|
+
const map = new Map<number, AbandonedInfo>();
|
|
64
|
+
for (const item of data.abandoned_scopes) {
|
|
65
|
+
map.set(item.scope_id, { from_status: item.from_status, abandoned_at: item.abandoned_at });
|
|
66
|
+
}
|
|
67
|
+
setAbandonedScopes(map);
|
|
68
|
+
}
|
|
69
|
+
} catch {
|
|
70
|
+
// Silently ignore — will retry on next reconnect
|
|
71
|
+
}
|
|
72
|
+
}, []);
|
|
73
|
+
|
|
74
|
+
const recoverScope = useCallback(async (scopeId: number, fromStatus: string) => {
|
|
75
|
+
try {
|
|
76
|
+
const res = await fetch(`/api/orbital/dispatch/recover/${scopeId}`, {
|
|
77
|
+
method: 'POST',
|
|
78
|
+
headers: { 'Content-Type': 'application/json' },
|
|
79
|
+
body: JSON.stringify({ from_status: fromStatus }),
|
|
80
|
+
});
|
|
81
|
+
if (!res.ok) {
|
|
82
|
+
const body = await res.json().catch(() => ({ error: res.statusText }));
|
|
83
|
+
console.error('[Orbital] Failed to recover scope:', body.error);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
removeFromAbandoned(scopeId);
|
|
87
|
+
} catch (err) {
|
|
88
|
+
console.error('[Orbital] Failed to recover scope:', err);
|
|
89
|
+
}
|
|
90
|
+
}, [removeFromAbandoned]);
|
|
91
|
+
|
|
92
|
+
const dismissAbandoned = useCallback(async (scopeId: number) => {
|
|
93
|
+
try {
|
|
94
|
+
const res = await fetch(`/api/orbital/dispatch/dismiss-abandoned/${scopeId}`, {
|
|
95
|
+
method: 'POST',
|
|
96
|
+
headers: { 'Content-Type': 'application/json' },
|
|
97
|
+
});
|
|
98
|
+
if (!res.ok) {
|
|
99
|
+
const body = await res.json().catch(() => ({ error: res.statusText }));
|
|
100
|
+
console.error('[Orbital] Failed to dismiss abandoned scope:', body.error);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
removeFromAbandoned(scopeId);
|
|
104
|
+
} catch (err) {
|
|
105
|
+
console.error('[Orbital] Failed to dismiss abandoned scope:', err);
|
|
106
|
+
}
|
|
107
|
+
}, [removeFromAbandoned]);
|
|
108
|
+
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
mountedRef.current = true;
|
|
111
|
+
fetchActiveScopes();
|
|
112
|
+
return () => { mountedRef.current = false; };
|
|
113
|
+
}, [fetchActiveScopes]);
|
|
114
|
+
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
function onNewEvent(event: OrbitalEvent) {
|
|
117
|
+
if (event.type !== 'DISPATCH' || event.data.resolved != null) return;
|
|
118
|
+
|
|
119
|
+
// Collect scope IDs: single dispatch uses event.scope_id, batch uses data.scope_ids
|
|
120
|
+
const ids: number[] = [];
|
|
121
|
+
if (event.scope_id != null) ids.push(event.scope_id);
|
|
122
|
+
if (Array.isArray(event.data.scope_ids)) {
|
|
123
|
+
for (const id of event.data.scope_ids as number[]) {
|
|
124
|
+
if (!ids.includes(id)) ids.push(id);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (ids.length === 0) return;
|
|
128
|
+
|
|
129
|
+
setActiveScopes((prev) => {
|
|
130
|
+
const toAdd = ids.filter(id => !prev.has(id));
|
|
131
|
+
if (toAdd.length === 0) return prev;
|
|
132
|
+
const next = new Set(prev);
|
|
133
|
+
for (const id of toAdd) next.add(id);
|
|
134
|
+
return next;
|
|
135
|
+
});
|
|
136
|
+
for (const id of ids) removeFromAbandoned(id);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function onDispatchResolved(payload: DispatchResolvedPayload) {
|
|
140
|
+
// Collect all scope IDs: single dispatch + batch scope_ids
|
|
141
|
+
const ids: number[] = [];
|
|
142
|
+
if (payload.scope_id != null) ids.push(payload.scope_id);
|
|
143
|
+
if (Array.isArray(payload.scope_ids)) ids.push(...payload.scope_ids);
|
|
144
|
+
if (ids.length === 0) return;
|
|
145
|
+
|
|
146
|
+
setActiveScopes((prev) => {
|
|
147
|
+
const toRemove = ids.filter(id => prev.has(id));
|
|
148
|
+
if (toRemove.length === 0) return prev;
|
|
149
|
+
const next = new Set(prev);
|
|
150
|
+
for (const id of toRemove) next.delete(id);
|
|
151
|
+
return next;
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
if (payload.outcome === 'abandoned') {
|
|
155
|
+
fetchActiveScopes();
|
|
156
|
+
} else {
|
|
157
|
+
for (const id of ids) removeFromAbandoned(id);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function onScopeUpdated(scope: Scope) {
|
|
162
|
+
if (terminalStatuses.has(scope.status)) {
|
|
163
|
+
const scopeId = scope.id;
|
|
164
|
+
setActiveScopes((prev) => {
|
|
165
|
+
if (!prev.has(scopeId)) return prev;
|
|
166
|
+
const next = new Set(prev);
|
|
167
|
+
next.delete(scopeId);
|
|
168
|
+
return next;
|
|
169
|
+
});
|
|
170
|
+
// Terminal state clears abandoned
|
|
171
|
+
removeFromAbandoned(scopeId);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function onReconnect() {
|
|
176
|
+
fetchActiveScopes();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
socket.on('event:new', onNewEvent);
|
|
180
|
+
socket.on('dispatch:resolved', onDispatchResolved);
|
|
181
|
+
socket.on('scope:updated', onScopeUpdated);
|
|
182
|
+
socket.on('connect', onReconnect);
|
|
183
|
+
|
|
184
|
+
return () => {
|
|
185
|
+
socket.off('event:new', onNewEvent);
|
|
186
|
+
socket.off('dispatch:resolved', onDispatchResolved);
|
|
187
|
+
socket.off('scope:updated', onScopeUpdated);
|
|
188
|
+
socket.off('connect', onReconnect);
|
|
189
|
+
};
|
|
190
|
+
}, [fetchActiveScopes, removeFromAbandoned, terminalStatuses]);
|
|
191
|
+
|
|
192
|
+
return { activeScopes, abandonedScopes, recoverScope, dismissAbandoned };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Consumer hook — use in ScopeCard to check dispatch state */
|
|
196
|
+
export function useActiveDispatches(): ActiveDispatchContextValue {
|
|
197
|
+
return useContext(ActiveDispatchContext);
|
|
198
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { useState, useCallback, useEffect } from 'react';
|
|
2
|
+
import type { Scope } from '@/types';
|
|
3
|
+
import { EFFORT_BUCKETS } from '@/types';
|
|
4
|
+
|
|
5
|
+
// ─── Types ─────────────────────────────────────────────────
|
|
6
|
+
export type SortField = 'id' | 'priority' | 'effort' | 'updated_at' | 'created_at' | 'title';
|
|
7
|
+
export type SortDirection = 'asc' | 'desc';
|
|
8
|
+
|
|
9
|
+
export interface BoardSettings {
|
|
10
|
+
sortField: SortField;
|
|
11
|
+
sortDirection: SortDirection;
|
|
12
|
+
collapsed: Set<string>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// ─── Constants ─────────────────────────────────────────────
|
|
16
|
+
const SORT_KEY = 'cc-board-sort';
|
|
17
|
+
const COLLAPSE_KEY = 'cc-board-collapsed';
|
|
18
|
+
|
|
19
|
+
const DEFAULT_SORT_DIRECTIONS: Record<SortField, SortDirection> = {
|
|
20
|
+
id: 'asc',
|
|
21
|
+
priority: 'asc',
|
|
22
|
+
effort: 'asc',
|
|
23
|
+
updated_at: 'desc',
|
|
24
|
+
created_at: 'desc',
|
|
25
|
+
title: 'asc',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const PRIORITY_ORDER: Record<string, number> = {
|
|
29
|
+
critical: 0,
|
|
30
|
+
high: 1,
|
|
31
|
+
medium: 2,
|
|
32
|
+
low: 3,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function effortRank(raw: string | null): number {
|
|
36
|
+
if (!raw) return Infinity;
|
|
37
|
+
const idx = EFFORT_BUCKETS.indexOf(raw as typeof EFFORT_BUCKETS[number]);
|
|
38
|
+
return idx >= 0 ? idx : Infinity;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ─── localStorage helpers ──────────────────────────────────
|
|
42
|
+
function readSort(): { field: SortField; direction: SortDirection } {
|
|
43
|
+
try {
|
|
44
|
+
const raw = localStorage.getItem(SORT_KEY);
|
|
45
|
+
if (raw) {
|
|
46
|
+
const parsed = JSON.parse(raw) as { field: string; direction: string };
|
|
47
|
+
if (parsed.field in DEFAULT_SORT_DIRECTIONS) {
|
|
48
|
+
return {
|
|
49
|
+
field: parsed.field as SortField,
|
|
50
|
+
direction: parsed.direction === 'desc' ? 'desc' : 'asc',
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
} catch { /* use defaults */ }
|
|
55
|
+
return { field: 'id', direction: 'asc' };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function readCollapsed(): Set<string> {
|
|
59
|
+
try {
|
|
60
|
+
const raw = localStorage.getItem(COLLAPSE_KEY);
|
|
61
|
+
if (raw) {
|
|
62
|
+
const arr = JSON.parse(raw) as string[];
|
|
63
|
+
if (Array.isArray(arr)) return new Set(arr);
|
|
64
|
+
}
|
|
65
|
+
} catch { /* use defaults */ }
|
|
66
|
+
return new Set();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function persistSort(field: SortField, direction: SortDirection) {
|
|
70
|
+
try { localStorage.setItem(SORT_KEY, JSON.stringify({ field, direction })); } catch { /* noop */ }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function persistCollapsed(collapsed: Set<string>) {
|
|
74
|
+
try { localStorage.setItem(COLLAPSE_KEY, JSON.stringify([...collapsed])); } catch { /* noop */ }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ─── Sort comparator ───────────────────────────────────────
|
|
78
|
+
export function sortScopes(scopes: Scope[], field: SortField, direction: SortDirection): Scope[] {
|
|
79
|
+
const sorted = [...scopes].sort((a, b) => {
|
|
80
|
+
const cmp = compareByField(a, b, field);
|
|
81
|
+
return direction === 'desc' ? -cmp : cmp;
|
|
82
|
+
});
|
|
83
|
+
return sorted;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function compareByField(a: Scope, b: Scope, field: SortField): number {
|
|
87
|
+
switch (field) {
|
|
88
|
+
case 'id':
|
|
89
|
+
return a.id - b.id;
|
|
90
|
+
|
|
91
|
+
case 'priority': {
|
|
92
|
+
const pa = a.priority ? (PRIORITY_ORDER[a.priority] ?? Infinity) : Infinity;
|
|
93
|
+
const pb = b.priority ? (PRIORITY_ORDER[b.priority] ?? Infinity) : Infinity;
|
|
94
|
+
return pa - pb;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
case 'effort':
|
|
98
|
+
return effortRank(a.effort_estimate) - effortRank(b.effort_estimate);
|
|
99
|
+
|
|
100
|
+
case 'updated_at': {
|
|
101
|
+
const ua = a.updated_at ? new Date(a.updated_at).getTime() : 0;
|
|
102
|
+
const ub = b.updated_at ? new Date(b.updated_at).getTime() : 0;
|
|
103
|
+
return ua - ub;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
case 'created_at': {
|
|
107
|
+
const ca = a.created_at ? new Date(a.created_at).getTime() : 0;
|
|
108
|
+
const cb = b.created_at ? new Date(b.created_at).getTime() : 0;
|
|
109
|
+
return ca - cb;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
case 'title':
|
|
113
|
+
return a.title.localeCompare(b.title);
|
|
114
|
+
|
|
115
|
+
default:
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ─── Hook ──────────────────────────────────────────────────
|
|
121
|
+
export function useBoardSettings() {
|
|
122
|
+
const [sortField, setSortField] = useState<SortField>(() => readSort().field);
|
|
123
|
+
const [sortDirection, setSortDirection] = useState<SortDirection>(() => readSort().direction);
|
|
124
|
+
const [collapsed, setCollapsed] = useState<Set<string>>(readCollapsed);
|
|
125
|
+
|
|
126
|
+
// Cross-tab sync
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
function onStorage(e: StorageEvent) {
|
|
129
|
+
if (e.key === SORT_KEY) {
|
|
130
|
+
const s = readSort();
|
|
131
|
+
setSortField(s.field);
|
|
132
|
+
setSortDirection(s.direction);
|
|
133
|
+
}
|
|
134
|
+
if (e.key === COLLAPSE_KEY) {
|
|
135
|
+
setCollapsed(readCollapsed());
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
window.addEventListener('storage', onStorage);
|
|
139
|
+
return () => window.removeEventListener('storage', onStorage);
|
|
140
|
+
}, []);
|
|
141
|
+
|
|
142
|
+
const setSort = useCallback((field: SortField) => {
|
|
143
|
+
setSortField((prevField) => {
|
|
144
|
+
setSortDirection((prevDir) => {
|
|
145
|
+
// Same field → toggle direction; different field → default direction
|
|
146
|
+
const nextDir = prevField === field
|
|
147
|
+
? (prevDir === 'asc' ? 'desc' : 'asc')
|
|
148
|
+
: DEFAULT_SORT_DIRECTIONS[field];
|
|
149
|
+
persistSort(field, nextDir);
|
|
150
|
+
return nextDir;
|
|
151
|
+
});
|
|
152
|
+
return field;
|
|
153
|
+
});
|
|
154
|
+
}, []);
|
|
155
|
+
|
|
156
|
+
const toggleCollapse = useCallback((columnId: string) => {
|
|
157
|
+
setCollapsed((prev) => {
|
|
158
|
+
const next = new Set(prev);
|
|
159
|
+
if (next.has(columnId)) {
|
|
160
|
+
next.delete(columnId);
|
|
161
|
+
} else {
|
|
162
|
+
next.add(columnId);
|
|
163
|
+
}
|
|
164
|
+
persistCollapsed(next);
|
|
165
|
+
return next;
|
|
166
|
+
});
|
|
167
|
+
}, []);
|
|
168
|
+
|
|
169
|
+
return { sortField, sortDirection, setSort, collapsed, toggleCollapse } as const;
|
|
170
|
+
}
|