bb36cb9e01
* refactor: move integrations to top-level settings with install-wide configs Jira and Linear configurations are now install-wide singletons rather than per-workspace duplicates, and the GitHub/Jira/Linear settings pages live under /settings/integrations instead of being nested inside each workspace. Issue and PR watchers stay per-workspace via a workspace switcher inside each integration page. Backend ships one-shot migrations that promote the most-recently-updated row and copy its secret onto the new global key. * fix: address review feedback on integrations refactor Fix migration crash on installs whose jira_configs predate the auth-health columns by selecting only guaranteed-present legacy columns and falling back to defaults for the rest. Cover the empty-legacy-table and pre-health-columns upgrade paths with new store tests. On the frontend, refactor useIntegrationEnabled to take string parameters instead of an inline options object so getSnapshot stays referentially stable across renders, and stop dispatching a misleading no-op sync event when localStorage.setItem fails in private mode. * fix: hydrate workspace list on integration settings pages The integration pages wrapped their content in a fresh StateProvider, which created a nested store scope with an empty initial state and shadowed the settings layout's hydrated workspace list. WorkspaceScopedSection then saw zero workspaces and rendered the "Create a workspace to configure this integration" empty state even when the user had workspaces. Drop the redundant StateProvider from the jira and linear pages so they share the layout's store. The github page switches to StateHydrator so it can still seed the GitHub OAuth status without resetting the rest of the store. * refactor(integrations): flatten watcher tables, pick workspace in create dialog Hiding watchers behind a workspace switcher made the list invisible at a glance — a user with multiple workspaces had to flip between each one to see what was configured. Replace the switcher with a flat table that lists every watch across every workspace, with a Workspace column for context, and move workspace selection into the create dialog so the user picks once when the watcher is born. Backend: jira list-issue-watches and github list-review-watches / list-issue-watches accept the workspace_id query param as optional; when omitted, the corresponding store ListAll* method returns rows across every workspace. Per-row mutations still require workspace_id as the IDOR guard. Frontend: useReviewWatches / useIssueWatches / useJiraIssueWatches gain a three-state contract — string for one workspace, undefined for all workspaces, null to skip fetching. Watch tables grow an optional showWorkspace column. Watch dialogs accept an optional workspaceId prop; when absent, the dialog renders a workspace dropdown for the create flow (disabled when editing existing rows so refs aren't orphaned). Github review-watch-dialog crossed the 600-line lint cap so the prompt editor moved to review-watch-prompt-field.tsx. * fix(integrations): make watcher step dropdown reactive across workspace switches Switching to a workspace whose workflows had to be fetched left the workflow step dropdown stuck — the user could pick a workflow but the step Select either showed the previous workflow's steps or appeared inert. The cause was the per-dialog useWorkflowSteps hook deferring its empty-state reset via Promise.resolve().then(() => setSteps([])) to dodge a setState-in-effect lint rule, which let stale steps survive into the next render and gave Radix nothing to anchor the popover to during the in-flight fetch. Hoist the hook into a shared useWorkflowSteps that resets via React's official setState-during-render pattern, exposes a loading flag, and returns a stepPlaceholder helper so the Select can render "Loading steps…" or "No steps in this workflow" instead of an empty popover. The Step Select is now also disabled while loading or when there are no steps, making the state of the dropdown unambiguous. * fix: address PR review feedback on integrations refactor Backend: - linear migration: add column-presence guard mirroring the Jira fix so installs that upgraded from the original Linear schema (without org_slug / last_checked_at / last_ok / last_error) don't crash startup with "no such column"; add TestStore_MigrateLegacyPerWorkspaceTable_PreHealthColumns - jira/handlers.go: drop the now-unused errMsgWorkspaceIDRequired constant that was failing golangci-lint - jira/mock_controller_test.go (new): cover setAuthHealth happy-path and invalid-JSON-rejection so the singleton-shaped payload is locked in Frontend: - useIntegrationAvailable: gate the auth-health probe on loaded && enabled so disabled integrations don't poll /config every 90s - use-workflow-steps: initialize loading to !!workflowId so edit-mode opens show "Loading steps…" instead of "No steps in this workflow" before the first fetch lands - use-workflow-steps.test.ts (new): cover the empty/non-empty workflowId, workflow switch, fetch error, and stepPlaceholder branches - use-linear-enabled.test: tighten the legacy-key migration assertion to pin the migrated value rather than just verifying key cleanup --------- Co-authored-by: Kandev Agent <agent@kandev.dev>
114 lines
4.5 KiB
TypeScript
114 lines
4.5 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useMemo, useSyncExternalStore } from "react";
|
|
|
|
// useIntegrationEnabled is the install-wide on/off toggle every third-party
|
|
// integration UI (jira, linear, future) needs: a localStorage-backed boolean
|
|
// that defaults to true, syncs across tabs via the `storage` event, and within
|
|
// a tab via a custom event the integration provides.
|
|
//
|
|
// Connection state is install-wide (one Jira account, one Linear account), so
|
|
// the toggle is too. The legacy per-workspace key is migrated transparently on
|
|
// first read.
|
|
//
|
|
// The signature takes plain string parameters rather than an options object so
|
|
// useSyncExternalStore's getSnapshot stays referentially stable across
|
|
// renders — passing an inline `{...}` would give getSnapshot a new identity
|
|
// every render and re-run the migration scan on each pass.
|
|
|
|
// migrateLegacyKey is idempotent: the first call moves any leftover
|
|
// per-workspace value onto storageKey and removes the legacy entries;
|
|
// subsequent calls are no-ops because there are no legacy keys left to scan.
|
|
// Cheap enough to run on every snapshot read (a localStorage iteration over
|
|
// kandev:* keys), so we don't need to memoize across re-renders.
|
|
function migrateLegacyKey(storageKey: string, legacyKeyPrefix: string): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
if (window.localStorage.getItem(storageKey) !== null) return;
|
|
let surviving: string | null = null;
|
|
const stale: string[] = [];
|
|
for (let i = 0; i < window.localStorage.length; i++) {
|
|
const key = window.localStorage.key(i);
|
|
if (!key || !key.startsWith(legacyKeyPrefix) || key === storageKey) continue;
|
|
stale.push(key);
|
|
const value = window.localStorage.getItem(key);
|
|
if (value !== null && surviving === null) surviving = value;
|
|
}
|
|
if (surviving !== null) {
|
|
window.localStorage.setItem(storageKey, surviving);
|
|
}
|
|
for (const k of stale) window.localStorage.removeItem(k);
|
|
} catch {
|
|
// Quota / private mode — fall through; the toggle just defaults to on.
|
|
}
|
|
}
|
|
|
|
function readEnabled(storageKey: string): boolean {
|
|
if (typeof window === "undefined") return true;
|
|
try {
|
|
const raw = window.localStorage.getItem(storageKey);
|
|
if (raw === null) return true;
|
|
return raw !== "false";
|
|
} catch {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
export function useIntegrationEnabled(
|
|
storageKey: string,
|
|
legacyKeyPrefix: string,
|
|
syncEvent: string,
|
|
) {
|
|
// useSyncExternalStore reads localStorage on every render, but the snapshot
|
|
// is referentially stable (a boolean) so React only re-renders when the
|
|
// value changes. This avoids setState-in-effect warnings while still giving
|
|
// SSR a deterministic default and post-mount hydration to the persisted
|
|
// value.
|
|
const subscribe = useMemo(
|
|
() => (notify: () => void) => {
|
|
if (typeof window === "undefined") return () => {};
|
|
window.addEventListener("storage", notify);
|
|
window.addEventListener(syncEvent, notify);
|
|
return () => {
|
|
window.removeEventListener("storage", notify);
|
|
window.removeEventListener(syncEvent, notify);
|
|
};
|
|
},
|
|
[syncEvent],
|
|
);
|
|
|
|
const getSnapshot = useCallback(() => {
|
|
migrateLegacyKey(storageKey, legacyKeyPrefix);
|
|
return readEnabled(storageKey);
|
|
}, [storageKey, legacyKeyPrefix]);
|
|
|
|
const getServerSnapshot = useCallback(() => true, []);
|
|
|
|
const enabled = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
|
|
const setEnabled = useCallback(
|
|
(next: boolean) => {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(storageKey, String(next));
|
|
} catch {
|
|
// Quota / private mode: the write failed, so a re-read via
|
|
// getSnapshot would still return the old value. Don't dispatch the
|
|
// sync event — the previous in-memory fallback is gone with
|
|
// useSyncExternalStore, so silently re-dispatching would just look
|
|
// like a no-op flip. Surface the failure by leaving the toggle stuck
|
|
// at its previous value; the caller can detect persistence failure
|
|
// by observing that `enabled` did not change.
|
|
return;
|
|
}
|
|
window.dispatchEvent(new Event(syncEvent));
|
|
},
|
|
[storageKey, syncEvent],
|
|
);
|
|
|
|
// `loaded` is always true with useSyncExternalStore — the snapshot is read
|
|
// synchronously on first render. Kept in the return shape so existing
|
|
// callers (which gated effects on `loaded`) don't need to change.
|
|
return { enabled, setEnabled, loaded: true };
|
|
}
|