Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 'use client' import { useQuery } from '@tanstack/react-query' import { useTranslations } from 'next-intl' import { css } from '../../styled-system/css' import type { FlashcardFormState } from '@/app/create/flashcards/page' interface FlashcardPreviewProps { config: Partial<FlashcardFormState> } async function fetchFlashcardPreview(config: Partial<FlashcardFormState>): Promise<string | null> { const response = await fetch('/api/create/flashcards/preview', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config), }) if (!response.ok) { const errorData = await response.json().catch(() => ({})) throw new Error(errorData.error || errorData.message || 'Failed to fetch preview') } const data = await response.json() return data.svg } export function FlashcardPreview({ config }: FlashcardPreviewProps) { const t = useTranslations('create.flashcards') // Use React Query to fetch preview (with automatic caching and updates) const { data: previewSvg, isLoading, error, } = useQuery({ queryKey: ['flashcard-preview', config], queryFn: () => fetchFlashcardPreview(config), enabled: typeof window !== 'undefined' && !!config.range, staleTime: 30000, // Consider data fresh for 30 seconds retry: 1, }) // Show loading state if (isLoading || !previewSvg) { return ( <div className={css({ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: '400px', color: 'gray.600', })} > <div className={css({ w: '8', h: '8', border: '3px solid', borderColor: 'gray.200', borderTopColor: 'brand.600', rounded: 'full', animation: 'spin 1s linear infinite', mb: '4', })} /> <p className={css({ fontSize: 'lg' })}> {isLoading ? t('preview.loading') : t('preview.noPreview')} </p> </div> ) } // Show error state if (error) { return ( <div className={css({ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: '400px', color: 'red.600', p: '6', textAlign: 'center', })} > <p className={css({ fontSize: 'lg', fontWeight: 'semibold', mb: '2' })}> {t('preview.error')} </p> <p className={css({ fontSize: 'sm', color: 'red.500' })}> {error instanceof Error ? error.message : 'Unknown error'} </p> </div> ) } return ( <div className={css({ display: 'flex', flexDirection: 'column', gap: '4', })} > <div className={css({ fontSize: 'sm', fontWeight: 'medium', color: 'brand.700', textAlign: 'center', })} > {t('preview.livePreview')} </div> <div className={css({ bg: 'white', rounded: 'lg', p: '4', border: '2px solid', borderColor: 'gray.200', overflow: 'auto', maxHeight: '600px', })} dangerouslySetInnerHTML={{ __html: previewSvg }} /> <div className={css({ fontSize: 'xs', color: 'gray.500', textAlign: 'center', })} > {t('preview.hint')} </div> </div> ) } |