"use client";
import { useEffect, useState, type Dispatch, type SetStateAction } from "react";
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";
import type { WorkflowSyncProvider } from "@/lib/types/workflow-sync";
import { RemoteRepoProviderTabs } from "@/components/task-create-dialog-remote-repo-provider-tabs";
import { InlineConfirmActions } from "@/components/confirmation/inline-confirm-actions";
import type { RemoteRepositoryProvider } from "@/hooks/domains/integrations/use-remote-repositories";
const WORKFLOW_EXPORT_FORMAT = "kandev_workflow";
const WORKFLOW_EXPORT_EXTENSIONS = ".yml/.yaml/.json";
const SYNC_PROVIDERS: RemoteRepositoryProvider[] = ["github", "gitlab"];
const REMOVE_TEST_ID = "workflow-sync-remove";
const REMOVE_CONFIRMATION_TEST_ID = "workflow-sync-remove-confirmation";
const REMOVE_CONFIRM_TEST_ID = "workflow-sync-remove-confirm";
type ProviderFieldProps = {
provider: WorkflowSyncProvider;
onChange: (provider: WorkflowSyncProvider) => void;
};
// ProviderField lets the user pick which host workflow sync reads from.
// Only one source syncs per workspace — picking a different provider than
// the currently-saved config replaces it on the next save (see the warning
// surfaced in WorkflowSyncDialog).
function ProviderField({ provider, onChange }: ProviderFieldProps) {
const { t } = useTranslation();
return (
onChange(next as WorkflowSyncProvider)}
/>
);
}
type RepoUrlFieldProps = {
provider: WorkflowSyncProvider;
url: string;
invalid: boolean;
resolved: string;
onChange: (value: string) => void;
};
// RepoUrlField captures the repository identity — owner/repo for GitHub,
// project_path for GitLab. Pasting a full link (including a /tree/.../-/tree/
// suffix) still convenience-fills Branch and Directory below, but those are
// their own directly-editable fields: a branch name can itself contain
// slashes (e.g. "features/my-ticket"), which makes splitting a combined
// "project/branch/path" string ambiguous by construction — no client-side
// parse can always place that boundary correctly. If a paste gets it wrong,
// fix it directly in Branch/Directory rather than needing a different link.
// GitLab sync has no host of its own (it always uses the workspace's
// configured GitLab connection, including self-managed instances), so a
// saved GitLab config redisplays as a bare "group/project" reference.
function RepoUrlField({ provider, url, invalid, resolved, onChange }: RepoUrlFieldProps) {
const { t } = useTranslation();
const providerLabel = provider === "gitlab" ? "GitLab" : "GitHub";
// GitLab's placeholder is a bare path, not a URL: a full link (any host —
// gitlab.com or a self-managed instance) is accepted, but nothing here
// needs a host at all, since syncing always uses this workspace's own
// connected GitLab host regardless of what's typed in this field. Showing
// a "gitlab.com" example would wrongly suggest a self-managed host has to
// match it.
const placeholder = provider === "gitlab" ? "group/project" : "https://github.com/kdlbs/kandev";
const hint =
provider === "gitlab"
? t("workflows:pasteGitLabPathHint")
: t("workflows:pasteRepositoryLinkHint", { provider: providerLabel });
return (
);
}
type FieldsProps = {
form: WorkflowSyncFormState;
update: (key: K, value: WorkflowSyncFormState[K]) => void;
};
// BranchDirectoryFields exposes branch and directory as their own inputs
// rather than only ever deriving them from a parsed link. A pasted link
// still convenience-fills both, but a branch name with a slash in it (a
// common convention — "features/TICKET-123") can't always be told apart
// from the directory that follows it, so these fields are the way to
// directly correct a wrong guess instead of needing a differently-shaped
// link.
function BranchDirectoryFields({ form, update }: FieldsProps) {
const { t } = useTranslation();
const directoryPlaceholder = ".kandev/workflows";
return (
update("branch", e.target.value)}
/>
update("path", e.target.value)}
/>
);
}
// PollFields is a single compact row: the auto-sync switch and, when on, the
// interval right beside it.
function PollFields({ form, update }: FieldsProps) {
const { t } = useTranslation();
return (