'use client'; import { useRouter } from 'next/navigation'; import { useContext, useMemo, useState } from 'react'; import { useForm } from 'react-hook-form'; import { useSWRConfig } from 'swr'; import AppLink from './AppLink'; import Button from './Button'; import Dialog from './Dialog'; import Icon from './Icon'; import Input from './Input'; import Select from './Select'; import { UserContext } from '@/context/user'; import { useGroups } from '@/lib/hooks/use-groups'; import { usePuzzles } from '@/lib/hooks/use-puzzles'; import type { Chapter, Puzzle } from '@/lib/puzzles'; import { cn } from '@/lib/utils'; export default function Puzzles({ token }: { token: string }) { const { data: me } = useContext(UserContext); const { data, isLoading } = usePuzzles({ token }); const [isOpen, setIsOpen] = useState([]); const [filter, setFilter] = useState(''); const [filterChapter, setFilterChapter] = useState(); function handleClick(index: number) { setIsOpen((prevState) => { const newState = [...prevState]; newState[index] = !newState[index]; return newState; }); } function isInEventGroup(chapter: Chapter) { return ( chapter.startDate && chapter.endDate && me?.groups?.some((group) => group.chapter && group.chapter === chapter.id) ); } const filteredData = useMemo(() => { if (filter && filterChapter) { console.log(filter); return data ?.find((chapter) => chapter.id === filterChapter) ?.puzzles.filter((puzzle) => puzzle!.tags!.some((tag) => tag.name === filter)) .map((puzzle) => puzzle); } return data?.find((chapter) => chapter.id === filterChapter)?.puzzles; }, [data, filter, filterChapter]); return ( <> {(!isLoading && data?.map((chapter) => (

{chapter.name}

{!isInEventGroup(chapter) && ( handleClick(chapter.id)} trigger={ } className="right-96 p-4" > )}
{chapter.startDate && chapter.endDate ? (
{new Date(chapter.startDate).toLocaleDateString('fr-FR', { day: 'numeric', month: 'long', year: 'numeric', hour: 'numeric' })}{' '} -{' '} {new Date(chapter.endDate).toLocaleDateString('fr-FR', { day: 'numeric', month: 'long', year: 'numeric', hour: 'numeric' })}
) : (
)}
{isInEventGroup(chapter) && ( )}
{isInEventGroup(chapter) && (
    {filteredData && filteredData .sort((p1, p2) => { const p1Tags = p1.tags || []; const p2Tags = p2.tags || []; const p1Easy = p1Tags.some((tag) => tag.name === 'easy'); const p2Easy = p2Tags.some((tag) => tag.name === 'easy'); if (p1Easy !== p2Easy) { return p1Easy ? -1 : 1; } const p1Medium = p1Tags.some((tag) => tag.name === 'medium'); const p2Medium = p2Tags.some((tag) => tag.name === 'medium'); if (p1Medium !== p2Medium) { return p1Medium ? -1 : 1; } const p1Hard = p1Tags.some((tag) => tag.name === 'hard'); const p2Hard = p2Tags.some((tag) => tag.name === 'hard'); if (p1Hard !== p2Hard) { return p1Hard ? -1 : 1; } return p1Tags.length - p2Tags.length; }) .map((puzzle) => ( ))}
)}
))) || (
{[...Array(3).keys()].map((i) => (
    {[...Array(6).keys()].map((j) => ( ))}
))}
)} ); } function PuzzleProp({ puzzle, chapter }: { puzzle: Puzzle; chapter: Chapter }) { const isPuzzleAvailable = (chapter: Chapter) => { const today = new Date(); const startDate = new Date(chapter.startDate!); const endDate = new Date(chapter.endDate!); return ( (chapter.startDate && chapter.endDate && today >= startDate && today <= endDate) || (!chapter.startDate && !chapter.endDate) ); }; return (
  • tag.name.toLowerCase() === 'easy'), 'border-yellow-600/30': puzzle.tags?.find((tag) => tag.name.toLowerCase() === 'medium'), 'border-red-600/30': puzzle.tags?.find((tag) => tag.name.toLowerCase() === 'hard'), 'border-highlight-primary': !puzzle.tags?.length, 'cursor-not-allowed': !isPuzzleAvailable(chapter) } )} > {isPuzzleAvailable(chapter) ? (
    {puzzle.name}{' '} ({puzzle.score ? `${puzzle.score}` : '?'}/{puzzle.scoreMax} points) {puzzle.tags?.length && (
    {puzzle.tags .filter((tag) => !['easy', 'medium', 'hard'].includes(tag.name)) .map((tag, i) => ( {tag.name} ))}
    )}
    ) : (
    {puzzle.name} ({puzzle.scoreMax}) {puzzle.tags?.length && (
    {puzzle.tags .filter((tag) => !['easy', 'medium', 'hard'].includes(tag.name)) .map((tag, i) => ( {tag.name} ))}
    )}
    )}
  • ); } function FilterChapter({ chapters, chapter, filter, setFilter, setFilterChapter }: { chapters: Chapter[]; chapter: Chapter; filter: string; setFilter: (filter: string) => void; setFilterChapter: (chapter: number) => void; }) { let options = [] as { title: string; value: string }[]; options = chapters .find((c) => c.id === chapter.id) ?.puzzles?.map((p) => p.tags) .flat() .filter((tag, index, self) => self.findIndex((t) => t!.name === tag!.name) === index) .map((t) => { return { title: t!.name, value: t!.name }; }) as { title: string; value: string }[]; options?.unshift({ title: 'Tout les puzzles', value: '' }); setFilterChapter(chapter.id); return (
    ) : ( <>