'use client'; import { useEffect, useState } from 'react'; import { cc } from '@/utility/css'; import Link from 'next/link'; import { BiError } from 'react-icons/bi'; import Spinner from '@/components/Spinner'; import { IMAGE_OG_DIMENSION } from '../photo/image-response'; export type OGLoadingState = 'unloaded' | 'loading' | 'loaded' | 'failed'; export default function OGTile({ title, description, path, pathImageAbsolute, loadingState: loadingStateExternal, riseOnHover, onLoad, onFail, retryTime, }: { title: string description: string path: string pathImageAbsolute: string loadingState?: OGLoadingState onLoad?: () => void onFail?: () => void riseOnHover?: boolean retryTime?: number }) { const [loadingStateInternal, setLoadingStateInternal] = useState(loadingStateExternal ?? 'unloaded'); const loadingState = loadingStateExternal ?? loadingStateInternal; useEffect(() => { if ( !loadingStateExternal && loadingStateInternal === 'unloaded' ) { setLoadingStateInternal('loading'); } }, [loadingStateExternal, loadingStateInternal]); const { width, height, aspectRatio } = IMAGE_OG_DIMENSION; return (
{loadingState === 'loading' &&
} {loadingState === 'failed' &&
} {(loadingState === 'loading' || loadingState === 'loaded') && {title} { if (onLoad) { onLoad(); } else { setLoadingStateInternal('loaded'); } }} onError={() => { if (onFail) { onFail(); } else { setLoadingStateInternal('failed'); } if (retryTime !== undefined) { setTimeout(() => { setLoadingStateInternal('loading'); }, retryTime); } }} />}
{title}
{description}
); };