From c7576b43ac2171e399cafbf4d7905ebb7ee8be7a Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Mon, 20 Jan 2025 13:11:27 -0600 Subject: [PATCH] Introduce loading status to thumbnails --- src/components/LinkWithStatus.tsx | 15 ++++++++++++--- src/photo/PhotoMedium.tsx | 6 +++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/components/LinkWithStatus.tsx b/src/components/LinkWithStatus.tsx index 5655454d..6b00bf2c 100644 --- a/src/components/LinkWithStatus.tsx +++ b/src/components/LinkWithStatus.tsx @@ -17,10 +17,15 @@ const FLICKER_THRESHOLD = 400; // Clear loading status after long duration const MAX_LOADING_DURATION = 15_000; -export type LinkWithStatusProps = ComponentProps & { +export type LinkWithStatusProps = Omit< + ComponentProps, 'children' +> & { loadingElement?: ReactNode loadingClassName?: string contentClassName?: string + children: ReactNode | ((props: { + isLoading: boolean + }) => ReactNode) } export default function LinkWithStatus({ @@ -44,6 +49,8 @@ export default function LinkWithStatus({ const stopLoadingTimeout = useRef(undefined); const maxLoadingTimeout = useRef(undefined); + const isControlled = typeof children === 'function'; + const clearTimeouts = useCallback(() => { [startLoadingTimeout, stopLoadingTimeout, maxLoadingTimeout] .forEach(timeout => { @@ -114,11 +121,13 @@ export default function LinkWithStatus({ contentClassName, loadingElement ? isLoading ? 'opacity-0' : 'opacity-100' - : loadingClassName + : (loadingClassName || isControlled) ? 'opacity-100' : isLoading ? 'opacity-50' : 'opacity-100', )}> - {children} + {typeof children === 'function' + ? children({ isLoading }) + : children} {isLoading && loadingElement && - + ); };