added image file component

This commit is contained in:
Roland Osborne 2024-12-13 16:28:03 -08:00
parent 1be6ab4770
commit 21b4d1a4b6
3 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,9 @@
.asset {
padding-top: 16px;
.thumb {
width: auto;
height: 64px;
}
}

View File

@ -0,0 +1,17 @@
import React from 'react';
import { Image } from '@mantine/core'
import { useImageFile } from './useImageFile.hook';
import classes from './ImageFile.module.css'
export function ImageFile({ source }: {source: File}) {
const { state, actions } = useImageFile(source);
return (
<div className={classes.asset}>
{ state.thumbUrl && (
<Image radius="sm" className={classes.thumb} src={state.thumbUrl} />
)}
</div>
);
}

View File

@ -0,0 +1,23 @@
import { useState, useEffect } from 'react'
export function useImageFile(source: File) {
const [state, setState] = useState({
thumbUrl: null,
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const updateState = (value: any) => {
setState((s) => ({ ...s, ...value }))
}
useEffect(() => {
const thumbUrl = URL.createObjectURL(source);
updateState({ thumbUrl });
return () => { URL.revokeObjectURL(thumbUrl) };
}, [source]);
const actions = {
}
return { state, actions }
}