import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { useClosePlayerModal } from "@/hooks/playerModal"; import { ClipboardPasteIcon, ExternalLinkIcon, Loader2Icon } from "lucide-react"; import { forwardRef, useImperativeHandle, useMemo, useRef, useState } from "react"; import { DropDownSelect, DropDownSelectContent, DropDownSelectItem, DropDownSelectTrigger } from "@/components/dropDownSelect"; import { banDurationToShortString, banDurationToString, cn } from "@/lib/utils"; import { Link, useLocation } from "wouter"; import type { BanTemplatesDataType } from "@shared/otherTypes"; // Consts const reasonTruncateLength = 150; const ADD_NEW_SELECT_OPTION = '!add-new'; const defaultDurations = ['permanent', '2 hours', '8 hours', '1 day', '2 days', '1 week', '2 weeks']; // Types type BanFormRespType = { reason: string; duration: string; } export type BanFormType = HTMLDivElement & { focusReason: () => void; clearData: () => void; getData: () => BanFormRespType; } type BanFormProps = { banTemplates?: BanTemplatesDataType[]; //undefined = loading disabled?: boolean; onNavigateAway?: () => void; }; /** * A form to set ban reason and duration. */ export default forwardRef(function BanForm({ banTemplates, disabled, onNavigateAway }: BanFormProps, ref) { const reasonRef = useRef(null); const customMultiplierRef = useRef(null); const setLocation = useLocation()[1]; const [currentDuration, setCurrentDuration] = useState('2 days'); const [customUnits, setCustomUnits] = useState('days'); const closeModal = useClosePlayerModal(); //Exposing methods to the parent useImperativeHandle(ref, () => { return { getData: () => { return { reason: reasonRef.current?.value.trim(), duration: currentDuration === 'custom' ? `${customMultiplierRef.current?.value} ${customUnits}` : currentDuration, }; }, clearData: () => { if (!reasonRef.current || !customMultiplierRef.current) return; reasonRef.current.value = ''; customMultiplierRef.current.value = ''; setCurrentDuration('2 days'); setCustomUnits('days'); }, focusReason: () => { reasonRef.current?.focus(); } }; }, [reasonRef, customMultiplierRef, currentDuration, customUnits]); const handleTemplateSelectChange = (value: string) => { if (value === ADD_NEW_SELECT_OPTION) { setLocation('/settings/ban-templates'); onNavigateAway?.(); } else { if (!banTemplates) return; const template = banTemplates.find(template => template.id === value); if (!template) return; const processedDuration = banDurationToString(template.duration); if (defaultDurations.includes(processedDuration)) { setCurrentDuration(processedDuration); } else if (typeof template.duration === 'object') { setCurrentDuration('custom'); customMultiplierRef.current!.value = template.duration.value.toString(); setCustomUnits(template.duration.unit); } reasonRef.current!.value = template.reason; setTimeout(() => { reasonRef.current!.focus(); }, 50); } } //Ban templates render optimization const processedTemplates = useMemo(() => { if (!banTemplates) return; return banTemplates.map((template, index) => { const duration = banDurationToShortString(template.duration); const reason = template.reason.length > reasonTruncateLength ? template.reason.slice(0, reasonTruncateLength - 3) + '...' : template.reason; return ( {duration} {reason} ); }); }, [banTemplates]); // Simplifying the jsx below let banTemplatesContentNode: React.ReactNode; if (!Array.isArray(banTemplates)) { banTemplatesContentNode = (
); } else { if (!banTemplates.length) { banTemplatesContentNode = (
You do not have any template configured.
{ closeModal(); }} > Add Ban Template
); } else { banTemplatesContentNode = <> {processedTemplates} Add Ban Template ; } } return (
{banTemplatesContentNode}
); });