Merge pull request #204 from unslothai/fix/image-preview-thumbnail
Fix: Resolve image preview thumbnail not rendering before send
This commit is contained in:
commit
d3fbaa2256
1 changed files with 8 additions and 11 deletions
|
|
@ -26,25 +26,22 @@ import {
|
|||
type FC,
|
||||
type PropsWithChildren,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
} from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
|
||||
const useFileSrc = (file: File | undefined): string | undefined => {
|
||||
const objectUrl = useMemo(
|
||||
() => (file ? URL.createObjectURL(file) : undefined),
|
||||
[file],
|
||||
);
|
||||
const [objectUrl, setObjectUrl] = useState<string | undefined>(undefined);
|
||||
|
||||
useEffect(() => {
|
||||
if (!objectUrl) {
|
||||
return undefined;
|
||||
if (!file) {
|
||||
setObjectUrl(undefined);
|
||||
return;
|
||||
}
|
||||
return () => {
|
||||
URL.revokeObjectURL(objectUrl);
|
||||
};
|
||||
}, [objectUrl]);
|
||||
const url = URL.createObjectURL(file);
|
||||
setObjectUrl(url);
|
||||
return () => URL.revokeObjectURL(url);
|
||||
}, [file]);
|
||||
|
||||
return objectUrl;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue