"use client"; import { useState } from "react"; import { Pencil, Check } from "lucide-react"; import { cn } from "@/lib/utils"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; interface FormatSelectorProps { value: string | null | undefined; isEditing: boolean; canEdit: boolean; onEdit: () => void; onChange: (value: string | null) => void; onCloseEdit: () => void; } interface FormatOption { label: string; topHours: number; feedHours: number | null; } const FORMAT_OPTIONS: FormatOption[] = [ { label: "1/24", topHours: 1, feedHours: 24 }, { label: "1/48", topHours: 1, feedHours: 48 }, { label: "1/72", topHours: 1, feedHours: 72 }, { label: "1/7 дней", topHours: 1, feedHours: 168 }, { label: "1/без удаления", topHours: 1, feedHours: null }, ]; function parseFormat(format: string | null | undefined): { topHours: number; feedHours: number | null } | null { if (!format) return null; const match = format.match(/^(\d+)\/(\d+(?:\s*дней)?|без\s*удаления)$/i); if (!match) return null; const topHours = parseInt(match[1], 10); let feedHours: number | null = null; if (match[2].toLowerCase().includes('без')) { feedHours = null; } else if (match[2].includes('дней')) { feedHours = parseInt(match[2], 10) * 24; } else { feedHours = parseInt(match[2], 10); } return { topHours, feedHours }; } function formatToString(topHours: number, feedHours: number | null): string { if (feedHours === null) { return "1/без удаления"; } if (feedHours >= 168) { const days = feedHours / 24; return `1/${days} дней`; } return `1/${feedHours}`; } function findMatchingOption(topHours: number, feedHours: number | null): string | null { const option = FORMAT_OPTIONS.find( o => o.topHours === topHours && o.feedHours === feedHours ); return option?.label || null; } function FormatOptionItem({ option, isSelected, onClick, }: { option: FormatOption; isSelected: boolean; onClick: () => void; }) { return ( ); } export function FormatSelector({ value, isEditing, canEdit, onEdit, onChange, onCloseEdit, }: FormatSelectorProps) { const [showCustom, setShowCustom] = useState(false); const [customTop, setCustomTop] = useState(""); const [customFeedText, setCustomFeedText] = useState(""); const parsed = parseFormat(value); const matchedOption = parsed ? findMatchingOption(parsed.topHours, parsed.feedHours) : null; const handleSelectPreset = (option: FormatOption) => { const formatStr = formatToString(option.topHours, option.feedHours); onChange(formatStr); onCloseEdit(); }; const handleSelectCustom = () => { const top = parseInt(customTop, 10); let feed: number | null = null; if (customFeedText.toLowerCase().includes('без')) { feed = null; } else if (customFeedText.includes('дней')) { const daysMatch = customFeedText.match(/(\d+)/); if (daysMatch) { feed = parseInt(daysMatch[1], 10) * 24; } } else if (customFeedText) { feed = parseInt(customFeedText, 10); } if (!isNaN(top) && feed !== null && !isNaN(feed)) { const formatStr = formatToString(top, feed); onChange(formatStr); onCloseEdit(); } }; if (isEditing) { return ( !open && onCloseEdit()}>
Стандартные форматы
{FORMAT_OPTIONS.map((option) => ( handleSelectPreset(option)} /> ))}
{!showCustom ? ( ) : (
Свой формат
setCustomTop(e.target.value)} /> / setCustomFeedText(e.target.value)} />
)}
); } return ( ); }