Files
kandev/apps/web/lib/state/slices/github/github-slice.ts
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

175 lines
4.9 KiB
TypeScript

import type { StateCreator } from "zustand";
import type { GitHubSlice, GitHubSliceState } from "./types";
export const defaultGitHubState: GitHubSliceState = {
githubStatus: { status: null, loaded: false, loading: false },
taskPRs: { byTaskId: {}, loaded: false, loading: false },
prWatches: { items: [], loaded: false, loading: false },
reviewWatches: { items: [], loaded: false, loading: false },
issueWatches: { items: [], loaded: false, loading: false },
actionPresets: { byWorkspaceId: {}, loading: {} },
};
type ImmerSet = Parameters<
StateCreator<GitHubSlice, [["zustand/immer", never]], [], GitHubSlice>
>[0];
function createGitHubStatusActions(
set: ImmerSet,
): Pick<GitHubSlice, "setGitHubStatus" | "setGitHubStatusLoading"> {
return {
setGitHubStatus: (status) =>
set((draft) => {
draft.githubStatus.status = status;
draft.githubStatus.loaded = true;
}),
setGitHubStatusLoading: (loading) =>
set((draft) => {
draft.githubStatus.loading = loading;
}),
};
}
function createTaskPRActions(
set: ImmerSet,
): Pick<GitHubSlice, "setTaskPRs" | "setTaskPR" | "removeTaskPR" | "setTaskPRsLoading"> {
return {
setTaskPRs: (prs) =>
set((draft) => {
draft.taskPRs.byTaskId = prs;
draft.taskPRs.loaded = true;
}),
setTaskPR: (taskId, pr) =>
set((draft) => {
draft.taskPRs.byTaskId[taskId] = pr;
}),
removeTaskPR: (taskId) =>
set((draft) => {
delete draft.taskPRs.byTaskId[taskId];
}),
setTaskPRsLoading: (loading) =>
set((draft) => {
draft.taskPRs.loading = loading;
}),
};
}
function createWatchActions(
set: ImmerSet,
): Pick<
GitHubSlice,
| "setPRWatches"
| "setPRWatchesLoading"
| "removePRWatch"
| "setReviewWatches"
| "setReviewWatchesLoading"
| "addReviewWatch"
| "updateReviewWatch"
| "removeReviewWatch"
| "setIssueWatches"
| "setIssueWatchesLoading"
| "addIssueWatch"
| "updateIssueWatch"
| "removeIssueWatch"
> {
return {
setPRWatches: (watches) =>
set((draft) => {
draft.prWatches.items = watches;
draft.prWatches.loaded = true;
}),
setPRWatchesLoading: (loading) =>
set((draft) => {
draft.prWatches.loading = loading;
}),
removePRWatch: (id) =>
set((draft) => {
draft.prWatches.items = draft.prWatches.items.filter((w) => w.id !== id);
}),
setReviewWatches: (watches) =>
set((draft) => {
draft.reviewWatches.items = watches;
draft.reviewWatches.loaded = true;
}),
setReviewWatchesLoading: (loading) =>
set((draft) => {
draft.reviewWatches.loading = loading;
}),
addReviewWatch: (watch) =>
set((draft) => {
draft.reviewWatches.items = [
...draft.reviewWatches.items.filter((w) => w.id !== watch.id),
watch,
];
draft.reviewWatches.loaded = true;
}),
updateReviewWatch: (watch) =>
set((draft) => {
const idx = draft.reviewWatches.items.findIndex((w) => w.id === watch.id);
if (idx >= 0) {
draft.reviewWatches.items[idx] = watch;
}
}),
removeReviewWatch: (id) =>
set((draft) => {
draft.reviewWatches.items = draft.reviewWatches.items.filter((w) => w.id !== id);
}),
setIssueWatches: (watches) =>
set((draft) => {
draft.issueWatches.items = watches;
draft.issueWatches.loaded = true;
}),
setIssueWatchesLoading: (loading) =>
set((draft) => {
draft.issueWatches.loading = loading;
}),
addIssueWatch: (watch) =>
set((draft) => {
draft.issueWatches.items = [
...draft.issueWatches.items.filter((w) => w.id !== watch.id),
watch,
];
draft.issueWatches.loaded = true;
}),
updateIssueWatch: (watch) =>
set((draft) => {
const idx = draft.issueWatches.items.findIndex((w) => w.id === watch.id);
if (idx >= 0) {
draft.issueWatches.items[idx] = watch;
}
}),
removeIssueWatch: (id) =>
set((draft) => {
draft.issueWatches.items = draft.issueWatches.items.filter((w) => w.id !== id);
}),
};
}
function createActionPresetActions(
set: ImmerSet,
): Pick<GitHubSlice, "setActionPresets" | "setActionPresetsLoading"> {
return {
setActionPresets: (workspaceId, presets) =>
set((draft) => {
draft.actionPresets.byWorkspaceId[workspaceId] = presets;
}),
setActionPresetsLoading: (workspaceId, loading) =>
set((draft) => {
draft.actionPresets.loading[workspaceId] = loading;
}),
};
}
export const createGitHubSlice: StateCreator<
GitHubSlice,
[["zustand/immer", never]],
[],
GitHubSlice
> = (set) => ({
...defaultGitHubState,
...createGitHubStatusActions(set),
...createTaskPRActions(set),
...createWatchActions(set),
...createActionPresetActions(set),
});