"use client"; import { useState, useEffect, useCallback, useMemo } from "react"; import { Button } from "@kandev/ui/button"; import { Switch } from "@kandev/ui/switch"; import { Label } from "@kandev/ui/label"; import { Input } from "@kandev/ui/input"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from "@kandev/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@kandev/ui/select"; import { Textarea } from "@kandev/ui/textarea"; import { IconInfoCircle } from "@tabler/icons-react"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@kandev/ui/tooltip"; import { CliModeIcon } from "@/components/cli-mode-icon"; import { useAppStore } from "@/components/state-provider"; import { useSettingsData } from "@/hooks/domains/settings/use-settings-data"; import { useWorkflows } from "@/hooks/use-workflows"; import { useWorkflowSteps, stepPlaceholder, type WorkflowStepOption, } from "@/hooks/use-workflow-steps"; import { DEFAULT_REVIEW_WATCH_PROMPT } from "@/components/github/review-watch-placeholders"; import { ReviewWatchPromptField } from "@/components/github/review-watch-prompt-field"; import { RepoFilterSelector } from "@/components/github/repo-filter-selector"; import { STEP_DEFAULT, STEP_DEFAULT_LABEL, resolveProfileId } from "@/lib/watcher-profile-default"; import type { RepoFilter, ReviewWatch, CreateReviewWatchRequest, UpdateReviewWatchRequest, CleanupPolicy, } from "@/lib/types/github"; type ReviewWatchDialogProps = { open: boolean; onOpenChange: (open: boolean) => void; watch: ReviewWatch | null; // Pre-binds the dialog to one workspace; omit on the install-wide settings // page so the create flow shows a workspace picker. workspaceId?: string; onCreate: (req: CreateReviewWatchRequest) => Promise; onUpdate: (id: string, req: UpdateReviewWatchRequest) => Promise; }; const QUERY_TEMPLATES = { meAndTeams: "type:pr state:open review-requested:@me -is:draft", me: "type:pr state:open user-review-requested:@me -is:draft", } as const; type FormState = { workspaceId: string; selectedRepos: RepoFilter[]; allRepos: boolean; workflowId: string; workflowStepId: string; agentProfileId: string; executorProfileId: string; prompt: string; customQuery: string; enabled: boolean; pollInterval: number; cleanupPolicy: CleanupPolicy; }; function makeDefaultForm(workspaceId: string): FormState { return { workspaceId, selectedRepos: [], allRepos: false, workflowId: "", workflowStepId: "", agentProfileId: "", executorProfileId: "", prompt: DEFAULT_REVIEW_WATCH_PROMPT, customQuery: QUERY_TEMPLATES.meAndTeams, enabled: true, pollInterval: 300, cleanupPolicy: "auto", }; } function formStateFromWatch(watch: ReviewWatch): FormState { const hasRepos = watch.repos && watch.repos.length > 0; return { workspaceId: watch.workspace_id, selectedRepos: hasRepos ? watch.repos : [], allRepos: !hasRepos, workflowId: watch.workflow_id, workflowStepId: watch.workflow_step_id, agentProfileId: watch.agent_profile_id, executorProfileId: watch.executor_profile_id, prompt: watch.prompt || DEFAULT_REVIEW_WATCH_PROMPT, customQuery: watch.custom_query || QUERY_TEMPLATES.meAndTeams, enabled: watch.enabled, pollInterval: watch.poll_interval_seconds, cleanupPolicy: watch.cleanup_policy ?? "auto", }; } const CLEANUP_POLICY_OPTIONS: Array<{ id: CleanupPolicy; label: string; description: string }> = [ { id: "auto", label: "Auto (recommended)", description: "Delete merged/closed PR tasks unless you typed a message in them.", }, { id: "always", label: "Always delete", description: "Delete on merge/close even if you engaged with the task.", }, { id: "never", label: "Never auto-delete", description: "Keep all tasks. Delete them manually from the task list.", }, ]; // --- Generic select field with description --- type SelectFieldItem = { id: string; label: string; icon?: React.ReactNode }; type SelectFieldProps = { label: string; description?: string; value: string; onChange: (value: string) => void; placeholder: string; items: SelectFieldItem[]; disabled?: boolean; }; function SelectField({ label, description, value, onChange, placeholder, items, disabled, }: SelectFieldProps) { return (
{description &&

{description}

}
); } function WorkspacePicker({ value, onChange, disabled, }: { value: string; onChange: (v: string) => void; disabled?: boolean; }) { const workspaces = useAppStore((s) => s.workspaces.items); return ( ({ id: w.id, label: w.name }))} disabled={disabled} /> ); } // --- Hooks --- function useWatchFormData(workspaceId: string) { useSettingsData(true); useWorkflows(workspaceId, true); const allWorkflows = useAppStore((state) => state.workflows.items); const workflows = useMemo(() => allWorkflows.filter((w) => !w.hidden), [allWorkflows]); const agentProfiles = useAppStore((state) => state.agentProfiles.items); const executors = useAppStore((state) => state.executors.items); const allExecutorProfiles = useMemo( () => executors .filter((e) => e.type !== "local" && e.type !== "local_pc") .flatMap((e) => e.profiles ?? []), [executors], ); return { workflows, agentProfiles, allExecutorProfiles }; } // --- Section header --- function SectionHeader({ children }: { children: React.ReactNode }) { return (
{children}
); } // --- Form field groups --- function QueryField({ form, setForm, }: { form: FormState; setForm: React.Dispatch>; }) { const current = form.customQuery.trim(); const isMeAndTeams = current === QUERY_TEMPLATES.meAndTeams; const isMe = current === QUERY_TEMPLATES.me; return (
PRs where you or any of your teams are requested as reviewers Only PRs where you are explicitly requested as a reviewer (not via team membership)