import React, { Suspense } from 'react'; import { cva } from 'class-variance-authority'; import { useLocation, useRouter } from '@tanstack/react-router'; import { cn } from '@/lib/cn'; import ErrorBoundary from '@/components/elements/ErrorBoundary'; export type SpinnerSize = 'small' | 'base' | 'large'; interface Props { size?: SpinnerSize; centered?: boolean; isBlue?: boolean; children?: React.ReactNode; } type SpinnerComponentType = ((props: Props) => React.JSX.Element) & { displayName?: string; Size: Record<'SMALL' | 'BASE' | 'LARGE', SpinnerSize>; Suspense: ((props: Props) => React.JSX.Element) & { displayName?: string }; }; const spinnerSize = cva('', { variants: { size: { small: 'w-4 h-4 border-2', base: 'w-8 h-8 border-[3px]', large: 'w-16 h-16 border-[6px]', }, }, defaultVariants: { size: 'base', }, }); const SpinnerComponent = ({ size, isBlue }: Props) => ( ); const Spinner = (({ centered, ...props }: Props) => centered ? (
) : ( )) as SpinnerComponentType; Spinner.displayName = 'Spinner'; Spinner.Size = { SMALL: 'small', BASE: 'base', LARGE: 'large', }; const LocationErrorBoundary = ({ children }: { children?: React.ReactNode }) => { const href = useLocation({ select: (location) => location.href }); return {children}; }; const SuspenseErrorBoundary = ({ children }: { children?: React.ReactNode }) => useRouter({ warn: false }) ? ( {children} ) : ( {children} ); Spinner.Suspense = ({ children, centered = true, size = Spinner.Size.LARGE, ...props }) => ( }> {children} ); Spinner.Suspense.displayName = 'Spinner.Suspense'; export default Spinner;