|
| 1 | +import { useState } from "react"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import type { AppConfig, PasteMode, SnippetLibraryItem } from "../../lib/config"; |
| 4 | +import type { ConfirmModalRequest } from "../ConfirmModal"; |
| 5 | +import { |
| 6 | + upsertSnippetLibraryItem, |
| 7 | + removeSnippetLibraryItem, |
| 8 | + snippetReferenceCount, |
| 9 | + makeSnippetId, |
| 10 | + nextUniqueId, |
| 11 | +} from "../../lib/config-editing"; |
| 12 | +import { SelectField } from "../shared"; |
| 13 | +import { TrashIcon } from "../icons"; |
| 14 | + |
| 15 | +export interface SnippetLibrarySettingsProps { |
| 16 | + activeConfig: AppConfig; |
| 17 | + updateDraft: (updater: (config: AppConfig) => AppConfig) => void; |
| 18 | + setConfirmModal: (modal: ConfirmModalRequest | null) => void; |
| 19 | +} |
| 20 | + |
| 21 | +/** Snippets tab: full CRUD over the reusable snippet library — |
| 22 | + * add / rename / edit text + paste mode / delete. Selected snippet edits |
| 23 | + * on top, the whole library lists below (mirrors the Profiles tab). */ |
| 24 | +export function SnippetLibrarySettings({ |
| 25 | + activeConfig, |
| 26 | + updateDraft, |
| 27 | + setConfirmModal, |
| 28 | +}: SnippetLibrarySettingsProps) { |
| 29 | + const { t } = useTranslation(); |
| 30 | + const [selectedId, setSelectedId] = useState<string | null>(null); |
| 31 | + |
| 32 | + const library = activeConfig.snippetLibrary; |
| 33 | + const selected = library.find((s) => s.id === selectedId) ?? null; |
| 34 | + |
| 35 | + const pasteOptions: ReadonlyArray<{ value: PasteMode; label: string }> = [ |
| 36 | + { value: "clipboardPaste", label: t("paste.clipboard") }, |
| 37 | + { value: "sendText", label: t("paste.direct") }, |
| 38 | + ]; |
| 39 | + |
| 40 | + function patch(id: string, fields: Partial<SnippetLibraryItem>) { |
| 41 | + updateDraft((c) => { |
| 42 | + const current = c.snippetLibrary.find((s) => s.id === id); |
| 43 | + if (!current) return c; |
| 44 | + return upsertSnippetLibraryItem(c, { ...current, ...fields }); |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + function handleAdd() { |
| 49 | + const name = t("snippetLibrary.newSnippetName"); |
| 50 | + const id = nextUniqueId( |
| 51 | + activeConfig.snippetLibrary.map((s) => s.id), |
| 52 | + makeSnippetId(name), |
| 53 | + ); |
| 54 | + updateDraft((c) => |
| 55 | + upsertSnippetLibraryItem(c, { id, name, text: "", pasteMode: "clipboardPaste", tags: [] }), |
| 56 | + ); |
| 57 | + setSelectedId(id); |
| 58 | + } |
| 59 | + |
| 60 | + function handleDelete(snippet: SnippetLibraryItem) { |
| 61 | + const refs = snippetReferenceCount(activeConfig, snippet.id); |
| 62 | + setConfirmModal({ |
| 63 | + title: t("snippetLibrary.deleteConfirmTitle"), |
| 64 | + message: |
| 65 | + refs > 0 |
| 66 | + ? t("snippetLibrary.deleteConfirmReferenced", { name: snippet.name, count: refs }) |
| 67 | + : t("snippetLibrary.deleteConfirmMessage", { name: snippet.name }), |
| 68 | + confirmLabel: t("common.delete"), |
| 69 | + danger: true, |
| 70 | + onConfirm: () => { |
| 71 | + updateDraft((c) => removeSnippetLibraryItem(c, snippet.id)); |
| 72 | + if (selectedId === snippet.id) setSelectedId(null); |
| 73 | + setConfirmModal(null); |
| 74 | + }, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + return ( |
| 79 | + <> |
| 80 | + {selected ? ( |
| 81 | + <section className="settings-section"> |
| 82 | + <div className="settings-editor"> |
| 83 | + <div className="settings-editor__title">{selected.name}</div> |
| 84 | + |
| 85 | + <label className="field"> |
| 86 | + <span className="field__label">{t("snippet.name")}</span> |
| 87 | + <input |
| 88 | + type="text" |
| 89 | + value={selected.name} |
| 90 | + onChange={(e) => patch(selected.id, { name: e.target.value })} |
| 91 | + onBlur={(e) => { |
| 92 | + if (!e.target.value.trim()) { |
| 93 | + patch(selected.id, { name: t("snippetLibrary.newSnippetName") }); |
| 94 | + } |
| 95 | + }} |
| 96 | + /> |
| 97 | + </label> |
| 98 | + |
| 99 | + <label className="field"> |
| 100 | + <span className="field__label">{t("snippet.text")}</span> |
| 101 | + <textarea |
| 102 | + rows={4} |
| 103 | + value={selected.text} |
| 104 | + onChange={(e) => patch(selected.id, { text: e.target.value })} |
| 105 | + placeholder={t("picker.textPlaceholder")} |
| 106 | + /> |
| 107 | + </label> |
| 108 | + |
| 109 | + <SelectField<PasteMode> |
| 110 | + label={t("snippetLibrary.pasteModeLabel")} |
| 111 | + value={selected.pasteMode} |
| 112 | + options={pasteOptions} |
| 113 | + onChange={(mode) => patch(selected.id, { pasteMode: mode })} |
| 114 | + /> |
| 115 | + |
| 116 | + <div className="settings-actions"> |
| 117 | + <button |
| 118 | + type="button" |
| 119 | + className="action-button action-button--small action-button--danger" |
| 120 | + onClick={() => handleDelete(selected)} |
| 121 | + > |
| 122 | + <TrashIcon /> |
| 123 | + {t("common.delete")} |
| 124 | + </button> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </section> |
| 128 | + ) : null} |
| 129 | + |
| 130 | + <section className="settings-section"> |
| 131 | + <div className="settings-section__header"> |
| 132 | + <span className="settings-section__title">{t("snippet.eyebrow")}</span> |
| 133 | + <span className="settings-section__count">{library.length}</span> |
| 134 | + </div> |
| 135 | + |
| 136 | + {library.length === 0 ? ( |
| 137 | + <p className="panel__muted">{t("snippetLibrary.emptyList")}</p> |
| 138 | + ) : ( |
| 139 | + <div className="settings-profile-list"> |
| 140 | + {library.map((snippet) => { |
| 141 | + const refs = snippetReferenceCount(activeConfig, snippet.id); |
| 142 | + return ( |
| 143 | + <div |
| 144 | + key={snippet.id} |
| 145 | + className={`settings-profile-card${snippet.id === selectedId ? " settings-profile-card--active" : ""}`} |
| 146 | + > |
| 147 | + <button |
| 148 | + type="button" |
| 149 | + className="settings-profile-card__info" |
| 150 | + aria-pressed={snippet.id === selectedId} |
| 151 | + onClick={() => setSelectedId(snippet.id)} |
| 152 | + > |
| 153 | + <span className="settings-profile-card__name">{snippet.name}</span> |
| 154 | + <span className="settings-profile-card__meta"> |
| 155 | + {t("snippet.usageCount", { count: refs })} |
| 156 | + </span> |
| 157 | + </button> |
| 158 | + </div> |
| 159 | + ); |
| 160 | + })} |
| 161 | + </div> |
| 162 | + )} |
| 163 | + |
| 164 | + <div className="settings-actions mt-12"> |
| 165 | + <button type="button" className="action-button" onClick={handleAdd}> |
| 166 | + {t("snippetLibrary.addButton")} |
| 167 | + </button> |
| 168 | + </div> |
| 169 | + </section> |
| 170 | + </> |
| 171 | + ); |
| 172 | +} |
0 commit comments