"use client"; import { useMemo, useRef } from "react"; import { useTranslations } from "next-intl"; import { useSettingsStore } from "@/stores/settings-store"; import { SettingsSection } from "./settings-section"; import { RotateCcw } from "lucide-react"; import { cn } from "@/lib/utils"; import { DEFAULT_ATTACHMENT_TEMPLATE, DEFAULT_EMAIL_TEMPLATE, EMAIL_TOKENS, ATTACHMENT_TOKENS, emailExportFilename, attachmentDownloadFilename, buildSampleEmail, } from "@/lib/download-filename"; function insertTokenAtCursor( input: HTMLInputElement, token: string, current: string, onChange: (next: string) => void, ): void { const start = input.selectionStart ?? current.length; const end = input.selectionEnd ?? current.length; const before = current.slice(0, start); const after = current.slice(end); const insertion = `{${token}}`; const next = `${before}${insertion}${after}`; onChange(next); // Restore focus and place caret after the inserted token. requestAnimationFrame(() => { input.focus(); const caret = before.length + insertion.length; input.setSelectionRange(caret, caret); }); } interface TemplateEditorProps { label: string; description: string; value: string; defaultValue: string; tokens: { token: string; description: string }[]; preview: string; onChange: (next: string) => void; resetLabel: string; placeholder?: string; } function TemplateEditor({ label, description, value, defaultValue, tokens, preview, onChange, resetLabel, placeholder, }: TemplateEditorProps) { const inputRef = useRef(null); return (

{description}

onChange(e.target.value)} spellCheck={false} className="flex-1 px-3 py-1.5 text-sm rounded-md bg-muted border border-border text-foreground font-mono focus:outline-none focus:ring-2 focus:ring-ring transition-colors duration-150" />
{tokens.map((t) => ( ))}
Preview: {preview}
); } export function DownloadsSettings() { const t = useTranslations("settings.downloads"); const { emailDownloadTemplate, attachmentDownloadTemplate, updateSetting } = useSettingsStore(); const sampleEmail = useMemo(() => buildSampleEmail(), []); const sampleAttachment = useMemo(() => ({ name: "Invoice-2026-05.pdf", type: "application/pdf" }), []); const emlPreview = useMemo( () => emailExportFilename(sampleEmail, emailDownloadTemplate || DEFAULT_EMAIL_TEMPLATE), [sampleEmail, emailDownloadTemplate], ); const attachmentPreview = useMemo( () => attachmentDownloadFilename( sampleEmail, sampleAttachment, attachmentDownloadTemplate || DEFAULT_ATTACHMENT_TEMPLATE, ), [sampleEmail, sampleAttachment, attachmentDownloadTemplate], ); return ( updateSetting("emailDownloadTemplate", next)} resetLabel={t("reset")} placeholder={DEFAULT_EMAIL_TEMPLATE} /> updateSetting("attachmentDownloadTemplate", next)} resetLabel={t("reset")} placeholder={DEFAULT_ATTACHMENT_TEMPLATE} /> ); }