Files
kandev/apps/web/components/github/github-settings.tsx
T
João Salavisa ccc62d9324 feat(github): configurable quick-action presets and PR branch checkout (#689)
* feat(github): configurable quick-action presets and PR branch checkout

- Add workspace-scoped action presets (icon, label, hint, prompt template) for PRs and Issues, editable from GitHub settings with reset-to-defaults.
- When starting a task from a PR, check out the PR head branch directly in the worktree instead of branching off main.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(github): extract action-preset service to keep service.go under line limit

Splits the preset CRUD methods out of the oversized service.go and applies prettier formatting to three frontend files flagged by CI.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(github): address review feedback on action presets

- Prevent infinite refetch loop when fetching presets fails by tracking attempted workspaces in a ref instead of re-entering the effect on every loading-state change.
- Drop unused savePR/saveIssue helpers from the hook; only save() is consumed.
- Reorder the init spread in updateGitHubActionPresets/resetGitHubActionPresets so callers can't accidentally overwrite method/body.
- Tighten the GitHubActionPreset.icon type with string & {} so known icon keys keep autocomplete.
- Sharpen the eye-icon test to assert the exact IconEye mapping.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(github): address second round of review feedback

- Seed preset editor drafts from current store state via lazy initializers so a warm mount shows the user's saved customisations instead of silently reverting to defaults when Save is clicked.
- Guard Reset button with its own in-flight state to prevent double-firing.
- Stop freezing resolved defaults into the stored row on a partial update; persist only what the store has for the untouched kind so future default improvements still reach those workspaces.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(github): tighten quick-actions editor UI

Replace the stacked PR/Issue editors and multi-line preset cards with a tabbed layout and single-line rows. Each row shows icon · label · hint inline; an "Edit prompt" toggle reveals the template textarea only when needed. Drops the redundant section headings, up/down reorder buttons, and verbose template helper label (hint moved into the textarea placeholder).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(github): widen icon select trigger so the icon is visible

The icon select trigger had a fixed 40px width which the SelectTrigger's built-in chevron, padding, and icon gap all competed for, clipping the icon out of view. Drop the width override so the trigger sizes to its content.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-24 14:10:59 +01:00

293 lines
8.9 KiB
TypeScript

"use client";
import { useState, useCallback } from "react";
import { IconBrandGithub, IconPlus } from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import { Card, CardContent } from "@kandev/ui/card";
import { Separator } from "@kandev/ui/separator";
import { TooltipProvider } from "@kandev/ui/tooltip";
import { useToast } from "@/components/toast-provider";
import { SettingsSection } from "@/components/settings/settings-section";
import { GitHubStatusCard } from "./github-status";
import { ReviewWatchTable } from "./review-watch-table";
import { ReviewWatchDialog } from "./review-watch-dialog";
import { IssueWatchTable } from "./issue-watch-table";
import { IssueWatchDialog } from "./issue-watch-dialog";
import { ActionPresetsSection } from "./action-presets-section";
import { PRStatsPanel } from "./pr-stats";
import { useReviewWatches } from "@/hooks/domains/github/use-review-watches";
import { useIssueWatches } from "@/hooks/domains/github/use-issue-watches";
import type { ReviewWatch, IssueWatch } from "@/lib/types/github";
type GitHubSettingsProps = {
workspaceId: string;
};
function useWatchActions(workspaceId: string | null) {
const { items: watches, create, update, remove, trigger } = useReviewWatches(workspaceId);
const { toast } = useToast();
const handleDelete = useCallback(
async (id: string) => {
try {
await remove(id);
toast({ description: "Review watch deleted", variant: "success" });
} catch {
toast({ description: "Failed to delete review watch", variant: "error" });
}
},
[remove, toast],
);
const handleTrigger = useCallback(
async (id: string) => {
try {
const result = await trigger(id);
const count = result?.new_prs_found ?? 0;
if (count > 0) {
toast({
description: `Found ${count} new PR${count > 1 ? "s" : ""}`,
variant: "success",
});
} else {
toast({ description: "No new PRs found" });
}
} catch {
toast({ description: "Failed to check for PRs", variant: "error" });
}
},
[trigger, toast],
);
const handleToggleEnabled = useCallback(
async (watch: ReviewWatch) => {
try {
await update(watch.id, { enabled: !watch.enabled });
toast({
description: watch.enabled ? "Watch paused" : "Watch enabled",
variant: "success",
});
} catch {
toast({ description: "Failed to update watch", variant: "error" });
}
},
[update, toast],
);
return { watches, create, update, handleDelete, handleTrigger, handleToggleEnabled };
}
function useIssueWatchActions(workspaceId: string | null) {
const { items: watches, create, update, remove, trigger } = useIssueWatches(workspaceId);
const { toast } = useToast();
const handleDelete = useCallback(
async (id: string) => {
try {
await remove(id);
toast({ description: "Issue watch deleted", variant: "success" });
} catch {
toast({ description: "Failed to delete issue watch", variant: "error" });
}
},
[remove, toast],
);
const handleTrigger = useCallback(
async (id: string) => {
try {
const result = await trigger(id);
const count = result?.new_issues_found ?? 0;
if (count > 0) {
toast({
description: `Found ${count} new issue${count > 1 ? "s" : ""}`,
variant: "success",
});
} else {
toast({ description: "No new issues found" });
}
} catch {
toast({ description: "Failed to check for issues", variant: "error" });
}
},
[trigger, toast],
);
const handleToggleEnabled = useCallback(
async (watch: IssueWatch) => {
try {
await update(watch.id, { enabled: !watch.enabled });
toast({
description: watch.enabled ? "Watch paused" : "Watch enabled",
variant: "success",
});
} catch {
toast({ description: "Failed to update watch", variant: "error" });
}
},
[update, toast],
);
return { watches, create, update, handleDelete, handleTrigger, handleToggleEnabled };
}
export function GitHubSettings({ workspaceId }: GitHubSettingsProps) {
return (
<TooltipProvider>
<div className="space-y-8">
<div>
<h2 className="text-2xl font-bold flex items-center gap-2">
<IconBrandGithub className="h-6 w-6" />
GitHub Integration
</h2>
<p className="text-sm text-muted-foreground mt-1">
Configure GitHub PR monitoring, issue tracking, and review queue automation.
</p>
</div>
<Separator />
<SettingsSection title="Connection Status" description="GitHub authentication status">
<Card>
<CardContent className="py-3">
<GitHubStatusCard />
</CardContent>
</Card>
</SettingsSection>
<ReviewWatchSection workspaceId={workspaceId} />
<IssueWatchSection workspaceId={workspaceId} />
<ActionPresetsSection workspaceId={workspaceId} />
<SettingsSection
title="PR Analytics"
description="Pull request activity for this workspace."
>
<PRStatsPanel workspaceId={workspaceId} />
</SettingsSection>
</div>
</TooltipProvider>
);
}
function ReviewWatchSection({ workspaceId }: { workspaceId: string }) {
const { watches, create, update, handleDelete, handleTrigger, handleToggleEnabled } =
useWatchActions(workspaceId);
const { toast } = useToast();
const [dialogOpen, setDialogOpen] = useState(false);
const [editingWatch, setEditingWatch] = useState<ReviewWatch | null>(null);
const handleEdit = useCallback((watch: ReviewWatch) => {
setEditingWatch(watch);
setDialogOpen(true);
}, []);
return (
<>
<SettingsSection
title="Review Watches"
description="Automatically create tasks for PRs that need your review."
action={
<Button
size="sm"
onClick={() => {
setEditingWatch(null);
setDialogOpen(true);
}}
className="cursor-pointer"
>
<IconPlus className="h-4 w-4 mr-1" />
Add Watch
</Button>
}
>
<Card>
<CardContent className="p-0">
<ReviewWatchTable
watches={watches}
onEdit={handleEdit}
onDelete={handleDelete}
onTrigger={handleTrigger}
onToggleEnabled={handleToggleEnabled}
/>
</CardContent>
</Card>
</SettingsSection>
<ReviewWatchDialog
open={dialogOpen}
onOpenChange={setDialogOpen}
watch={editingWatch}
workspaceId={workspaceId}
onCreate={async (req) => {
await create(req);
toast({ description: "Review watch created", variant: "success" });
}}
onUpdate={async (id, req) => {
await update(id, req);
toast({ description: "Review watch updated", variant: "success" });
}}
/>
</>
);
}
function IssueWatchSection({ workspaceId }: { workspaceId: string }) {
const issueActions = useIssueWatchActions(workspaceId);
const { toast } = useToast();
const [dialogOpen, setDialogOpen] = useState(false);
const [editingWatch, setEditingIssueWatch] = useState<IssueWatch | null>(null);
const handleEdit = useCallback((watch: IssueWatch) => {
setEditingIssueWatch(watch);
setDialogOpen(true);
}, []);
return (
<>
<SettingsSection
title="Issue Watches"
description="Automatically create tasks for GitHub issues matching your criteria."
action={
<Button
size="sm"
onClick={() => {
setEditingIssueWatch(null);
setDialogOpen(true);
}}
className="cursor-pointer"
>
<IconPlus className="h-4 w-4 mr-1" />
Add Watch
</Button>
}
>
<Card>
<CardContent className="p-0">
<IssueWatchTable
watches={issueActions.watches}
onEdit={handleEdit}
onDelete={issueActions.handleDelete}
onTrigger={issueActions.handleTrigger}
onToggleEnabled={issueActions.handleToggleEnabled}
/>
</CardContent>
</Card>
</SettingsSection>
<IssueWatchDialog
open={dialogOpen}
onOpenChange={setDialogOpen}
watch={editingWatch}
workspaceId={workspaceId}
onCreate={async (req) => {
await issueActions.create(req);
toast({ description: "Issue watch created", variant: "success" });
}}
onUpdate={async (id, req) => {
await issueActions.update(id, req);
toast({ description: "Issue watch updated", variant: "success" });
}}
/>
</>
);
}