import React from 'react'; import { useTranslation } from 'react-i18next'; import { Loader2, RefreshCcw } from 'lucide-react'; import { AdminLayout } from '../components/AdminLayout'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import type { HelpCenterArticleSummary, HelpCenterArticle } from '../api'; import { fetchHelpCenterArticles, fetchHelpCenterArticle } from '../api'; function normalizeLocale(language: string | undefined): 'de' | 'en' { const normalized = (language ?? 'de').toLowerCase().split('-')[0]; return normalized === 'en' ? 'en' : 'de'; } export default function FaqPage() { const { t, i18n } = useTranslation('dashboard'); const helpLocale = normalizeLocale(i18n.language); const [query, setQuery] = React.useState(''); const [articles, setArticles] = React.useState([]); const [listState, setListState] = React.useState<'loading' | 'ready' | 'error'>('loading'); const [selectedSlug, setSelectedSlug] = React.useState(null); const [detailState, setDetailState] = React.useState<'idle' | 'loading' | 'ready' | 'error'>('idle'); const [articleCache, setArticleCache] = React.useState>({}); const loadArticles = React.useCallback(async () => { setListState('loading'); try { const data = await fetchHelpCenterArticles(helpLocale); setArticles(data); setListState('ready'); } catch (error) { console.error('[HelpCenter] Failed to load list', error); setListState('error'); } }, [helpLocale]); React.useEffect(() => { setArticles([]); setArticleCache({}); setSelectedSlug(null); loadArticles(); }, [loadArticles]); React.useEffect(() => { if (!selectedSlug && articles.length > 0) { setSelectedSlug(articles[0].slug); } else if (selectedSlug && !articles.some((article) => article.slug === selectedSlug)) { setSelectedSlug(articles[0]?.slug ?? null); } }, [articles, selectedSlug]); const loadArticle = React.useCallback(async (slug: string, options?: { bypassCache?: boolean }) => { if (!slug) { return; } const bypassCache = options?.bypassCache ?? false; if (!bypassCache && articleCache[slug]) { setDetailState('ready'); return; } setDetailState('loading'); try { const article = await fetchHelpCenterArticle(slug, helpLocale); setArticleCache((prev) => ({ ...prev, [slug]: article })); setDetailState('ready'); } catch (error) { console.error('[HelpCenter] Failed to load article', error); setDetailState('error'); } }, [articleCache, helpLocale]); React.useEffect(() => { if (selectedSlug) { loadArticle(selectedSlug); } }, [selectedSlug, loadArticle]); const filteredArticles = React.useMemo(() => { if (!query.trim()) { return articles; } const needle = query.trim().toLowerCase(); return articles.filter((article) => `${article.title} ${article.summary}`.toLowerCase().includes(needle)); }, [articles, query]); const activeArticle = selectedSlug ? articleCache[selectedSlug] : null; return (
{t('helpCenter.title', 'Hilfe & Dokumentation')} {t('helpCenter.subtitle', 'Aktuelle Playbooks für dein Team.')} setQuery(event.target.value)} />
{listState === 'loading' && (
{t('helpCenter.article.loading', 'Lädt...')}
)} {listState === 'error' && (

{t('helpCenter.list.error')}

)} {listState === 'ready' && filteredArticles.length === 0 && (
{t('helpCenter.list.empty')}
)} {listState === 'ready' && filteredArticles.length > 0 && (
{filteredArticles.map((article) => { const isActive = selectedSlug === article.slug; return ( ); })}
)}
{activeArticle?.title ?? t('helpCenter.article.placeholder')} {activeArticle?.summary ?? ''} {!selectedSlug && (

{t('helpCenter.article.placeholder')}

)} {selectedSlug && detailState === 'loading' && (
{t('helpCenter.article.loading')}
)} {selectedSlug && detailState === 'error' && (

{t('helpCenter.article.error')}

)} {detailState === 'ready' && activeArticle && (
{activeArticle.updated_at && ( {t('helpCenter.article.updated', { date: formatDate(activeArticle.updated_at, i18n.language) })} )}
{activeArticle.related && activeArticle.related.length > 0 && (

{t('helpCenter.article.related')}

{activeArticle.related.map((rel) => ( ))}
)}
)}
); } function formatDate(value: string, language: string | undefined): string { try { return new Date(value).toLocaleDateString(language ?? 'de-DE', { day: '2-digit', month: 'short', year: 'numeric', }); } catch { return value; } }