corporateai 0.0.1
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/.dockerignore +10 -0
- package/.env.example +3 -0
- package/.github/workflows/publish-cli.yml +49 -0
- package/.mailmap +1 -0
- package/AGENTS.md +148 -0
- package/CONTRIBUTING.md +75 -0
- package/Dockerfile +59 -0
- package/Dockerfile.onboard-smoke +42 -0
- package/LICENSE +21 -0
- package/README.md +93 -0
- package/cli/esbuild.config.mjs +11 -0
- package/cli/package.json +24 -0
- package/cli/scripts/build-cli.mjs +5 -0
- package/cli/src/index.ts +27 -0
- package/docker-compose.quickstart.yml +18 -0
- package/docker-compose.untrusted-review.yml +33 -0
- package/docker-compose.yml +38 -0
- package/package.json +56 -0
- package/patches/embedded-postgres@18.1.0-beta.16.patch +0 -0
- package/pnpm-workspace.yaml +4 -0
- package/releases/.gitkeep +0 -0
- package/releases/v0.0.1.md +36 -0
- package/releases/v0.2.7.md +15 -0
- package/releases/v0.3.0.md +54 -0
- package/releases/v0.3.1.md +55 -0
- package/releases/v2026.318.0.md +66 -0
- package/releases/v2026.325.0.md +78 -0
- package/report/2026-03-13-08-46-token-optimization-implementation.md +48 -0
- package/scripts/backup-db.sh +17 -0
- package/scripts/build-npm.sh +80 -0
- package/scripts/check-forbidden-tokens.mjs +115 -0
- package/scripts/clean-onboard-git.sh +14 -0
- package/scripts/clean-onboard-npm.sh +13 -0
- package/scripts/clean-onboard-ref.sh +86 -0
- package/scripts/create-github-release.sh +99 -0
- package/scripts/dev-runner-paths.mjs +38 -0
- package/scripts/dev-runner.mjs +606 -0
- package/scripts/docker-onboard-smoke.sh +306 -0
- package/scripts/ensure-plugin-build-deps.mjs +47 -0
- package/scripts/generate-company-assets.ts +365 -0
- package/scripts/generate-npm-package-json.mjs +113 -0
- package/scripts/generate-org-chart-images.ts +694 -0
- package/scripts/generate-org-chart-satori-comparison.ts +225 -0
- package/scripts/generate-ui-package-json.mjs +31 -0
- package/scripts/kill-dev.sh +71 -0
- package/scripts/migrate-inline-env-secrets.ts +126 -0
- package/scripts/prepare-server-ui-dist.sh +22 -0
- package/scripts/provision-worktree.sh +333 -0
- package/scripts/release-lib.sh +306 -0
- package/scripts/release-package-map.mjs +169 -0
- package/scripts/release.sh +312 -0
- package/scripts/rollback-latest.sh +111 -0
- package/scripts/smoke/openclaw-docker-ui.sh +329 -0
- package/scripts/smoke/openclaw-gateway-e2e.sh +954 -0
- package/scripts/smoke/openclaw-join.sh +295 -0
- package/scripts/smoke/openclaw-sse-standalone.sh +146 -0
- package/scripts/workspace-compat.mjs +60 -0
- package/server/CHANGELOG.md +130 -0
- package/server/package.json +96 -0
- package/server/scripts/copy-onboarding-assets.mjs +10 -0
- package/server/scripts/dev-watch.ts +33 -0
- package/server/src/__tests__/activity-routes.test.ts +70 -0
- package/server/src/__tests__/adapter-models.test.ts +105 -0
- package/server/src/__tests__/adapter-session-codecs.test.ts +194 -0
- package/server/src/__tests__/agent-auth-jwt.test.ts +79 -0
- package/server/src/__tests__/agent-instructions-routes.test.ts +318 -0
- package/server/src/__tests__/agent-instructions-service.test.ts +361 -0
- package/server/src/__tests__/agent-permissions-routes.test.ts +275 -0
- package/server/src/__tests__/agent-shortname-collision.test.ts +69 -0
- package/server/src/__tests__/agent-skill-contract.test.ts +50 -0
- package/server/src/__tests__/agent-skills-routes.test.ts +462 -0
- package/server/src/__tests__/app-hmr-port.test.ts +19 -0
- package/server/src/__tests__/approval-routes-idempotency.test.ts +110 -0
- package/server/src/__tests__/approvals-service.test.ts +107 -0
- package/server/src/__tests__/assets.test.ts +250 -0
- package/server/src/__tests__/attachment-types.test.ts +97 -0
- package/server/src/__tests__/board-mutation-guard.test.ts +105 -0
- package/server/src/__tests__/budgets-service.test.ts +311 -0
- package/server/src/__tests__/claude-local-adapter-environment.test.ts +92 -0
- package/server/src/__tests__/claude-local-adapter.test.ts +31 -0
- package/server/src/__tests__/claude-local-skill-sync.test.ts +111 -0
- package/server/src/__tests__/cli-auth-routes.test.ts +230 -0
- package/server/src/__tests__/codex-local-adapter-environment.test.ts +143 -0
- package/server/src/__tests__/codex-local-adapter.test.ts +253 -0
- package/server/src/__tests__/codex-local-execute.test.ts +391 -0
- package/server/src/__tests__/codex-local-skill-injection.test.ts +175 -0
- package/server/src/__tests__/codex-local-skill-sync.test.ts +123 -0
- package/server/src/__tests__/companies-route-path-guard.test.ts +56 -0
- package/server/src/__tests__/company-branding-route.test.ts +196 -0
- package/server/src/__tests__/company-portability-routes.test.ts +175 -0
- package/server/src/__tests__/company-portability.test.ts +2186 -0
- package/server/src/__tests__/company-skills-routes.test.ts +113 -0
- package/server/src/__tests__/company-skills.test.ts +229 -0
- package/server/src/__tests__/costs-service.test.ts +226 -0
- package/server/src/__tests__/cursor-local-adapter-environment.test.ts +196 -0
- package/server/src/__tests__/cursor-local-adapter.test.ts +406 -0
- package/server/src/__tests__/cursor-local-execute.test.ts +263 -0
- package/server/src/__tests__/cursor-local-skill-injection.test.ts +104 -0
- package/server/src/__tests__/cursor-local-skill-sync.test.ts +145 -0
- package/server/src/__tests__/dev-runner-paths.test.ts +25 -0
- package/server/src/__tests__/dev-server-status.test.ts +66 -0
- package/server/src/__tests__/dev-watch-ignore.test.ts +42 -0
- package/server/src/__tests__/documents.test.ts +29 -0
- package/server/src/__tests__/error-handler.test.ts +53 -0
- package/server/src/__tests__/execution-workspace-policy.test.ts +170 -0
- package/server/src/__tests__/forbidden-tokens.test.ts +77 -0
- package/server/src/__tests__/gemini-local-adapter-environment.test.ts +135 -0
- package/server/src/__tests__/gemini-local-adapter.test.ts +190 -0
- package/server/src/__tests__/gemini-local-execute.test.ts +172 -0
- package/server/src/__tests__/gemini-local-skill-sync.test.ts +90 -0
- package/server/src/__tests__/health.test.ts +16 -0
- package/server/src/__tests__/heartbeat-process-recovery.test.ts +256 -0
- package/server/src/__tests__/heartbeat-run-summary.test.ts +33 -0
- package/server/src/__tests__/heartbeat-workspace-session.test.ts +334 -0
- package/server/src/__tests__/helpers/embedded-postgres.ts +7 -0
- package/server/src/__tests__/hire-hook.test.ts +181 -0
- package/server/src/__tests__/instance-settings-routes.test.ts +156 -0
- package/server/src/__tests__/invite-accept-gateway-defaults.test.ts +119 -0
- package/server/src/__tests__/invite-accept-replay.test.ts +92 -0
- package/server/src/__tests__/invite-expiry.test.ts +10 -0
- package/server/src/__tests__/invite-join-grants.test.ts +57 -0
- package/server/src/__tests__/invite-join-manager.test.ts +33 -0
- package/server/src/__tests__/invite-onboarding-text.test.ts +116 -0
- package/server/src/__tests__/issue-comment-reopen-routes.test.ts +146 -0
- package/server/src/__tests__/issue-goal-fallback.test.ts +99 -0
- package/server/src/__tests__/issues-checkout-wakeup.test.ts +48 -0
- package/server/src/__tests__/issues-goal-context-routes.test.ts +187 -0
- package/server/src/__tests__/issues-service.test.ts +317 -0
- package/server/src/__tests__/issues-user-context.test.ts +113 -0
- package/server/src/__tests__/log-redaction.test.ts +74 -0
- package/server/src/__tests__/monthly-spend-service.test.ts +90 -0
- package/server/src/__tests__/normalize-agent-mention-token.test.ts +41 -0
- package/server/src/__tests__/openclaw-gateway-adapter.test.ts +626 -0
- package/server/src/__tests__/openclaw-invite-prompt-route.test.ts +192 -0
- package/server/src/__tests__/opencode-local-adapter-environment.test.ts +97 -0
- package/server/src/__tests__/opencode-local-adapter.test.ts +226 -0
- package/server/src/__tests__/opencode-local-skill-sync.test.ts +91 -0
- package/server/src/__tests__/paperclip-env.test.ts +58 -0
- package/server/src/__tests__/paperclip-skill-utils.test.ts +63 -0
- package/server/src/__tests__/pi-local-adapter-environment.test.ts +102 -0
- package/server/src/__tests__/pi-local-skill-sync.test.ts +95 -0
- package/server/src/__tests__/plugin-dev-watcher.test.ts +68 -0
- package/server/src/__tests__/plugin-worker-manager.test.ts +43 -0
- package/server/src/__tests__/private-hostname-guard.test.ts +56 -0
- package/server/src/__tests__/project-shortname-resolution.test.ts +45 -0
- package/server/src/__tests__/quota-windows-service.test.ts +56 -0
- package/server/src/__tests__/quota-windows.test.ts +1109 -0
- package/server/src/__tests__/redaction.test.ts +66 -0
- package/server/src/__tests__/routines-e2e.test.ts +276 -0
- package/server/src/__tests__/routines-routes.test.ts +271 -0
- package/server/src/__tests__/routines-service.test.ts +424 -0
- package/server/src/__tests__/storage-local-provider.test.ts +78 -0
- package/server/src/__tests__/ui-branding.test.ts +82 -0
- package/server/src/__tests__/work-products.test.ts +95 -0
- package/server/src/__tests__/workspace-runtime.test.ts +1131 -0
- package/server/src/__tests__/worktree-config.test.ts +426 -0
- package/server/src/adapters/codex-models.ts +105 -0
- package/server/src/adapters/cursor-models.ts +171 -0
- package/server/src/adapters/http/execute.ts +42 -0
- package/server/src/adapters/http/index.ts +21 -0
- package/server/src/adapters/http/test.ts +116 -0
- package/server/src/adapters/index.ts +18 -0
- package/server/src/adapters/process/execute.ts +77 -0
- package/server/src/adapters/process/index.ts +24 -0
- package/server/src/adapters/process/test.ts +89 -0
- package/server/src/adapters/registry.ts +225 -0
- package/server/src/adapters/server-utils-compat.ts +57 -0
- package/server/src/adapters/types.ts +30 -0
- package/server/src/adapters/utils.ts +48 -0
- package/server/src/agent-auth-jwt.ts +141 -0
- package/server/src/app.ts +321 -0
- package/server/src/attachment-types.ts +74 -0
- package/server/src/auth/better-auth.ts +148 -0
- package/server/src/board-claim.ts +150 -0
- package/server/src/config-file.ts +17 -0
- package/server/src/config.ts +260 -0
- package/server/src/dev-server-status.ts +103 -0
- package/server/src/dev-watch-ignore.ts +36 -0
- package/server/src/errors.ts +34 -0
- package/server/src/home-paths.ts +95 -0
- package/server/src/index.ts +799 -0
- package/server/src/log-redaction.ts +146 -0
- package/server/src/middleware/auth.ts +178 -0
- package/server/src/middleware/board-mutation-guard.ts +66 -0
- package/server/src/middleware/error-handler.ts +71 -0
- package/server/src/middleware/index.ts +3 -0
- package/server/src/middleware/logger.ts +90 -0
- package/server/src/middleware/private-hostname-guard.ts +92 -0
- package/server/src/middleware/validate.ts +9 -0
- package/server/src/onboarding-assets/ceo/AGENTS.md +54 -0
- package/server/src/onboarding-assets/ceo/HEARTBEAT.md +72 -0
- package/server/src/onboarding-assets/ceo/SOUL.md +33 -0
- package/server/src/onboarding-assets/ceo/TOOLS.md +3 -0
- package/server/src/onboarding-assets/default/AGENTS.md +3 -0
- package/server/src/paths.ts +34 -0
- package/server/src/realtime/live-events-ws.ts +274 -0
- package/server/src/redaction.ts +59 -0
- package/server/src/routes/access.ts +2888 -0
- package/server/src/routes/activity.ts +89 -0
- package/server/src/routes/agents.ts +2313 -0
- package/server/src/routes/approvals.ts +346 -0
- package/server/src/routes/assets.ts +341 -0
- package/server/src/routes/authz.ts +52 -0
- package/server/src/routes/companies.ts +343 -0
- package/server/src/routes/company-skills.ts +300 -0
- package/server/src/routes/costs.ts +335 -0
- package/server/src/routes/dashboard.ts +19 -0
- package/server/src/routes/execution-workspaces.ts +182 -0
- package/server/src/routes/goals.ts +107 -0
- package/server/src/routes/health.ts +94 -0
- package/server/src/routes/index.ts +17 -0
- package/server/src/routes/instance-settings.ts +95 -0
- package/server/src/routes/issues-checkout-wakeup.ts +14 -0
- package/server/src/routes/issues.ts +1680 -0
- package/server/src/routes/llms.ts +86 -0
- package/server/src/routes/org-chart-svg.ts +777 -0
- package/server/src/routes/plugin-ui-static.ts +497 -0
- package/server/src/routes/plugins.ts +2220 -0
- package/server/src/routes/projects.ts +295 -0
- package/server/src/routes/routines.ts +300 -0
- package/server/src/routes/secrets.ts +166 -0
- package/server/src/routes/sidebar-badges.ts +52 -0
- package/server/src/secrets/external-stub-providers.ts +32 -0
- package/server/src/secrets/local-encrypted-provider.ts +135 -0
- package/server/src/secrets/provider-registry.ts +31 -0
- package/server/src/secrets/types.ts +23 -0
- package/server/src/services/access.ts +381 -0
- package/server/src/services/activity-log.ts +95 -0
- package/server/src/services/activity.ts +164 -0
- package/server/src/services/agent-instructions.ts +735 -0
- package/server/src/services/agent-permissions.ts +27 -0
- package/server/src/services/agents.ts +694 -0
- package/server/src/services/approvals.ts +273 -0
- package/server/src/services/assets.ts +23 -0
- package/server/src/services/board-auth.ts +355 -0
- package/server/src/services/budgets.ts +959 -0
- package/server/src/services/companies.ts +313 -0
- package/server/src/services/company-export-readme.ts +173 -0
- package/server/src/services/company-portability.ts +4263 -0
- package/server/src/services/company-skills.ts +2356 -0
- package/server/src/services/costs.ts +365 -0
- package/server/src/services/cron.ts +373 -0
- package/server/src/services/dashboard.ts +110 -0
- package/server/src/services/default-agent-instructions.ts +27 -0
- package/server/src/services/documents.ts +434 -0
- package/server/src/services/execution-workspace-policy.ts +210 -0
- package/server/src/services/execution-workspaces.ts +100 -0
- package/server/src/services/finance.ts +135 -0
- package/server/src/services/goals.ts +81 -0
- package/server/src/services/heartbeat-run-summary.ts +35 -0
- package/server/src/services/heartbeat.ts +3863 -0
- package/server/src/services/hire-hook.ts +114 -0
- package/server/src/services/index.ts +32 -0
- package/server/src/services/instance-settings.ts +138 -0
- package/server/src/services/issue-approvals.ts +175 -0
- package/server/src/services/issue-assignment-wakeup.ts +48 -0
- package/server/src/services/issue-goal-fallback.ts +56 -0
- package/server/src/services/issues.ts +1828 -0
- package/server/src/services/live-events.ts +55 -0
- package/server/src/services/plugin-capability-validator.ts +450 -0
- package/server/src/services/plugin-config-validator.ts +55 -0
- package/server/src/services/plugin-dev-watcher.ts +339 -0
- package/server/src/services/plugin-event-bus.ts +413 -0
- package/server/src/services/plugin-host-service-cleanup.ts +59 -0
- package/server/src/services/plugin-host-services.ts +1132 -0
- package/server/src/services/plugin-job-coordinator.ts +261 -0
- package/server/src/services/plugin-job-scheduler.ts +753 -0
- package/server/src/services/plugin-job-store.ts +466 -0
- package/server/src/services/plugin-lifecycle.ts +822 -0
- package/server/src/services/plugin-loader.ts +1955 -0
- package/server/src/services/plugin-log-retention.ts +87 -0
- package/server/src/services/plugin-manifest-validator.ts +164 -0
- package/server/src/services/plugin-registry.ts +683 -0
- package/server/src/services/plugin-runtime-sandbox.ts +222 -0
- package/server/src/services/plugin-secrets-handler.ts +355 -0
- package/server/src/services/plugin-state-store.ts +238 -0
- package/server/src/services/plugin-stream-bus.ts +81 -0
- package/server/src/services/plugin-tool-dispatcher.ts +449 -0
- package/server/src/services/plugin-tool-registry.ts +450 -0
- package/server/src/services/plugin-worker-manager.ts +1343 -0
- package/server/src/services/projects.ts +860 -0
- package/server/src/services/quota-windows.ts +65 -0
- package/server/src/services/routines.ts +1269 -0
- package/server/src/services/run-log-store.ts +156 -0
- package/server/src/services/secrets.ts +370 -0
- package/server/src/services/sidebar-badges.ts +56 -0
- package/server/src/services/work-products.ts +124 -0
- package/server/src/services/workspace-operation-log-store.ts +156 -0
- package/server/src/services/workspace-operations.ts +262 -0
- package/server/src/services/workspace-runtime.ts +1565 -0
- package/server/src/startup-banner.ts +176 -0
- package/server/src/storage/index.ts +35 -0
- package/server/src/storage/local-disk-provider.ts +89 -0
- package/server/src/storage/provider-registry.ts +18 -0
- package/server/src/storage/s3-provider.ts +153 -0
- package/server/src/storage/service.ts +131 -0
- package/server/src/storage/types.ts +63 -0
- package/server/src/ui-branding.ts +217 -0
- package/server/src/version.ts +10 -0
- package/server/src/worktree-config.ts +468 -0
- package/server/tsconfig.json +9 -0
- package/server/vitest.config.ts +7 -0
- package/skills/paperclip/SKILL.md +365 -0
- package/skills/paperclip/references/api-reference.md +647 -0
- package/skills/paperclip/references/company-skills.md +193 -0
- package/skills/paperclip-create-agent/SKILL.md +142 -0
- package/skills/paperclip-create-agent/references/api-reference.md +105 -0
- package/skills/paperclip-create-plugin/SKILL.md +102 -0
- package/skills/para-memory-files/SKILL.md +104 -0
- package/skills/para-memory-files/references/schemas.md +35 -0
- package/tests/e2e/onboarding.spec.ts +142 -0
- package/tests/e2e/playwright.config.ts +35 -0
- package/tests/release-smoke/docker-auth-onboarding.spec.ts +141 -0
- package/tests/release-smoke/playwright.config.ts +28 -0
- package/tsconfig.base.json +18 -0
- package/tsconfig.json +18 -0
- package/ui/README.md +12 -0
- package/ui/components.json +21 -0
- package/ui/index.html +47 -0
- package/ui/package.json +73 -0
- package/ui/public/android-chrome-192x192.png +0 -0
- package/ui/public/android-chrome-512x512.png +0 -0
- package/ui/public/apple-touch-icon.png +0 -0
- package/ui/public/brands/opencode-logo-dark-square.svg +18 -0
- package/ui/public/brands/opencode-logo-light-square.svg +18 -0
- package/ui/public/favicon-16x16.png +0 -0
- package/ui/public/favicon-32x32.png +0 -0
- package/ui/public/favicon.ico +0 -0
- package/ui/public/favicon.svg +9 -0
- package/ui/public/site.webmanifest +30 -0
- package/ui/public/sprites/1-D-1.png +0 -0
- package/ui/public/sprites/1-D-2.png +0 -0
- package/ui/public/sprites/1-D-3.png +0 -0
- package/ui/public/sprites/1-L-1.png +0 -0
- package/ui/public/sprites/1-R-1.png +0 -0
- package/ui/public/sprites/10-D-1.png +0 -0
- package/ui/public/sprites/10-D-2.png +0 -0
- package/ui/public/sprites/10-D-3.png +0 -0
- package/ui/public/sprites/10-L-1.png +0 -0
- package/ui/public/sprites/10-R-1.png +0 -0
- package/ui/public/sprites/11-D-1.png +0 -0
- package/ui/public/sprites/11-D-2.png +0 -0
- package/ui/public/sprites/11-D-3.png +0 -0
- package/ui/public/sprites/11-L-1.png +0 -0
- package/ui/public/sprites/11-R-1.png +0 -0
- package/ui/public/sprites/12-D-1.png +0 -0
- package/ui/public/sprites/12-D-2.png +0 -0
- package/ui/public/sprites/12-D-3.png +0 -0
- package/ui/public/sprites/12-L-1.png +0 -0
- package/ui/public/sprites/12-R-1.png +0 -0
- package/ui/public/sprites/13-D-1.png +0 -0
- package/ui/public/sprites/13-D-2.png +0 -0
- package/ui/public/sprites/13-D-3.png +0 -0
- package/ui/public/sprites/13-L-1.png +0 -0
- package/ui/public/sprites/13-R-1.png +0 -0
- package/ui/public/sprites/14-D-1.png +0 -0
- package/ui/public/sprites/14-D-2.png +0 -0
- package/ui/public/sprites/14-D-3.png +0 -0
- package/ui/public/sprites/14-L-1.png +0 -0
- package/ui/public/sprites/14-R-1.png +0 -0
- package/ui/public/sprites/2-D-1.png +0 -0
- package/ui/public/sprites/2-D-2.png +0 -0
- package/ui/public/sprites/2-D-3.png +0 -0
- package/ui/public/sprites/2-L-1.png +0 -0
- package/ui/public/sprites/2-R-1.png +0 -0
- package/ui/public/sprites/3-D-1.png +0 -0
- package/ui/public/sprites/3-D-2.png +0 -0
- package/ui/public/sprites/3-D-3.png +0 -0
- package/ui/public/sprites/3-L-1.png +0 -0
- package/ui/public/sprites/3-R-1.png +0 -0
- package/ui/public/sprites/4-D-1.png +0 -0
- package/ui/public/sprites/4-D-2.png +0 -0
- package/ui/public/sprites/4-D-3.png +0 -0
- package/ui/public/sprites/4-L-1.png +0 -0
- package/ui/public/sprites/4-R-1.png +0 -0
- package/ui/public/sprites/5-D-1.png +0 -0
- package/ui/public/sprites/5-D-2.png +0 -0
- package/ui/public/sprites/5-D-3.png +0 -0
- package/ui/public/sprites/5-L-1.png +0 -0
- package/ui/public/sprites/5-R-1.png +0 -0
- package/ui/public/sprites/6-D-1.png +0 -0
- package/ui/public/sprites/6-D-2.png +0 -0
- package/ui/public/sprites/6-D-3.png +0 -0
- package/ui/public/sprites/6-L-1.png +0 -0
- package/ui/public/sprites/6-R-1.png +0 -0
- package/ui/public/sprites/7-D-1.png +0 -0
- package/ui/public/sprites/7-D-2.png +0 -0
- package/ui/public/sprites/7-D-3.png +0 -0
- package/ui/public/sprites/7-L-1.png +0 -0
- package/ui/public/sprites/7-R-1.png +0 -0
- package/ui/public/sprites/8-D-1.png +0 -0
- package/ui/public/sprites/8-D-2.png +0 -0
- package/ui/public/sprites/8-D-3.png +0 -0
- package/ui/public/sprites/8-L-1.png +0 -0
- package/ui/public/sprites/8-R-1.png +0 -0
- package/ui/public/sprites/9-D-1.png +0 -0
- package/ui/public/sprites/9-D-2.png +0 -0
- package/ui/public/sprites/9-D-3.png +0 -0
- package/ui/public/sprites/9-L-1.png +0 -0
- package/ui/public/sprites/9-R-1.png +0 -0
- package/ui/public/sprites/ceo-lobster.png +0 -0
- package/ui/public/sw.js +42 -0
- package/ui/public/worktree-favicon-16x16.png +0 -0
- package/ui/public/worktree-favicon-32x32.png +0 -0
- package/ui/public/worktree-favicon.ico +0 -0
- package/ui/public/worktree-favicon.svg +9 -0
- package/ui/src/App.tsx +354 -0
- package/ui/src/adapters/claude-local/config-fields.tsx +138 -0
- package/ui/src/adapters/claude-local/index.ts +13 -0
- package/ui/src/adapters/codex-local/config-fields.tsx +104 -0
- package/ui/src/adapters/codex-local/index.ts +13 -0
- package/ui/src/adapters/cursor/config-fields.tsx +49 -0
- package/ui/src/adapters/cursor/index.ts +13 -0
- package/ui/src/adapters/gemini-local/config-fields.tsx +51 -0
- package/ui/src/adapters/gemini-local/index.ts +13 -0
- package/ui/src/adapters/http/build-config.ts +9 -0
- package/ui/src/adapters/http/config-fields.tsx +38 -0
- package/ui/src/adapters/http/index.ts +12 -0
- package/ui/src/adapters/http/parse-stdout.ts +5 -0
- package/ui/src/adapters/index.ts +9 -0
- package/ui/src/adapters/local-workspace-runtime-fields.tsx +5 -0
- package/ui/src/adapters/openclaw-gateway/config-fields.tsx +237 -0
- package/ui/src/adapters/openclaw-gateway/index.ts +13 -0
- package/ui/src/adapters/opencode-local/config-fields.tsx +72 -0
- package/ui/src/adapters/opencode-local/index.ts +13 -0
- package/ui/src/adapters/pi-local/config-fields.tsx +49 -0
- package/ui/src/adapters/pi-local/index.ts +13 -0
- package/ui/src/adapters/process/build-config.ts +18 -0
- package/ui/src/adapters/process/config-fields.tsx +77 -0
- package/ui/src/adapters/process/index.ts +12 -0
- package/ui/src/adapters/process/parse-stdout.ts +5 -0
- package/ui/src/adapters/registry.ts +34 -0
- package/ui/src/adapters/runtime-json-fields.tsx +122 -0
- package/ui/src/adapters/transcript.test.ts +30 -0
- package/ui/src/adapters/transcript.ts +62 -0
- package/ui/src/adapters/types.ts +34 -0
- package/ui/src/api/access.ts +160 -0
- package/ui/src/api/activity.ts +37 -0
- package/ui/src/api/agents.ts +194 -0
- package/ui/src/api/approvals.ts +25 -0
- package/ui/src/api/assets.ts +30 -0
- package/ui/src/api/auth.ts +74 -0
- package/ui/src/api/budgets.ts +21 -0
- package/ui/src/api/client.ts +50 -0
- package/ui/src/api/companies.ts +59 -0
- package/ui/src/api/companySkills.ts +55 -0
- package/ui/src/api/costs.ts +60 -0
- package/ui/src/api/dashboard.ts +7 -0
- package/ui/src/api/execution-workspaces.ts +27 -0
- package/ui/src/api/goals.ts +12 -0
- package/ui/src/api/health.ts +41 -0
- package/ui/src/api/heartbeats.ts +62 -0
- package/ui/src/api/index.ts +18 -0
- package/ui/src/api/instanceSettings.ts +19 -0
- package/ui/src/api/issues.ts +115 -0
- package/ui/src/api/plugins.ts +424 -0
- package/ui/src/api/projects.ts +34 -0
- package/ui/src/api/routines.ts +59 -0
- package/ui/src/api/secrets.ts +26 -0
- package/ui/src/api/sidebarBadges.ts +7 -0
- package/ui/src/components/AccountingModelCard.tsx +69 -0
- package/ui/src/components/ActiveAgentsPanel.tsx +157 -0
- package/ui/src/components/ActivityCharts.tsx +264 -0
- package/ui/src/components/ActivityRow.tsx +147 -0
- package/ui/src/components/AgentActionButtons.tsx +51 -0
- package/ui/src/components/AgentConfigForm.tsx +1468 -0
- package/ui/src/components/AgentIconPicker.tsx +81 -0
- package/ui/src/components/AgentProperties.tsx +107 -0
- package/ui/src/components/ApprovalCard.tsx +107 -0
- package/ui/src/components/ApprovalPayload.tsx +134 -0
- package/ui/src/components/AsciiArtAnimation.tsx +355 -0
- package/ui/src/components/BillerSpendCard.tsx +146 -0
- package/ui/src/components/BreadcrumbBar.tsx +113 -0
- package/ui/src/components/BudgetIncidentCard.tsx +101 -0
- package/ui/src/components/BudgetPolicyCard.tsx +220 -0
- package/ui/src/components/BudgetSidebarMarker.tsx +13 -0
- package/ui/src/components/ClaudeSubscriptionPanel.tsx +141 -0
- package/ui/src/components/CodexSubscriptionPanel.tsx +158 -0
- package/ui/src/components/CommandPalette.tsx +239 -0
- package/ui/src/components/CommentThread.tsx +503 -0
- package/ui/src/components/CompanyPatternIcon.tsx +212 -0
- package/ui/src/components/CompanyRail.tsx +329 -0
- package/ui/src/components/CompanySwitcher.tsx +81 -0
- package/ui/src/components/CopyText.tsx +56 -0
- package/ui/src/components/DevRestartBanner.tsx +89 -0
- package/ui/src/components/EmptyState.tsx +27 -0
- package/ui/src/components/EntityRow.tsx +69 -0
- package/ui/src/components/FilterBar.tsx +39 -0
- package/ui/src/components/FinanceBillerCard.tsx +45 -0
- package/ui/src/components/FinanceKindCard.tsx +44 -0
- package/ui/src/components/FinanceTimelineCard.tsx +72 -0
- package/ui/src/components/GoalProperties.tsx +165 -0
- package/ui/src/components/GoalTree.tsx +118 -0
- package/ui/src/components/Identity.tsx +39 -0
- package/ui/src/components/InlineEditor.tsx +248 -0
- package/ui/src/components/InlineEntitySelector.tsx +206 -0
- package/ui/src/components/InstanceSidebar.tsx +53 -0
- package/ui/src/components/IssueDocumentsSection.tsx +892 -0
- package/ui/src/components/IssueProperties.tsx +621 -0
- package/ui/src/components/IssueRow.tsx +149 -0
- package/ui/src/components/IssueWorkspaceCard.tsx +404 -0
- package/ui/src/components/IssuesList.tsx +889 -0
- package/ui/src/components/JsonSchemaForm.tsx +1048 -0
- package/ui/src/components/KanbanBoard.tsx +275 -0
- package/ui/src/components/Layout.tsx +441 -0
- package/ui/src/components/LiveRunWidget.tsx +160 -0
- package/ui/src/components/MarkdownBody.test.tsx +50 -0
- package/ui/src/components/MarkdownBody.tsx +152 -0
- package/ui/src/components/MarkdownEditor.tsx +622 -0
- package/ui/src/components/MetricCard.tsx +53 -0
- package/ui/src/components/MobileBottomNav.tsx +123 -0
- package/ui/src/components/NewAgentDialog.tsx +223 -0
- package/ui/src/components/NewGoalDialog.tsx +283 -0
- package/ui/src/components/NewIssueDialog.tsx +1473 -0
- package/ui/src/components/NewProjectDialog.tsx +451 -0
- package/ui/src/components/OnboardingWizard.tsx +1392 -0
- package/ui/src/components/OpenCodeLogoIcon.tsx +22 -0
- package/ui/src/components/PackageFileTree.tsx +318 -0
- package/ui/src/components/PageSkeleton.tsx +180 -0
- package/ui/src/components/PageTabBar.tsx +45 -0
- package/ui/src/components/PathInstructionsModal.tsx +143 -0
- package/ui/src/components/PriorityIcon.tsx +77 -0
- package/ui/src/components/ProjectProperties.tsx +1127 -0
- package/ui/src/components/PropertiesPanel.tsx +29 -0
- package/ui/src/components/ProviderQuotaCard.tsx +417 -0
- package/ui/src/components/QuotaBar.tsx +65 -0
- package/ui/src/components/ReportsToPicker.tsx +127 -0
- package/ui/src/components/ScheduleEditor.tsx +344 -0
- package/ui/src/components/ScrollToBottom.tsx +79 -0
- package/ui/src/components/Sidebar.tsx +130 -0
- package/ui/src/components/SidebarAgents.tsx +146 -0
- package/ui/src/components/SidebarNavItem.tsx +92 -0
- package/ui/src/components/SidebarProjects.tsx +234 -0
- package/ui/src/components/SidebarSection.tsx +17 -0
- package/ui/src/components/StatusBadge.tsx +15 -0
- package/ui/src/components/StatusIcon.tsx +71 -0
- package/ui/src/components/SwipeToArchive.tsx +152 -0
- package/ui/src/components/ToastViewport.tsx +99 -0
- package/ui/src/components/WorktreeBanner.tsx +25 -0
- package/ui/src/components/agent-config-defaults.ts +31 -0
- package/ui/src/components/agent-config-primitives.tsx +476 -0
- package/ui/src/components/transcript/RunTranscriptView.test.tsx +84 -0
- package/ui/src/components/transcript/RunTranscriptView.tsx +1015 -0
- package/ui/src/components/transcript/useLiveRunTranscripts.ts +297 -0
- package/ui/src/components/ui/avatar.tsx +107 -0
- package/ui/src/components/ui/badge.tsx +48 -0
- package/ui/src/components/ui/breadcrumb.tsx +109 -0
- package/ui/src/components/ui/button.tsx +64 -0
- package/ui/src/components/ui/card.tsx +92 -0
- package/ui/src/components/ui/checkbox.tsx +32 -0
- package/ui/src/components/ui/collapsible.tsx +33 -0
- package/ui/src/components/ui/command.tsx +194 -0
- package/ui/src/components/ui/dialog.tsx +156 -0
- package/ui/src/components/ui/dropdown-menu.tsx +257 -0
- package/ui/src/components/ui/input.tsx +21 -0
- package/ui/src/components/ui/label.tsx +22 -0
- package/ui/src/components/ui/popover.tsx +88 -0
- package/ui/src/components/ui/scroll-area.tsx +56 -0
- package/ui/src/components/ui/select.tsx +188 -0
- package/ui/src/components/ui/separator.tsx +28 -0
- package/ui/src/components/ui/sheet.tsx +143 -0
- package/ui/src/components/ui/skeleton.tsx +13 -0
- package/ui/src/components/ui/tabs.tsx +89 -0
- package/ui/src/components/ui/textarea.tsx +18 -0
- package/ui/src/components/ui/tooltip.tsx +57 -0
- package/ui/src/components/visual-office/AgentAvatar.tsx +99 -0
- package/ui/src/components/visual-office/OfficeViewExact.tsx +417 -0
- package/ui/src/components/visual-office/i18n.ts +52 -0
- package/ui/src/components/visual-office/office-view/CliUsagePanel.tsx +240 -0
- package/ui/src/components/visual-office/office-view/VirtualPadOverlay.tsx +104 -0
- package/ui/src/components/visual-office/office-view/buildScene-break-room.ts +248 -0
- package/ui/src/components/visual-office/office-view/buildScene-ceo-hallway.ts +345 -0
- package/ui/src/components/visual-office/office-view/buildScene-department-agent.ts +242 -0
- package/ui/src/components/visual-office/office-view/buildScene-departments.ts +360 -0
- package/ui/src/components/visual-office/office-view/buildScene-final-layers.ts +113 -0
- package/ui/src/components/visual-office/office-view/buildScene-types.ts +91 -0
- package/ui/src/components/visual-office/office-view/buildScene.ts +232 -0
- package/ui/src/components/visual-office/office-view/drawing-core.ts +374 -0
- package/ui/src/components/visual-office/office-view/drawing-furniture-a.ts +338 -0
- package/ui/src/components/visual-office/office-view/drawing-furniture-b.ts +241 -0
- package/ui/src/components/visual-office/office-view/model.ts +301 -0
- package/ui/src/components/visual-office/office-view/officeTicker.ts +455 -0
- package/ui/src/components/visual-office/office-view/officeTickerRoomAndDelivery.ts +133 -0
- package/ui/src/components/visual-office/office-view/themes-locale.ts +460 -0
- package/ui/src/components/visual-office/office-view/useCliUsage.ts +37 -0
- package/ui/src/components/visual-office/office-view/useOfficeDeliveryEffects.ts +465 -0
- package/ui/src/components/visual-office/office-view/useOfficePixiRuntime.ts +282 -0
- package/ui/src/components/visual-office/types.ts +123 -0
- package/ui/src/context/BreadcrumbContext.tsx +44 -0
- package/ui/src/context/CompanyContext.tsx +151 -0
- package/ui/src/context/DialogContext.tsx +135 -0
- package/ui/src/context/LiveUpdatesProvider.test.ts +119 -0
- package/ui/src/context/LiveUpdatesProvider.tsx +760 -0
- package/ui/src/context/PanelContext.tsx +73 -0
- package/ui/src/context/SidebarContext.tsx +43 -0
- package/ui/src/context/ThemeContext.tsx +83 -0
- package/ui/src/context/ToastContext.tsx +172 -0
- package/ui/src/fixtures/runTranscriptFixtures.ts +226 -0
- package/ui/src/hooks/useAgentOrder.ts +105 -0
- package/ui/src/hooks/useAutosaveIndicator.ts +72 -0
- package/ui/src/hooks/useCompanyPageMemory.test.ts +90 -0
- package/ui/src/hooks/useCompanyPageMemory.ts +79 -0
- package/ui/src/hooks/useDateRange.ts +120 -0
- package/ui/src/hooks/useInboxBadge.ts +132 -0
- package/ui/src/hooks/useKeyboardShortcuts.ts +40 -0
- package/ui/src/hooks/useProjectOrder.ts +106 -0
- package/ui/src/index.css +770 -0
- package/ui/src/lib/agent-icons.ts +99 -0
- package/ui/src/lib/agent-order.ts +107 -0
- package/ui/src/lib/agent-skills-state.test.ts +90 -0
- package/ui/src/lib/agent-skills-state.ts +41 -0
- package/ui/src/lib/assignees.test.ts +92 -0
- package/ui/src/lib/assignees.ts +82 -0
- package/ui/src/lib/color-contrast.ts +107 -0
- package/ui/src/lib/company-export-selection.test.ts +41 -0
- package/ui/src/lib/company-export-selection.ts +57 -0
- package/ui/src/lib/company-page-memory.ts +65 -0
- package/ui/src/lib/company-portability-sidebar.test.ts +101 -0
- package/ui/src/lib/company-portability-sidebar.ts +62 -0
- package/ui/src/lib/company-routes.ts +88 -0
- package/ui/src/lib/company-selection.test.ts +34 -0
- package/ui/src/lib/company-selection.ts +18 -0
- package/ui/src/lib/groupBy.ts +11 -0
- package/ui/src/lib/inbox.test.ts +404 -0
- package/ui/src/lib/inbox.ts +292 -0
- package/ui/src/lib/instance-settings.test.ts +26 -0
- package/ui/src/lib/instance-settings.ts +25 -0
- package/ui/src/lib/issueDetailBreadcrumb.ts +24 -0
- package/ui/src/lib/legacy-agent-config.test.ts +40 -0
- package/ui/src/lib/legacy-agent-config.ts +17 -0
- package/ui/src/lib/mention-aware-link-node.test.ts +50 -0
- package/ui/src/lib/mention-aware-link-node.ts +67 -0
- package/ui/src/lib/mention-chips.ts +168 -0
- package/ui/src/lib/mention-deletion.test.ts +87 -0
- package/ui/src/lib/mention-deletion.ts +143 -0
- package/ui/src/lib/model-utils.ts +16 -0
- package/ui/src/lib/onboarding-goal.test.ts +22 -0
- package/ui/src/lib/onboarding-goal.ts +18 -0
- package/ui/src/lib/onboarding-launch.test.ts +131 -0
- package/ui/src/lib/onboarding-launch.ts +54 -0
- package/ui/src/lib/onboarding-route.test.ts +80 -0
- package/ui/src/lib/onboarding-route.ts +51 -0
- package/ui/src/lib/portable-files.ts +42 -0
- package/ui/src/lib/project-order.ts +71 -0
- package/ui/src/lib/queryKeys.ts +140 -0
- package/ui/src/lib/recent-assignees.ts +36 -0
- package/ui/src/lib/router.tsx +76 -0
- package/ui/src/lib/routine-trigger-patch.test.ts +72 -0
- package/ui/src/lib/routine-trigger-patch.ts +31 -0
- package/ui/src/lib/status-colors.ts +108 -0
- package/ui/src/lib/timeAgo.ts +31 -0
- package/ui/src/lib/utils.ts +168 -0
- package/ui/src/lib/worktree-branding.ts +65 -0
- package/ui/src/lib/zip.test.ts +289 -0
- package/ui/src/lib/zip.ts +284 -0
- package/ui/src/main.tsx +67 -0
- package/ui/src/pages/Activity.tsx +141 -0
- package/ui/src/pages/AgentDetail.tsx +4053 -0
- package/ui/src/pages/Agents.tsx +415 -0
- package/ui/src/pages/ApprovalDetail.tsx +369 -0
- package/ui/src/pages/Approvals.tsx +132 -0
- package/ui/src/pages/Auth.tsx +180 -0
- package/ui/src/pages/BoardClaim.tsx +125 -0
- package/ui/src/pages/CliAuth.tsx +184 -0
- package/ui/src/pages/Companies.tsx +297 -0
- package/ui/src/pages/CompanyExport.tsx +1019 -0
- package/ui/src/pages/CompanyImport.tsx +1355 -0
- package/ui/src/pages/CompanySettings.tsx +661 -0
- package/ui/src/pages/CompanySkills.tsx +1171 -0
- package/ui/src/pages/Costs.tsx +1103 -0
- package/ui/src/pages/Dashboard.tsx +388 -0
- package/ui/src/pages/DesignGuide.tsx +1330 -0
- package/ui/src/pages/ExecutionWorkspaceDetail.tsx +82 -0
- package/ui/src/pages/GoalDetail.tsx +197 -0
- package/ui/src/pages/Goals.tsx +63 -0
- package/ui/src/pages/Inbox.tsx +1291 -0
- package/ui/src/pages/InstanceExperimentalSettings.tsx +139 -0
- package/ui/src/pages/InstanceGeneralSettings.tsx +104 -0
- package/ui/src/pages/InstanceSettings.tsx +284 -0
- package/ui/src/pages/InviteLanding.tsx +320 -0
- package/ui/src/pages/IssueDetail.tsx +1201 -0
- package/ui/src/pages/Issues.tsx +116 -0
- package/ui/src/pages/MyIssues.tsx +72 -0
- package/ui/src/pages/NewAgent.tsx +353 -0
- package/ui/src/pages/NotFound.tsx +66 -0
- package/ui/src/pages/Org.tsx +132 -0
- package/ui/src/pages/OrgChart.tsx +447 -0
- package/ui/src/pages/PluginManager.tsx +510 -0
- package/ui/src/pages/PluginPage.tsx +156 -0
- package/ui/src/pages/PluginSettings.tsx +836 -0
- package/ui/src/pages/ProjectDetail.tsx +633 -0
- package/ui/src/pages/Projects.tsx +87 -0
- package/ui/src/pages/RoutineDetail.tsx +1022 -0
- package/ui/src/pages/Routines.tsx +661 -0
- package/ui/src/pages/RunTranscriptUxLab.tsx +334 -0
- package/ui/src/pages/VisualOffice.tsx +243 -0
- package/ui/src/plugins/bridge-init.ts +69 -0
- package/ui/src/plugins/bridge.ts +476 -0
- package/ui/src/plugins/launchers.tsx +834 -0
- package/ui/src/plugins/slots.tsx +855 -0
- package/ui/tsconfig.json +21 -0
- package/ui/vite.config.ts +23 -0
- package/ui/vitest.config.ts +14 -0
- package/vitest.config.ts +11 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
export type AutosaveState = "idle" | "saving" | "saved" | "error";
|
|
4
|
+
|
|
5
|
+
const SAVING_DELAY_MS = 250;
|
|
6
|
+
const SAVED_LINGER_MS = 1600;
|
|
7
|
+
|
|
8
|
+
export function useAutosaveIndicator() {
|
|
9
|
+
const [state, setState] = useState<AutosaveState>("idle");
|
|
10
|
+
const saveIdRef = useRef(0);
|
|
11
|
+
const savingTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
12
|
+
const clearSavedTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
13
|
+
|
|
14
|
+
const clearTimers = useCallback(() => {
|
|
15
|
+
if (savingTimerRef.current) {
|
|
16
|
+
clearTimeout(savingTimerRef.current);
|
|
17
|
+
savingTimerRef.current = null;
|
|
18
|
+
}
|
|
19
|
+
if (clearSavedTimerRef.current) {
|
|
20
|
+
clearTimeout(clearSavedTimerRef.current);
|
|
21
|
+
clearSavedTimerRef.current = null;
|
|
22
|
+
}
|
|
23
|
+
}, []);
|
|
24
|
+
|
|
25
|
+
useEffect(() => clearTimers, [clearTimers]);
|
|
26
|
+
|
|
27
|
+
const reset = useCallback(() => {
|
|
28
|
+
saveIdRef.current += 1;
|
|
29
|
+
clearTimers();
|
|
30
|
+
setState("idle");
|
|
31
|
+
}, [clearTimers]);
|
|
32
|
+
|
|
33
|
+
const markDirty = useCallback(() => {
|
|
34
|
+
clearTimers();
|
|
35
|
+
setState("idle");
|
|
36
|
+
}, [clearTimers]);
|
|
37
|
+
|
|
38
|
+
const runSave = useCallback(async (save: () => Promise<void>) => {
|
|
39
|
+
const saveId = saveIdRef.current + 1;
|
|
40
|
+
saveIdRef.current = saveId;
|
|
41
|
+
clearTimers();
|
|
42
|
+
savingTimerRef.current = setTimeout(() => {
|
|
43
|
+
if (saveIdRef.current === saveId) {
|
|
44
|
+
setState("saving");
|
|
45
|
+
}
|
|
46
|
+
}, SAVING_DELAY_MS);
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
await save();
|
|
50
|
+
if (saveIdRef.current !== saveId) return;
|
|
51
|
+
clearTimers();
|
|
52
|
+
setState("saved");
|
|
53
|
+
clearSavedTimerRef.current = setTimeout(() => {
|
|
54
|
+
if (saveIdRef.current === saveId) {
|
|
55
|
+
setState("idle");
|
|
56
|
+
}
|
|
57
|
+
}, SAVED_LINGER_MS);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (saveIdRef.current !== saveId) throw error;
|
|
60
|
+
clearTimers();
|
|
61
|
+
setState("error");
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}, [clearTimers]);
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
state,
|
|
68
|
+
markDirty,
|
|
69
|
+
reset,
|
|
70
|
+
runSave,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
getRememberedPathOwnerCompanyId,
|
|
4
|
+
sanitizeRememberedPathForCompany,
|
|
5
|
+
} from "../lib/company-page-memory";
|
|
6
|
+
|
|
7
|
+
const companies = [
|
|
8
|
+
{ id: "for", issuePrefix: "FOR" },
|
|
9
|
+
{ id: "pap", issuePrefix: "PAP" },
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
describe("getRememberedPathOwnerCompanyId", () => {
|
|
13
|
+
it("uses the route company instead of stale selected-company state for prefixed routes", () => {
|
|
14
|
+
expect(
|
|
15
|
+
getRememberedPathOwnerCompanyId({
|
|
16
|
+
companies,
|
|
17
|
+
pathname: "/FOR/issues/FOR-1",
|
|
18
|
+
fallbackCompanyId: "pap",
|
|
19
|
+
}),
|
|
20
|
+
).toBe("for");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("skips saving when a prefixed route cannot yet be resolved to a known company", () => {
|
|
24
|
+
expect(
|
|
25
|
+
getRememberedPathOwnerCompanyId({
|
|
26
|
+
companies: [],
|
|
27
|
+
pathname: "/FOR/issues/FOR-1",
|
|
28
|
+
fallbackCompanyId: "pap",
|
|
29
|
+
}),
|
|
30
|
+
).toBeNull();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("falls back to the previous company for unprefixed board routes", () => {
|
|
34
|
+
expect(
|
|
35
|
+
getRememberedPathOwnerCompanyId({
|
|
36
|
+
companies,
|
|
37
|
+
pathname: "/dashboard",
|
|
38
|
+
fallbackCompanyId: "pap",
|
|
39
|
+
}),
|
|
40
|
+
).toBe("pap");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("treats unprefixed skills routes as board routes instead of company prefixes", () => {
|
|
44
|
+
expect(
|
|
45
|
+
getRememberedPathOwnerCompanyId({
|
|
46
|
+
companies,
|
|
47
|
+
pathname: "/skills/skill-123/files/SKILL.md",
|
|
48
|
+
fallbackCompanyId: "pap",
|
|
49
|
+
}),
|
|
50
|
+
).toBe("pap");
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("sanitizeRememberedPathForCompany", () => {
|
|
55
|
+
it("keeps remembered issue paths that belong to the target company", () => {
|
|
56
|
+
expect(
|
|
57
|
+
sanitizeRememberedPathForCompany({
|
|
58
|
+
path: "/issues/PAP-12",
|
|
59
|
+
companyPrefix: "PAP",
|
|
60
|
+
}),
|
|
61
|
+
).toBe("/issues/PAP-12");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("falls back to dashboard for remembered issue identifiers from another company", () => {
|
|
65
|
+
expect(
|
|
66
|
+
sanitizeRememberedPathForCompany({
|
|
67
|
+
path: "/issues/FOR-1",
|
|
68
|
+
companyPrefix: "PAP",
|
|
69
|
+
}),
|
|
70
|
+
).toBe("/dashboard");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("falls back to dashboard when no remembered path exists", () => {
|
|
74
|
+
expect(
|
|
75
|
+
sanitizeRememberedPathForCompany({
|
|
76
|
+
path: null,
|
|
77
|
+
companyPrefix: "PAP",
|
|
78
|
+
}),
|
|
79
|
+
).toBe("/dashboard");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("keeps remembered skills paths intact for the target company", () => {
|
|
83
|
+
expect(
|
|
84
|
+
sanitizeRememberedPathForCompany({
|
|
85
|
+
path: "/skills/skill-123/files/SKILL.md",
|
|
86
|
+
companyPrefix: "PAP",
|
|
87
|
+
}),
|
|
88
|
+
).toBe("/skills/skill-123/files/SKILL.md");
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
2
|
+
import { useLocation, useNavigate } from "@/lib/router";
|
|
3
|
+
import { useCompany } from "../context/CompanyContext";
|
|
4
|
+
import { toCompanyRelativePath } from "../lib/company-routes";
|
|
5
|
+
import {
|
|
6
|
+
getRememberedPathOwnerCompanyId,
|
|
7
|
+
isRememberableCompanyPath,
|
|
8
|
+
sanitizeRememberedPathForCompany,
|
|
9
|
+
} from "../lib/company-page-memory";
|
|
10
|
+
|
|
11
|
+
const STORAGE_KEY = "paperclip.companyPaths";
|
|
12
|
+
|
|
13
|
+
function getCompanyPaths(): Record<string, string> {
|
|
14
|
+
try {
|
|
15
|
+
const raw = localStorage.getItem(STORAGE_KEY);
|
|
16
|
+
if (raw) return JSON.parse(raw);
|
|
17
|
+
} catch {
|
|
18
|
+
/* ignore */
|
|
19
|
+
}
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function saveCompanyPath(companyId: string, path: string) {
|
|
24
|
+
const paths = getCompanyPaths();
|
|
25
|
+
paths[companyId] = path;
|
|
26
|
+
localStorage.setItem(STORAGE_KEY, JSON.stringify(paths));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Remembers the last visited page per company and navigates to it on company switch.
|
|
31
|
+
* Falls back to /dashboard if no page was previously visited for a company.
|
|
32
|
+
*/
|
|
33
|
+
export function useCompanyPageMemory() {
|
|
34
|
+
const { companies, selectedCompanyId, selectedCompany, selectionSource } = useCompany();
|
|
35
|
+
const location = useLocation();
|
|
36
|
+
const navigate = useNavigate();
|
|
37
|
+
const prevCompanyId = useRef<string | null>(selectedCompanyId);
|
|
38
|
+
const rememberedPathOwnerCompanyId = useMemo(
|
|
39
|
+
() =>
|
|
40
|
+
getRememberedPathOwnerCompanyId({
|
|
41
|
+
companies,
|
|
42
|
+
pathname: location.pathname,
|
|
43
|
+
fallbackCompanyId: prevCompanyId.current,
|
|
44
|
+
}),
|
|
45
|
+
[companies, location.pathname],
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// Save current path for current company on every location change.
|
|
49
|
+
// Uses prevCompanyId ref so we save under the correct company even
|
|
50
|
+
// during the render where selectedCompanyId has already changed.
|
|
51
|
+
const fullPath = location.pathname + location.search;
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
const companyId = rememberedPathOwnerCompanyId;
|
|
54
|
+
const relativePath = toCompanyRelativePath(fullPath);
|
|
55
|
+
if (companyId && isRememberableCompanyPath(relativePath)) {
|
|
56
|
+
saveCompanyPath(companyId, relativePath);
|
|
57
|
+
}
|
|
58
|
+
}, [fullPath, rememberedPathOwnerCompanyId]);
|
|
59
|
+
|
|
60
|
+
// Navigate to saved path when company changes
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (!selectedCompanyId) return;
|
|
63
|
+
|
|
64
|
+
if (
|
|
65
|
+
prevCompanyId.current !== null &&
|
|
66
|
+
selectedCompanyId !== prevCompanyId.current
|
|
67
|
+
) {
|
|
68
|
+
if (selectionSource !== "route_sync" && selectedCompany) {
|
|
69
|
+
const paths = getCompanyPaths();
|
|
70
|
+
const targetPath = sanitizeRememberedPathForCompany({
|
|
71
|
+
path: paths[selectedCompanyId],
|
|
72
|
+
companyPrefix: selectedCompany.issuePrefix,
|
|
73
|
+
});
|
|
74
|
+
navigate(`/${selectedCompany.issuePrefix}${targetPath}`, { replace: true });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
prevCompanyId.current = selectedCompanyId;
|
|
78
|
+
}, [selectedCompany, selectedCompanyId, selectionSource, navigate]);
|
|
79
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
export type DatePreset = "mtd" | "7d" | "30d" | "ytd" | "all" | "custom";
|
|
4
|
+
|
|
5
|
+
export const PRESET_LABELS: Record<DatePreset, string> = {
|
|
6
|
+
mtd: "Month to Date",
|
|
7
|
+
"7d": "Last 7 Days",
|
|
8
|
+
"30d": "Last 30 Days",
|
|
9
|
+
ytd: "Year to Date",
|
|
10
|
+
all: "All Time",
|
|
11
|
+
custom: "Custom",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const PRESET_KEYS: DatePreset[] = ["mtd", "7d", "30d", "ytd", "all", "custom"];
|
|
15
|
+
|
|
16
|
+
// note: computeRange is called inside a useMemo that re-evaluates once per minute
|
|
17
|
+
// (driven by minuteTick). this means sliding windows (7d, 30d) advance their upper
|
|
18
|
+
// bound at most once per minute — acceptable for a cost dashboard.
|
|
19
|
+
function computeRange(preset: DatePreset): { from: string; to: string } {
|
|
20
|
+
const now = new Date();
|
|
21
|
+
const to = now.toISOString();
|
|
22
|
+
switch (preset) {
|
|
23
|
+
case "mtd": {
|
|
24
|
+
const d = new Date(now.getFullYear(), now.getMonth(), 1);
|
|
25
|
+
return { from: d.toISOString(), to };
|
|
26
|
+
}
|
|
27
|
+
case "7d": {
|
|
28
|
+
const d = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7, 0, 0, 0, 0);
|
|
29
|
+
return { from: d.toISOString(), to };
|
|
30
|
+
}
|
|
31
|
+
case "30d": {
|
|
32
|
+
const d = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 30, 0, 0, 0, 0);
|
|
33
|
+
return { from: d.toISOString(), to };
|
|
34
|
+
}
|
|
35
|
+
case "ytd": {
|
|
36
|
+
const d = new Date(now.getFullYear(), 0, 1);
|
|
37
|
+
return { from: d.toISOString(), to };
|
|
38
|
+
}
|
|
39
|
+
case "all":
|
|
40
|
+
case "custom":
|
|
41
|
+
return { from: "", to: "" };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// floor a Date to the nearest minute so the query key is stable across
|
|
46
|
+
// 30s refetch ticks (prevents new cache entries on every poll cycle)
|
|
47
|
+
function floorToMinute(d: Date): string {
|
|
48
|
+
const floored = new Date(d);
|
|
49
|
+
floored.setSeconds(0, 0);
|
|
50
|
+
return floored.toISOString();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface UseDateRangeResult {
|
|
54
|
+
preset: DatePreset;
|
|
55
|
+
setPreset: (p: DatePreset) => void;
|
|
56
|
+
customFrom: string;
|
|
57
|
+
setCustomFrom: (v: string) => void;
|
|
58
|
+
customTo: string;
|
|
59
|
+
setCustomTo: (v: string) => void;
|
|
60
|
+
/** resolved iso strings ready to pass to api calls; empty string means unbounded */
|
|
61
|
+
from: string;
|
|
62
|
+
to: string;
|
|
63
|
+
/** false when preset=custom but both dates are not yet selected */
|
|
64
|
+
customReady: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function useDateRange(): UseDateRangeResult {
|
|
68
|
+
const [preset, setPreset] = useState<DatePreset>("mtd");
|
|
69
|
+
const [customFrom, setCustomFrom] = useState("");
|
|
70
|
+
const [customTo, setCustomTo] = useState("");
|
|
71
|
+
|
|
72
|
+
// tick at the next calendar minute boundary, then every 60s, so sliding presets
|
|
73
|
+
// (7d, 30d) advance their upper bound in sync with wall clock minutes rather than
|
|
74
|
+
// drifting by the mount offset.
|
|
75
|
+
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
76
|
+
const [minuteTick, setMinuteTick] = useState(() => floorToMinute(new Date()));
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
const now = new Date();
|
|
79
|
+
const msToNextMinute = (60 - now.getSeconds()) * 1000 - now.getMilliseconds();
|
|
80
|
+
const timeout = setTimeout(() => {
|
|
81
|
+
setMinuteTick(floorToMinute(new Date()));
|
|
82
|
+
intervalRef.current = setInterval(
|
|
83
|
+
() => setMinuteTick(floorToMinute(new Date())),
|
|
84
|
+
60_000,
|
|
85
|
+
);
|
|
86
|
+
}, msToNextMinute);
|
|
87
|
+
return () => {
|
|
88
|
+
clearTimeout(timeout);
|
|
89
|
+
if (intervalRef.current != null) clearInterval(intervalRef.current);
|
|
90
|
+
};
|
|
91
|
+
}, []);
|
|
92
|
+
|
|
93
|
+
const { from, to } = useMemo(() => {
|
|
94
|
+
if (preset !== "custom") return computeRange(preset);
|
|
95
|
+
// treat custom date strings as local-date boundaries so the full day is included
|
|
96
|
+
// regardless of the user's timezone. "from" starts at local midnight, "to" at 23:59:59.999.
|
|
97
|
+
const fromDate = customFrom ? new Date(customFrom + "T00:00:00") : null;
|
|
98
|
+
const toDate = customTo ? new Date(customTo + "T23:59:59.999") : null;
|
|
99
|
+
return {
|
|
100
|
+
from: fromDate ? fromDate.toISOString() : "",
|
|
101
|
+
to: toDate ? toDate.toISOString() : "",
|
|
102
|
+
};
|
|
103
|
+
// minuteTick drives re-evaluation of sliding presets once per minute.
|
|
104
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
105
|
+
}, [preset, customFrom, customTo, minuteTick]);
|
|
106
|
+
|
|
107
|
+
const customReady = preset !== "custom" || (!!customFrom && !!customTo);
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
preset,
|
|
111
|
+
setPreset,
|
|
112
|
+
customFrom,
|
|
113
|
+
setCustomFrom,
|
|
114
|
+
customTo,
|
|
115
|
+
setCustomTo,
|
|
116
|
+
from,
|
|
117
|
+
to,
|
|
118
|
+
customReady,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { useQuery } from "@tanstack/react-query";
|
|
3
|
+
import { accessApi } from "../api/access";
|
|
4
|
+
import { ApiError } from "../api/client";
|
|
5
|
+
import { approvalsApi } from "../api/approvals";
|
|
6
|
+
import { dashboardApi } from "../api/dashboard";
|
|
7
|
+
import { heartbeatsApi } from "../api/heartbeats";
|
|
8
|
+
import { issuesApi } from "../api/issues";
|
|
9
|
+
import { queryKeys } from "../lib/queryKeys";
|
|
10
|
+
import {
|
|
11
|
+
computeInboxBadgeData,
|
|
12
|
+
getRecentTouchedIssues,
|
|
13
|
+
loadDismissedInboxItems,
|
|
14
|
+
saveDismissedInboxItems,
|
|
15
|
+
loadReadInboxItems,
|
|
16
|
+
saveReadInboxItems,
|
|
17
|
+
READ_ITEMS_KEY,
|
|
18
|
+
} from "../lib/inbox";
|
|
19
|
+
|
|
20
|
+
const INBOX_ISSUE_STATUSES = "backlog,todo,in_progress,in_review,blocked,done";
|
|
21
|
+
|
|
22
|
+
export function useDismissedInboxItems() {
|
|
23
|
+
const [dismissed, setDismissed] = useState<Set<string>>(loadDismissedInboxItems);
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const handleStorage = (event: StorageEvent) => {
|
|
27
|
+
if (event.key !== "paperclip:inbox:dismissed") return;
|
|
28
|
+
setDismissed(loadDismissedInboxItems());
|
|
29
|
+
};
|
|
30
|
+
window.addEventListener("storage", handleStorage);
|
|
31
|
+
return () => window.removeEventListener("storage", handleStorage);
|
|
32
|
+
}, []);
|
|
33
|
+
|
|
34
|
+
const dismiss = (id: string) => {
|
|
35
|
+
setDismissed((prev) => {
|
|
36
|
+
const next = new Set(prev);
|
|
37
|
+
next.add(id);
|
|
38
|
+
saveDismissedInboxItems(next);
|
|
39
|
+
return next;
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return { dismissed, dismiss };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function useReadInboxItems() {
|
|
47
|
+
const [readItems, setReadItems] = useState<Set<string>>(loadReadInboxItems);
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
const handleStorage = (event: StorageEvent) => {
|
|
51
|
+
if (event.key !== READ_ITEMS_KEY) return;
|
|
52
|
+
setReadItems(loadReadInboxItems());
|
|
53
|
+
};
|
|
54
|
+
window.addEventListener("storage", handleStorage);
|
|
55
|
+
return () => window.removeEventListener("storage", handleStorage);
|
|
56
|
+
}, []);
|
|
57
|
+
|
|
58
|
+
const markRead = (id: string) => {
|
|
59
|
+
setReadItems((prev) => {
|
|
60
|
+
const next = new Set(prev);
|
|
61
|
+
next.add(id);
|
|
62
|
+
saveReadInboxItems(next);
|
|
63
|
+
return next;
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return { readItems, markRead };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function useInboxBadge(companyId: string | null | undefined) {
|
|
71
|
+
const { dismissed } = useDismissedInboxItems();
|
|
72
|
+
|
|
73
|
+
const { data: approvals = [] } = useQuery({
|
|
74
|
+
queryKey: queryKeys.approvals.list(companyId!),
|
|
75
|
+
queryFn: () => approvalsApi.list(companyId!),
|
|
76
|
+
enabled: !!companyId,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const { data: joinRequests = [] } = useQuery({
|
|
80
|
+
queryKey: queryKeys.access.joinRequests(companyId!),
|
|
81
|
+
queryFn: async () => {
|
|
82
|
+
try {
|
|
83
|
+
return await accessApi.listJoinRequests(companyId!, "pending_approval");
|
|
84
|
+
} catch (err) {
|
|
85
|
+
if (err instanceof ApiError && (err.status === 401 || err.status === 403)) {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
throw err;
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
enabled: !!companyId,
|
|
92
|
+
retry: false,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const { data: dashboard } = useQuery({
|
|
96
|
+
queryKey: queryKeys.dashboard(companyId!),
|
|
97
|
+
queryFn: () => dashboardApi.summary(companyId!),
|
|
98
|
+
enabled: !!companyId,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const { data: mineIssuesRaw = [] } = useQuery({
|
|
102
|
+
queryKey: queryKeys.issues.listMineByMe(companyId!),
|
|
103
|
+
queryFn: () =>
|
|
104
|
+
issuesApi.list(companyId!, {
|
|
105
|
+
touchedByUserId: "me",
|
|
106
|
+
inboxArchivedByUserId: "me",
|
|
107
|
+
status: INBOX_ISSUE_STATUSES,
|
|
108
|
+
}),
|
|
109
|
+
enabled: !!companyId,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const mineIssues = useMemo(() => getRecentTouchedIssues(mineIssuesRaw), [mineIssuesRaw]);
|
|
113
|
+
|
|
114
|
+
const { data: heartbeatRuns = [] } = useQuery({
|
|
115
|
+
queryKey: queryKeys.heartbeats(companyId!),
|
|
116
|
+
queryFn: () => heartbeatsApi.list(companyId!),
|
|
117
|
+
enabled: !!companyId,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return useMemo(
|
|
121
|
+
() =>
|
|
122
|
+
computeInboxBadgeData({
|
|
123
|
+
approvals,
|
|
124
|
+
joinRequests,
|
|
125
|
+
dashboard,
|
|
126
|
+
heartbeatRuns,
|
|
127
|
+
mineIssues,
|
|
128
|
+
dismissed,
|
|
129
|
+
}),
|
|
130
|
+
[approvals, joinRequests, dashboard, heartbeatRuns, mineIssues, dismissed],
|
|
131
|
+
);
|
|
132
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
|
|
3
|
+
interface ShortcutHandlers {
|
|
4
|
+
onNewIssue?: () => void;
|
|
5
|
+
onToggleSidebar?: () => void;
|
|
6
|
+
onTogglePanel?: () => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function useKeyboardShortcuts({ onNewIssue, onToggleSidebar, onTogglePanel }: ShortcutHandlers) {
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
function handleKeyDown(e: KeyboardEvent) {
|
|
12
|
+
// Don't fire shortcuts when typing in inputs
|
|
13
|
+
const target = e.target as HTMLElement;
|
|
14
|
+
if (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.isContentEditable) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// C → New Issue
|
|
19
|
+
if (e.key === "c" && !e.metaKey && !e.ctrlKey && !e.altKey) {
|
|
20
|
+
e.preventDefault();
|
|
21
|
+
onNewIssue?.();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// [ → Toggle Sidebar
|
|
25
|
+
if (e.key === "[" && !e.metaKey && !e.ctrlKey) {
|
|
26
|
+
e.preventDefault();
|
|
27
|
+
onToggleSidebar?.();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ] → Toggle Panel
|
|
31
|
+
if (e.key === "]" && !e.metaKey && !e.ctrlKey) {
|
|
32
|
+
e.preventDefault();
|
|
33
|
+
onTogglePanel?.();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
38
|
+
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
39
|
+
}, [onNewIssue, onToggleSidebar, onTogglePanel]);
|
|
40
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import type { Project } from "@corporateai/shared";
|
|
3
|
+
import {
|
|
4
|
+
getProjectOrderStorageKey,
|
|
5
|
+
PROJECT_ORDER_UPDATED_EVENT,
|
|
6
|
+
readProjectOrder,
|
|
7
|
+
sortProjectsByStoredOrder,
|
|
8
|
+
writeProjectOrder,
|
|
9
|
+
} from "../lib/project-order";
|
|
10
|
+
|
|
11
|
+
type UseProjectOrderParams = {
|
|
12
|
+
projects: Project[];
|
|
13
|
+
companyId: string | null | undefined;
|
|
14
|
+
userId: string | null | undefined;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type ProjectOrderUpdatedDetail = {
|
|
18
|
+
storageKey: string;
|
|
19
|
+
orderedIds: string[];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function areEqual(a: string[], b: string[]) {
|
|
23
|
+
if (a.length !== b.length) return false;
|
|
24
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
25
|
+
if (a[i] !== b[i]) return false;
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function buildOrderIds(projects: Project[], orderedIds: string[]) {
|
|
31
|
+
return sortProjectsByStoredOrder(projects, orderedIds).map((project) => project.id);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function useProjectOrder({ projects, companyId, userId }: UseProjectOrderParams) {
|
|
35
|
+
const storageKey = useMemo(() => {
|
|
36
|
+
if (!companyId) return null;
|
|
37
|
+
return getProjectOrderStorageKey(companyId, userId);
|
|
38
|
+
}, [companyId, userId]);
|
|
39
|
+
|
|
40
|
+
const [orderedIds, setOrderedIds] = useState<string[]>(() => {
|
|
41
|
+
if (!storageKey) return projects.map((project) => project.id);
|
|
42
|
+
return buildOrderIds(projects, readProjectOrder(storageKey));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
const nextIds = storageKey
|
|
47
|
+
? buildOrderIds(projects, readProjectOrder(storageKey))
|
|
48
|
+
: projects.map((project) => project.id);
|
|
49
|
+
setOrderedIds((current) => (areEqual(current, nextIds) ? current : nextIds));
|
|
50
|
+
}, [projects, storageKey]);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (!storageKey) return;
|
|
54
|
+
|
|
55
|
+
const syncFromIds = (ids: string[]) => {
|
|
56
|
+
const nextIds = buildOrderIds(projects, ids);
|
|
57
|
+
setOrderedIds((current) => (areEqual(current, nextIds) ? current : nextIds));
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const onStorage = (event: StorageEvent) => {
|
|
61
|
+
if (event.key !== storageKey) return;
|
|
62
|
+
syncFromIds(readProjectOrder(storageKey));
|
|
63
|
+
};
|
|
64
|
+
const onCustomEvent = (event: Event) => {
|
|
65
|
+
const detail = (event as CustomEvent<ProjectOrderUpdatedDetail>).detail;
|
|
66
|
+
if (!detail || detail.storageKey !== storageKey) return;
|
|
67
|
+
syncFromIds(detail.orderedIds);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
window.addEventListener("storage", onStorage);
|
|
71
|
+
window.addEventListener(PROJECT_ORDER_UPDATED_EVENT, onCustomEvent);
|
|
72
|
+
return () => {
|
|
73
|
+
window.removeEventListener("storage", onStorage);
|
|
74
|
+
window.removeEventListener(PROJECT_ORDER_UPDATED_EVENT, onCustomEvent);
|
|
75
|
+
};
|
|
76
|
+
}, [projects, storageKey]);
|
|
77
|
+
|
|
78
|
+
const orderedProjects = useMemo(
|
|
79
|
+
() => sortProjectsByStoredOrder(projects, orderedIds),
|
|
80
|
+
[projects, orderedIds],
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const persistOrder = useCallback(
|
|
84
|
+
(ids: string[]) => {
|
|
85
|
+
const idSet = new Set(projects.map((project) => project.id));
|
|
86
|
+
const filtered = ids.filter((id) => idSet.has(id));
|
|
87
|
+
for (const project of projects) {
|
|
88
|
+
if (!filtered.includes(project.id)) filtered.push(project.id);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
setOrderedIds((current) => (areEqual(current, filtered) ? current : filtered));
|
|
92
|
+
if (storageKey) {
|
|
93
|
+
writeProjectOrder(storageKey, filtered);
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
[projects, storageKey],
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
orderedProjects,
|
|
101
|
+
orderedIds,
|
|
102
|
+
persistOrder,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|