"use client"; import { useTranslation } from "react-i18next"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@kandev/ui/dialog"; import { Button } from "@kandev/ui/button"; import { Input } from "@kandev/ui/input"; import { Label } from "@kandev/ui/label"; import { Switch } from "@kandev/ui/switch"; import type { TFunction } from "i18next"; import type { WorkflowSyncController, WorkflowSyncFormState, } from "@/hooks/domains/settings/use-workflow-sync"; const WORKFLOW_EXPORT_FORMAT = "kandev_workflow"; const WORKFLOW_EXPORT_EXTENSIONS = ".yml/.yaml/.json"; type RepoUrlFieldProps = { url: string; invalid: boolean; resolved: string; onChange: (value: string) => void; }; // RepoUrlField is the primary input: a full GitHub link (optionally with // /tree//) that resolves into the stored owner, repo, // branch, and directory. The resolved target is echoed underneath so the // structured fields stay visible to the user. function RepoUrlField({ url, invalid, resolved, onChange }: RepoUrlFieldProps) { const { t } = useTranslation(); return (
onChange(e.target.value)} aria-invalid={invalid} /> {invalid ? (

{t("workflows:notARecognizedGitHubLink")}

) : (

{resolved || t("workflows:pasteGitHubLinkHint")}

)}
); } type FieldsProps = { form: WorkflowSyncFormState; update: (key: K, value: WorkflowSyncFormState[K]) => void; }; // PollFields is a single compact row: the auto-sync switch and, when on, the // interval right beside it. The branch needs no field of its own — it comes // from the pasted link (or defaults to main) and is echoed in the resolved // summary under the link input. function PollFields({ form, update }: FieldsProps) { const { t } = useTranslation(); return (
update("poll_enabled", checked)} className="cursor-pointer" /> {form.poll_enabled && (
update("interval_seconds", Number(e.target.value) || 0)} /> {t("workflows:seconds")}
)}

{form.poll_enabled ? t("workflows:pollEnabledHint") : t("workflows:pollDisabledHint")}

); } type WorkflowSyncDialogProps = { open: boolean; onOpenChange: (open: boolean) => void; sync: WorkflowSyncController; }; // WorkflowSyncDialog holds the GitHub Sync configuration form. It closes // itself after a successful save or removal; failures keep it open with the // error surfaced via toast. export function WorkflowSyncDialog({ open, onOpenChange, sync }: WorkflowSyncDialogProps) { const { t } = useTranslation(); const hasConfig = !!sync.config; const intervalInvalid = sync.form.poll_enabled && (!Number.isInteger(sync.form.interval_seconds) || sync.form.interval_seconds < 60); const disableSave = sync.saving || sync.loading || sync.urlInvalid || intervalInvalid || !sync.form.repo_owner.trim() || !sync.form.repo_name.trim(); const resolved = sync.form.repo_owner ? t("workflows:syncResolvedTarget", { owner: sync.form.repo_owner, repo: sync.form.repo_name, // `main` is git's default branch name, not copy. branch: sync.form.branch || "main", directory: sync.form.path || t("workflows:repositoryRoot"), }) : ""; const handleSave = async () => { if (await sync.handleSave()) onOpenChange(false); }; const handleRemove = async () => { if (await sync.handleDelete()) onOpenChange(false); }; return ( {t("workflows:githubSync")} {t("workflows:githubSyncDescription")}

{t("workflows:syncDirectoryHelp", { format: WORKFLOW_EXPORT_FORMAT, extensions: WORKFLOW_EXPORT_EXTENSIONS, })}

{hasConfig && ( )}
); } function saveLabel(t: TFunction, saving: boolean, hasConfig: boolean): string { if (saving) return t("workflows:saving"); return hasConfig ? t("workflows:update") : t("workflows:save"); }