import { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { MotionConfig } from 'framer-motion'; import { articlesApi } from '../api/articles'; import { FocusCardStack } from '../components/focus/FocusCardStack'; import { FocusEmptyState } from '../components/focus/FocusEmptyState'; interface FocusPageProps { onExit: () => void; } export function FocusPage({ onExit }: FocusPageProps) { const [isComplete, setIsComplete] = useState(false); // Snapshot of unread articles for the session. Read/favorite updates are // patched into this cache entry, never refetched, so the deck stays stable. const { data: articles, isPending, isError, refetch } = useQuery({ queryKey: ['articles', 'focus'], queryFn: () => articlesApi.list({ unread: true, limit: 100 }), staleTime: Infinity, gcTime: 0, }); // Deck order is frozen on first load (ids only; live data comes from the cache). const [deckIds, setDeckIds] = useState(null); if (deckIds === null && articles) setDeckIds(articles.map((a) => a.id)); const byId = new Map((articles ?? []).map((a) => [a.id, a])); const deck = (deckIds ?? []).map((id) => byId.get(id)).filter((a) => !!a); let body: React.ReactNode; if (isPending) { body = (
); } else if (isError) { body = (

Impossible de charger vos articles.

); } else if (deck.length === 0 || isComplete) { body = ; } else { body = (
setIsComplete(true)} onExit={onExit} />
); } return (
{body}
); }