"use client"; import { useCallback, useMemo, useState } from "react"; 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 { Combobox } from "@/components/combobox"; import { useTranslation } from "react-i18next"; const ALL_REPOSITORIES = "__all__"; export type IntegrationSaveQueryDialogProps = { open: boolean; onOpenChange(open: boolean): void; title?: string; description: string; suggestedLabel: string; query?: string; repositoryId?: string; repositoryOptions?: Array<{ value: string; label: string }>; onSave(label: string, repositoryId: string): void | Promise; }; function SavedQueryDetails({ query, repositoryOptions, options, repositoryId, onRepositoryChange, }: { query?: string; repositoryOptions: Array<{ value: string; label: string }>; options: Array<{ value: string; label: string }>; repositoryId: string; onRepositoryChange(value: string): void; }) { const { t } = useTranslation(); return ( <> {query ? (
{t("integrations:query")} {query}
) : null} {repositoryOptions.length > 0 ? (
{ if (value) onRepositoryChange(value === ALL_REPOSITORIES ? "" : value); }} options={options} ariaLabel={t("integrations:defaultRepository")} placeholder={t("integrations:allRepos")} searchPlaceholder={t("integrations:filterRepositories")} emptyMessage={t("integrations:noRepositoriesFound")} triggerClassName="h-11 border border-input bg-background px-3 py-2 text-sm hover:bg-secondary/50 md:h-9 md:py-1.5" testId="integration-save-query-repository-trigger" dropdownTestId="integration-save-query-repository-dropdown" />
) : null} ); } function SaveQueryForm({ description, suggestedLabel, query, repositoryId, repositoryOptions = [], onSave, onClose, }: Omit & { onClose(): void; }) { const { t } = useTranslation(); const [label, setLabel] = useState(suggestedLabel); const [defaultRepositoryId, setDefaultRepositoryId] = useState(repositoryId ?? ""); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const options = useMemo( () => [{ value: ALL_REPOSITORIES, label: t("integrations:allRepos") }, ...repositoryOptions], [repositoryOptions, t], ); const trimmedLabel = label.trim(); const handleSave = useCallback(async () => { if (!trimmedLabel || saving) return; setSaving(true); setError(null); try { await onSave(trimmedLabel, defaultRepositoryId); onClose(); } catch (reason) { setError(reason instanceof Error ? reason.message : t("integrations:unknownError")); } finally { setSaving(false); } }, [defaultRepositoryId, onClose, onSave, saving, t, trimmedLabel]); return ( <> {t("integrations:saveQuery")} {description}
setLabel(event.target.value)} onFocus={(event) => event.target.select()} placeholder={t("integrations:queryNamePlaceholder")} />
{error ? (

{error}

) : null}
); } export function IntegrationSaveQueryDialog({ open, onOpenChange, title, ...props }: IntegrationSaveQueryDialogProps) { const close = useCallback(() => onOpenChange(false), [onOpenChange]); return ( {open ? : null} ); }