import { Component, createSignal, For, Show } from 'solid-js' import styles from '../styles/Form.module.css' import Button from '../ui/Button' import Modal from '../ui/Modal' interface Topic { id: number title: string slug: string parent_ids?: number[] community: number } interface TopicParentModalProps { isOpen: boolean onClose: () => void topic: Topic | null allTopics: Topic[] onSave: (topic: Topic) => void onError: (error: string) => void } const TopicParentModal: Component = (props) => { const [selectedParentId, setSelectedParentId] = createSignal(null) const [searchQuery, setSearchQuery] = createSignal('') // Получаем текущего родителя при открытии модалки const getCurrentParentId = (): number | null => { const topic = props.topic if (!topic || !topic.parent_ids || topic.parent_ids.length === 0) { return null } return topic.parent_ids[topic.parent_ids.length - 1] } // Фильтрация доступных родителей const getAvailableParents = () => { const currentTopic = props.topic if (!currentTopic) return [] return props.allTopics.filter((topic) => { // Исключаем сам топик if (topic.id === currentTopic.id) return false // Исключаем топики из других сообществ if (topic.community !== currentTopic.community) return false // Исключаем дочерние топики (предотвращаем циклы) if (isDescendant(currentTopic.id, topic.id)) return false // Фильтр по поисковому запросу const query = searchQuery().toLowerCase() if (query && !topic.title.toLowerCase().includes(query)) return false return true }) } // Проверка, является ли топик потомком другого const isDescendant = (ancestorId: number, descendantId: number): boolean => { const descendant = props.allTopics.find((t) => t.id === descendantId) if (!descendant || !descendant.parent_ids) return false return descendant.parent_ids.includes(ancestorId) } // Получение пути к корню для отображения полного пути const getTopicPath = (topicId: number): string => { const topic = props.allTopics.find((t) => t.id === topicId) if (!topic) return '' if (!topic.parent_ids || topic.parent_ids.length === 0) { return topic.title } const parentPath = getTopicPath(topic.parent_ids[topic.parent_ids.length - 1]) return `${parentPath} → ${topic.title}` } // Сохранение изменений const handleSave = () => { const currentTopic = props.topic if (!currentTopic) return const newParentId = selectedParentId() let newParentIds: number[] = [] if (newParentId) { const parentTopic = props.allTopics.find((t) => t.id === newParentId) if (parentTopic) { // Строим полный путь от корня до нового родителя newParentIds = [...(parentTopic.parent_ids || []), newParentId] } } const updatedTopic: Topic = { ...currentTopic, parent_ids: newParentIds } props.onSave(updatedTopic) } // Инициализация при открытии if (props.isOpen && props.topic) { setSelectedParentId(getCurrentParentId()) setSearchQuery('') } return (
setSearchQuery(e.target.value)} placeholder="Введите название темы..." class={styles.searchInput} />
Корневая тема}> {getCurrentParentId() ? getTopicPath(getCurrentParentId()!) : ''}
{/* Опция "Сделать корневой" */}
setSelectedParentId(null)} />
{/* Доступные родители */}
{(topic) => (
setSelectedParentId(topic.id)} />
)}
Нет тем, соответствующих поисковому запросу "{searchQuery()}"
) } export default TopicParentModal