mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-18 16:53:57 +00:00
28 lines
754 B
TypeScript
28 lines
754 B
TypeScript
import { forwardRef, useEffect, useState } from "react";
|
|
|
|
export interface AsyncImageProps
|
|
extends React.DetailedHTMLProps<
|
|
React.ImgHTMLAttributes<HTMLImageElement>,
|
|
HTMLImageElement
|
|
> {
|
|
onSettled?: (url: string) => void;
|
|
}
|
|
|
|
export const AsyncImage = forwardRef<HTMLImageElement, AsyncImageProps>(
|
|
({ onSettled, ...props }, ref) => {
|
|
const [source, setSource] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (props.src && props.src.startsWith("http")) {
|
|
window.electron.getOrCacheImage(props.src).then((url) => {
|
|
setSource(url);
|
|
|
|
if (onSettled) onSettled(url);
|
|
});
|
|
}
|
|
}, [props.src, onSettled]);
|
|
|
|
return <img ref={ref} {...props} src={source ?? props.src} />;
|
|
}
|
|
);
|