Files
kandev/apps/web/components/linear/use-linear-enabled.ts
T
João Salavisa 7b5b3f329d feat: add Linear integration (#736)
* feat: add Linear integration

Mirrors the existing Jira integration so a workspace can connect to Linear
with a personal API key, browse and search issues, and start a Kandev task
from any issue. Backend uses Linear's GraphQL API with a workspace-scoped
config + secret store and a 90s auth-health poller; the frontend adds a
settings page, an issue dialog with state transitions, link/import buttons
in the task UI, and a paginated /linear browse page with a Start task flow.

* fix(linear): address PR review feedback

- Route httpListStates through Service.ListStates instead of clientFor
- Extract repeated workspace_id required string into a constant
- Log GetConfig errors during credential resolution instead of dropping them
- Clear popover error state on close in import + link buttons
- Pick the first workflow with steps in the quick task launcher (don't hard-pin to workflows[0])
- Reset useLinearEnabled state on workspace change via tag-based pattern
- Add cursor-pointer to anchor + Link elements per project convention
- Add tests: handlers_test.go, linear-api.test.ts, use-linear-enabled.test.ts, linear-issue-common.test.tsx
- Document internal/jira and internal/linear in CLAUDE.md/AGENTS.md

* fix(linear): address second-round PR review feedback

- Round Linear's fractional state position instead of truncating it via
  int(math.Round(s.Position)) so adjacent fractional positions don't collapse
- Force LinearSettings to remount on workspace change with key={id} so the
  previous workspace's config / form / testResult / teams cannot bleed into
  the next workspace before the new fetches settle
- Broaden the legacy-value useLinearEnabled test to cover "true", "1", "yes",
  and "legacy" so a regression to strict value === "true" would fail

* fix(linear): address third-round PR review feedback

- Propagate context.Canceled / context.DeadlineExceeded from ListStates in
  GetIssue instead of swallowing them — a caller cancellation must not return
  a partial-success response
- Drop the one-off contains() test helper in handlers_test.go and use
  strings.Contains directly so the assertion is self-documenting
- Add getLinearIssue URL-shape test that locks identifier-as-path-segment
  vs workspaceId-as-query-param so accidentally swapping them would fail

* test(linear): extract repeated literals in linear-api.test.ts

Pulls "ws-1", the API base URL, and the auth-method literal into module-
level constants so the assertions read as the route shape they care about
and the duplicate-strings ESLint rule (currently warn, threshold 4)
stays quiet as the file grows.

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
2026-05-01 10:50:14 +01:00

74 lines
2.6 KiB
TypeScript

"use client";
import { useCallback, useEffect, useState } from "react";
// Workspace-scoped: a workspace can have Linear configured but the user may
// want to silence the integration UI without removing credentials.
const storageKey = (workspaceId: string) => `kandev:linear:enabled:${workspaceId}:v1`;
const SYNC_EVENT = "kandev:linear:enabled-changed";
function readEnabled(workspaceId: string): boolean {
if (typeof window === "undefined") return true;
try {
const raw = window.localStorage.getItem(storageKey(workspaceId));
if (raw === null) return true;
return raw !== "false";
} catch {
return true;
}
}
// State tagged with the workspace it was read for. Comparing the tag against
// the currently-requested workspaceId at read time gives us a stale-free
// `loaded` signal without calling setState synchronously inside the effect
// body (which the react-hooks lint rule forbids).
type EnabledState = { workspaceId: string | undefined; enabled: boolean };
export function useLinearEnabled(workspaceId: string | undefined) {
const [state, setState] = useState<EnabledState>({ workspaceId: undefined, enabled: true });
useEffect(() => {
let cancelled = false;
async function init() {
if (cancelled) return;
const next = workspaceId ? readEnabled(workspaceId) : true;
setState({ workspaceId, enabled: next });
}
void init();
if (!workspaceId) return;
const onChange = () => {
const next = readEnabled(workspaceId);
setState({ workspaceId, enabled: next });
};
window.addEventListener("storage", onChange);
window.addEventListener(SYNC_EVENT, onChange);
return () => {
cancelled = true;
window.removeEventListener("storage", onChange);
window.removeEventListener(SYNC_EVENT, onChange);
};
}, [workspaceId]);
const setEnabled = useCallback(
(next: boolean) => {
if (!workspaceId || typeof window === "undefined") return;
try {
window.localStorage.setItem(storageKey(workspaceId), String(next));
window.dispatchEvent(new Event(SYNC_EVENT));
} catch {
// Quota or private mode: state still updates in-memory.
}
setState({ workspaceId, enabled: next });
},
[workspaceId],
);
// `loaded` flips true once the state's workspace tag matches the caller's;
// this naturally resets when workspaceId changes without us ever writing a
// synchronous `setLoaded(false)` inside the effect.
const loaded = state.workspaceId === workspaceId;
const enabled = loaded ? state.enabled : true;
return { enabled, setEnabled, loaded };
}