import { Fragment, useEffect, useState } from 'react' import TagLink from '@/components/TagLink' import SidebarComponent from '@/components/layout/SidebarComponent' import { apiGet } from '@/lib/api' import type { FC, ReactNode } from 'react' import type { Tag } from '@/types' type TagWithDepth = Tag & { hasChildren: boolean children: TagWithDepth[] } const setChildrenById = ( tags: TagWithDepth[], targetId: number, children: TagWithDepth[], ): TagWithDepth[] => ( tags.map (tag => { if (tag.id === targetId) return { ...tag, children } if (tag.children.length === 0) return tag return { ...tag, children: (setChildrenById (tag.children, targetId, children) .filter (t => t.category !== 'meme' || t.hasChildren)) } })) export default (() => { const [tags, setTags] = useState ([]) const [openTags, setOpenTags] = useState> ({ }) const [tagFetchedFlags, setTagFetchedFlags] = useState> ({ }) useEffect (() => { void (async () => { setTags ((await apiGet ('/tags/with-depth')) .filter (t => t.category !== 'meme' || t.hasChildren)) }) () }, []) const renderTags = (ts: TagWithDepth[], nestLevel = 0): ReactNode => ( ts.map (t => (
  • {openTags[t.id] && renderTags (t.children, nestLevel + 1)}
    ))) return (
      {renderTags (tags)}
    ) }) satisfies FC