import React, { useState } from "react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import { useSettings } from "../../hooks/useSettings"; import { Input } from "../ui/Input"; import { Button } from "../ui/Button"; import { SettingContainer } from "../ui/SettingContainer"; interface CustomWordsProps { descriptionMode?: "inline" | "tooltip"; grouped?: boolean; } export const CustomWords: React.FC = React.memo( ({ descriptionMode = "tooltip", grouped = false }) => { const { t } = useTranslation(); const { getSetting, updateSetting, isUpdating } = useSettings(); const [newWord, setNewWord] = useState(""); const customWords = getSetting("custom_words") || []; const handleAddWord = () => { const trimmedWord = newWord.trim(); const sanitizedWord = trimmedWord.replace(/[<>"'&]/g, ""); if ( sanitizedWord && !sanitizedWord.includes(" ") && sanitizedWord.length <= 50 ) { if (customWords.includes(sanitizedWord)) { toast.error( t("settings.advanced.customWords.duplicate", { word: sanitizedWord, }), ); return; } updateSetting("custom_words", [...customWords, sanitizedWord]); setNewWord(""); } }; const handleRemoveWord = (wordToRemove: string) => { updateSetting( "custom_words", customWords.filter((word) => word !== wordToRemove), ); }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault(); handleAddWord(); } }; return ( <>
setNewWord(e.target.value)} onKeyDown={handleKeyPress} placeholder={t("settings.advanced.customWords.placeholder")} variant="compact" disabled={isUpdating("custom_words")} />
{customWords.length > 0 && (
{customWords.map((word) => ( ))}
)} ); }, );