Files
kandev/apps/web/components/settings/settings-app-sidebar.tsx
T
João Salavisa bb36cb9e01 refactor: move integrations to top-level settings with install-wide configs (#787)
* 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>
2026-05-03 10:22:57 +01:00

372 lines
13 KiB
TypeScript

"use client";
import { useEffect } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import {
IconSettings,
IconFolder,
IconRobot,
IconBell,
IconCode,
IconCpu,
IconKey,
IconMessageCircle,
IconBrandGithub,
IconHexagon,
IconSparkles,
IconWand,
IconGitBranch,
IconArrowsShuffle,
IconTicket,
IconPlugConnected,
} from "@tabler/icons-react";
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupLabel,
SidebarGroupContent,
SidebarMenu,
SidebarMenuItem,
SidebarMenuButton,
SidebarMenuSub,
SidebarMenuSubItem,
SidebarMenuSubButton,
SidebarHeader,
useSidebar,
} from "@kandev/ui/sidebar";
import { ScrollArea } from "@kandev/ui/scroll-area";
import { ScrollOnOverflow } from "@kandev/ui/scroll-on-overflow";
import { useAppStore } from "@/components/state-provider";
import { useAvailableAgents } from "@/hooks/domains/settings/use-available-agents";
import { AgentLogo } from "@/components/agent-logo";
import { getExecutorIcon } from "@/lib/executor-icons";
import { getCapabilityWarning } from "@/lib/capability-warning";
import type { Workspace, Agent, AgentProfile, Executor } from "@/lib/types/http";
type GeneralSidebarSectionProps = {
pathname: string;
};
function GeneralSidebarSection({ pathname }: GeneralSidebarSectionProps) {
return (
<SidebarMenuItem>
<SidebarMenuButton asChild tooltip="General">
<Link href="/settings/general">
<IconSettings className="h-4 w-4" />
<span>General</span>
</Link>
</SidebarMenuButton>
<SidebarMenuSub className="ml-3 mt-1">
<SidebarMenuSubItem>
<SidebarMenuSubButton
asChild
size="sm"
isActive={pathname === "/settings/general/notifications"}
>
<Link href="/settings/general/notifications">
<IconBell className="h-4 w-4" />
<span>Notifications</span>
</Link>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
<SidebarMenuSubItem>
<SidebarMenuSubButton
asChild
size="sm"
isActive={pathname === "/settings/general/editors"}
>
<Link href="/settings/general/editors">
<IconCode className="h-4 w-4" />
<span>Editors</span>
</Link>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
</SidebarMenuSub>
</SidebarMenuItem>
);
}
type WorkspacesSidebarSectionProps = {
pathname: string;
workspaces: Workspace[];
};
function WorkspacesSidebarSection({ pathname, workspaces }: WorkspacesSidebarSectionProps) {
return (
<SidebarMenuItem>
<SidebarMenuButton asChild tooltip="Workspaces">
<Link href="/settings/workspace">
<IconFolder className="h-4 w-4" />
<span>Workspaces</span>
</Link>
</SidebarMenuButton>
{workspaces.length > 0 && (
<SidebarMenuSub className="ml-3 mt-1">
{workspaces.map((workspace: Workspace) => {
const workspacePath = `/settings/workspace/${workspace.id}`;
const workflowsPath = `${workspacePath}/workflows`;
const repositoriesPath = `${workspacePath}/repositories`;
return (
<SidebarMenuSubItem key={workspace.id}>
<SidebarMenuSubButton asChild isActive={pathname === workspacePath}>
<Link href={workspacePath}>
<span>{workspace.name}</span>
</Link>
</SidebarMenuSubButton>
<SidebarMenuSub className="ml-3">
<SidebarMenuSubItem>
<SidebarMenuSubButton
asChild
size="sm"
isActive={pathname === repositoriesPath}
>
<Link href={repositoriesPath}>
<IconGitBranch className="h-3.5 w-3.5" />
<span>Repositories</span>
</Link>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
<SidebarMenuSubItem>
<SidebarMenuSubButton asChild size="sm" isActive={pathname === workflowsPath}>
<Link href={workflowsPath}>
<IconArrowsShuffle className="h-3.5 w-3.5" />
<span>Workflows</span>
</Link>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
</SidebarMenuSub>
</SidebarMenuSubItem>
);
})}
</SidebarMenuSub>
)}
</SidebarMenuItem>
);
}
function IntegrationsSidebarSection({ pathname }: { pathname: string }) {
const items: Array<{ href: string; label: string; Icon: typeof IconBrandGithub }> = [
{ href: "/settings/integrations/github", label: "GitHub", Icon: IconBrandGithub },
{ href: "/settings/integrations/jira", label: "Jira", Icon: IconTicket },
{ href: "/settings/integrations/linear", label: "Linear", Icon: IconHexagon },
];
return (
<SidebarMenuItem>
<SidebarMenuButton asChild tooltip="Integrations">
<Link href="/settings/integrations">
<IconPlugConnected className="h-4 w-4" />
<span>Integrations</span>
</Link>
</SidebarMenuButton>
<SidebarMenuSub className="ml-3 mt-1">
{items.map(({ href, label, Icon }) => (
<SidebarMenuSubItem key={href}>
<SidebarMenuSubButton asChild size="sm" isActive={pathname === href}>
<Link href={href}>
<Icon className="h-3.5 w-3.5" />
<span>{label}</span>
</Link>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
))}
</SidebarMenuSub>
</SidebarMenuItem>
);
}
type AgentsSidebarSectionProps = {
pathname: string;
agents: Agent[];
};
function AgentsSidebarSection({ pathname, agents }: AgentsSidebarSectionProps) {
return (
<SidebarMenuItem>
<SidebarMenuButton asChild tooltip="Agents">
<Link href="/settings/agents">
<IconRobot className="h-4 w-4" />
<span>Agents</span>
</Link>
</SidebarMenuButton>
{agents.length > 0 && (
<SidebarMenuSub className="ml-3 mt-1">
{agents.flatMap((agent: Agent) =>
agent.profiles.map((profile: AgentProfile) => {
const encodedAgent = encodeURIComponent(agent.name);
const profilePath = `/settings/agents/${encodedAgent}/profiles/${profile.id}`;
const agentLabel = profile.agent_display_name || agent.name;
const warning = getCapabilityWarning(agent.capability_status, agent.capability_error);
return (
<SidebarMenuSubItem key={profile.id} className="min-w-0">
<SidebarMenuSubButton asChild isActive={pathname === profilePath}>
<Link
href={profilePath}
className="!flex min-w-0 items-center gap-1.5"
title={warning?.title || `${agentLabel} • ${profile.name}`}
>
<AgentLogo agentName={agent.name} className="shrink-0" />
<ScrollOnOverflow className="min-w-0">
{agentLabel} • {profile.name}
</ScrollOnOverflow>
{warning && <warning.Icon className={`size-3.5 shrink-0 ${warning.color}`} />}
</Link>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
);
}),
)}
</SidebarMenuSub>
)}
</SidebarMenuItem>
);
}
type ExecutorsSidebarSectionProps = {
pathname: string;
executors: Executor[];
};
function ExecutorsSidebarSection({ pathname, executors }: ExecutorsSidebarSectionProps) {
const allProfiles = executors.flatMap((e) =>
(e.profiles ?? []).map((p) => ({ ...p, executorType: e.type })),
);
return (
<SidebarMenuItem>
<SidebarMenuButton asChild tooltip="Executors" isActive={pathname === "/settings/executors"}>
<Link href="/settings/executors">
<IconCpu className="h-4 w-4" />
<span>Executors</span>
</Link>
</SidebarMenuButton>
{allProfiles.length > 0 && (
<SidebarMenuSub className="ml-3 mt-1">
{allProfiles.map((profile) => {
const Icon = getExecutorIcon(profile.executorType);
const profilePath = `/settings/executors/${profile.id}`;
return (
<SidebarMenuSubItem key={profile.id}>
<SidebarMenuSubButton asChild size="sm" isActive={pathname === profilePath}>
<Link href={profilePath} className="!flex items-center gap-1.5">
<Icon className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
<span>{profile.name}</span>
</Link>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
);
})}
</SidebarMenuSub>
)}
</SidebarMenuItem>
);
}
function SecretsSidebarSection({ pathname }: { pathname: string }) {
return (
<SidebarMenuItem>
<SidebarMenuButton asChild isActive={pathname === "/settings/general/secrets"}>
<Link href="/settings/general/secrets">
<IconKey className="h-4 w-4" />
<span>Secrets</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
);
}
export function SettingsAppSidebar() {
const pathname = usePathname();
const { setOpenMobile, isMobile } = useSidebar();
const workspaces = useAppStore((state) => state.workspaces.items);
const executors = useAppStore((state) => state.executors.items);
const agents = useAppStore((state) => state.settingsAgents.items);
useAvailableAgents();
// Close mobile sidebar when navigating to a new page
useEffect(() => {
if (isMobile) {
setOpenMobile(false);
}
}, [pathname, isMobile, setOpenMobile]);
return (
<Sidebar variant="inset">
<SidebarHeader className="h-16 justify-center">
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton size="lg" asChild>
<Link href="/">
<span className="text-2xl font-bold">KanDev</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarHeader>
<SidebarContent className="overflow-hidden">
<ScrollArea
className="h-full [&_[data-slot=scroll-area-viewport]>div]:!block [&_[data-slot=scroll-area-viewport]>div]:!min-w-0"
type="always"
>
<SidebarGroup>
<SidebarGroupLabel>Settings</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
<GeneralSidebarSection pathname={pathname} />
<WorkspacesSidebarSection pathname={pathname} workspaces={workspaces} />
<IntegrationsSidebarSection pathname={pathname} />
<AgentsSidebarSection pathname={pathname} agents={agents} />
{/* Prompts */}
<SidebarMenuItem>
<SidebarMenuButton asChild isActive={pathname === "/settings/prompts"}>
<Link href="/settings/prompts">
<IconMessageCircle className="h-4 w-4" />
<span>Prompts</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
{/* Utility Agents */}
<SidebarMenuItem>
<SidebarMenuButton asChild isActive={pathname === "/settings/utility-agents"}>
<Link href="/settings/utility-agents">
<IconWand className="h-4 w-4" />
<span>Utility Agents</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
<ExecutorsSidebarSection pathname={pathname} executors={executors} />
<SecretsSidebarSection pathname={pathname} />
{/* External MCP */}
<SidebarMenuItem>
<SidebarMenuButton asChild isActive={pathname === "/settings/external-mcp"}>
<Link href="/settings/external-mcp">
<IconPlugConnected className="h-4 w-4" />
<span>External MCP</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
{/* Changelog */}
<SidebarMenuItem>
<SidebarMenuButton asChild isActive={pathname === "/settings/changelog"}>
<Link href="/settings/changelog">
<IconSparkles className="h-4 w-4" />
<span>Changelog</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</ScrollArea>
</SidebarContent>
</Sidebar>
);
}