Files
wehub-resource-sync 2114b14ee0
Sync main into demo / sync (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:35:26 +08:00

42 lines
1005 B
TypeScript

import React, { useEffect, useState } from 'react';
import * as FileSystem from '../FileSystemService';
export const AsyncFsImage: React.FC<{
path: string;
className?: string;
alt?: string;
fallbackSrc?: string;
}> = ({ path, className, alt, fallbackSrc }) => {
const [src, setSrc] = useState<string | null>(fallbackSrc || null);
useEffect(() => {
let mounted = true;
const load = async () => {
const syncUri = FileSystem.getFileUri(path);
if (syncUri) {
if (mounted) setSrc(syncUri);
return;
}
const asyncUri = await FileSystem.getFileUriAsync(path);
if (mounted) setSrc(asyncUri || fallbackSrc || null);
};
load();
return () => {
mounted = false;
};
}, [path, fallbackSrc]);
if (!src) {
// Keep layout stable; caller supplies background styles.
return <div className={className} />;
}
return <img src={src} className={className} alt={alt} loading="lazy" />;
};
export default AsyncFsImage;