"use client"; import { useCallback, useRef, useState, type RefObject } from "react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@kandev/ui/dialog"; import { Input } from "@kandev/ui/input"; import { Button } from "@kandev/ui/button"; import { Label } from "@kandev/ui/label"; import { RepoFilterCombobox } from "./repo-filter-combobox"; import { useTranslation } from "react-i18next"; import { createFocusReturnHandler } from "@/lib/dialog-focus-return"; type SavePresetDialogProps = { open: boolean; onOpenChange: (open: boolean) => void; kind: "pr" | "issue"; customQuery: string; repoFilter: string; repoOptions: string[]; suggestedLabel: string; onSave: (label: string, repoFilter: string) => Promise; focusReturnRef?: RefObject; }; function SavePresetForm({ kind, customQuery, repoFilter, repoOptions, suggestedLabel, onSave, onClose, pending, }: { kind: "pr" | "issue"; customQuery: string; repoFilter: string; repoOptions: string[]; suggestedLabel: string; onSave: (label: string, repoFilter: string) => void; onClose: () => void; pending: boolean; }) { const { t } = useTranslation(); const [value, setValue] = useState(suggestedLabel); const [defaultRepoFilter, setDefaultRepoFilter] = useState(repoFilter); const trimmed = value.trim(); const canSubmit = trimmed.length > 0 && !pending; const handleSubmit = useCallback(() => { if (!canSubmit) return; onSave(trimmed, defaultRepoFilter); }, [canSubmit, trimmed, defaultRepoFilter, onSave]); return ( <> {t("github:saveQuery")} {t("github:savePresetDescription", { kind: kind === "pr" ? t("github:kindPullRequest") : t("github:kindIssue"), })}
setValue(e.target.value)} onFocus={(e) => e.target.select()} placeholder={t("github:eGNeedsMyReview")} />
{customQuery && (
{t("github:query")} {customQuery}
)}

{t("github:thisRepositoryOpensByDefaultYou")}

{pending && (

{t("github:saving")}

)} ); } export function SavePresetDialog({ open, onOpenChange, kind, customQuery, repoFilter, repoOptions, suggestedLabel, onSave, focusReturnRef, }: SavePresetDialogProps) { const [pending, setPending] = useState(false); const pendingRef = useRef(false); const handleOpenChange = useCallback( (nextOpen: boolean) => { if (!pendingRef.current) onOpenChange(nextOpen); }, [onOpenChange], ); const handleClose = useCallback(() => handleOpenChange(false), [handleOpenChange]); const handleSave = async (label: string, repo: string) => { if (pendingRef.current) return; pendingRef.current = true; setPending(true); try { if (await onSave(label, repo)) onOpenChange(false); } finally { pendingRef.current = false; setPending(false); } }; return ( {open && ( )} ); }