'use client'; import { getTutorial } from '@/utils/tutorial'; import { useQuery } from '@tanstack/react-query'; import { Avatar, AvatarFallback, AvatarImage } from '@workspace/ui/components/avatar'; import { buttonVariants } from '@workspace/ui/components/button'; import { Markdown } from '@workspace/ui/custom-components/markdown'; import { useOutsideClick } from '@workspace/ui/hooks/use-outside-click'; import { cn } from '@workspace/ui/lib/utils'; import { formatDate } from '@workspace/ui/utils'; import { AnimatePresence, motion } from 'framer-motion'; import { useTranslations } from 'next-intl'; import { RefObject, useEffect, useId, useRef, useState } from 'react'; import { CloseIcon } from './close-icon'; interface Item { path: string; title: string; updated_at?: string; icon?: string; } export function TutorialButton({ items }: { items: Item[] }) { const t = useTranslations('document'); const [active, setActive] = useState(null); const id = useId(); const ref = useRef(null); const { data } = useQuery({ enabled: !!(active as Item)?.path, queryKey: ['getTutorial', (active as Item)?.path], queryFn: async () => { const markdown = await getTutorial((active as Item)?.path); return markdown; }, }); useEffect(() => { function onKeyDown(event: KeyboardEvent) { if (event.key === 'Escape') { setActive(false); } } if (active && typeof active === 'object') { document.body.style.overflow = 'hidden'; } else { document.body.style.overflow = 'auto'; } window.addEventListener('keydown', onKeyDown); return () => window.removeEventListener('keydown', onKeyDown); }, [active]); useOutsideClick(ref as RefObject, () => setActive(null)); return ( <> {active && typeof active === 'object' && ( )} {active && typeof active === 'object' ? (
setActive(null)} > { return ( // eslint-disable-next-line @next/next/no-img-element ); }, }} > {data?.content || ''}
) : null}
    {items.map((item, index) => ( setActive(item)} className='bg-background hover:bg-accent flex cursor-pointer items-center justify-between rounded-[40px] border p-1 sm:p-4' >
    {item.title.split('')[0]}
    {item.title} {item.updated_at && ( {formatDate(new Date(item.updated_at), false)} )}
    {t('read')}
    ))}
); }