feat: strict type for EditorImages

This commit is contained in:
DIYgod
2023-08-25 17:30:23 +01:00
parent 32a3aee50d
commit 9776bbf7e1
5 changed files with 46 additions and 76 deletions
@@ -138,7 +138,6 @@ export default function PortfolioEditor() {
const baseValues = {
...values,
characterId: site.data?.characterId,
cover: values.cover,
}
if (visibility === PageVisibilityEnum.Draft) {
createPage.mutate({
@@ -195,8 +195,6 @@ export default function PostEditor() {
tags: uniqueTags,
slug: values.slug || defaultSlug,
characterId: site.data?.characterId,
cover: values.cover,
disableAISummary: values.disableAISummary,
}
if (visibility === PageVisibilityEnum.Draft) {
createPage.mutate({
@@ -33,7 +33,7 @@ export default function SiteSettingsGeneralPage() {
ua: "",
uh: "",
} as {
icon: string
icon?: string
banner?: {
address: string
mime_type: string
@@ -118,13 +118,12 @@ export default function SiteSettingsGeneralPage() {
control={form.control}
render={({ field }) => (
<ImageUploader
id="icon"
className="w-24 h-24 rounded-full"
uploadStart={() => {
setIconUploading(true)
}}
uploadEnd={(key) => {
form.setValue("icon", key as string)
form.setValue("icon", key)
setIconUploading(false)
}}
accept="image/*"
@@ -142,22 +141,18 @@ export default function SiteSettingsGeneralPage() {
control={form.control}
render={({ field }) => (
<ImageUploader
id="banner"
className="max-w-screen-md h-[220px]"
uploadStart={() => {
setBannerUploading(true)
}}
uploadEnd={(key) => {
form.setValue(
"banner",
key as { address: string; mime_type: string },
)
form.setValue("banner", key)
setBannerUploading(false)
}}
withMimeType={true}
hasClose={true}
accept="image/*,video/*"
{...(field as any)}
{...field}
/>
)}
/>
@@ -17,18 +17,13 @@ export default function EditorCover({
<div>
<FieldLabel label={t("Cover Image")} />
<ImageUploader
id="icon"
className="aspect-video rounded-lg"
value={value as any}
value={value}
hasClose={true}
withMimeType={true}
uploadEnd={(key) => {
const { address, mime_type } = key as Values["cover"]
updateValue({
cover: {
address,
mime_type,
},
cover: key,
})
}}
accept="image/*"
+40 -57
View File
@@ -28,18 +28,24 @@ export const ImageUploader = forwardRef(function ImageUploader(
className?: string
image?: string
uploadStart?: () => void
uploadEnd?: (
key:
| string
| {
address?: string
mime_type?: string
},
) => void
withMimeType?: boolean
hasClose?: boolean
accept?: string
} & React.ComponentPropsWithRef<"input">,
} & (
| {
withMimeType?: false
value?: string
uploadEnd?: (key?: string) => void
}
| {
withMimeType: true
value?: {
address?: string
mime_type?: string
}
uploadEnd?: (key?: { address: string; mime_type: string }) => void
}
) &
Omit<React.ComponentPropsWithRef<"input">, "value">,
ref: React.ForwardedRef<HTMLInputElement>,
) {
const uploadFile = useUploadFile()
@@ -56,32 +62,30 @@ export const ImageUploader = forwardRef(function ImageUploader(
setLoading(true)
uploadStart?.()
const key = (await uploadFile(event.target.files[0])).key
uploadEnd?.(
withMimeType
? {
address: key,
mime_type: event.target.files[0].type,
}
: key,
)
if (withMimeType) {
uploadEnd?.({
address: key,
mime_type: event.target.files[0].type,
})
} else {
uploadEnd?.(key)
}
setLoading(false)
} else if (event.target.value) {
setImageUrl(event.target.value)
}
}
const clear = () => {
setImageUrl(undefined)
uploadEnd?.(withMimeType ? {} : "")
uploadEnd?.(undefined)
}
useEffect(() => {
if (!imageUrl) {
setImageUrl(
((withMimeType
? (inputProps.value as any)?.address
: inputProps.value) as string) || "",
)
if (typeof inputProps.value === "object") {
setImageUrl(inputProps.value.address)
} else {
setImageUrl(inputProps.value)
}
}
}, [inputProps.value])
@@ -92,37 +96,9 @@ export const ImageUploader = forwardRef(function ImageUploader(
className,
)}
>
<input
{...(withMimeType
? {
value: (inputProps.value as any)?.address,
...inputProps,
}
: inputProps)}
onChange={handleChange}
className="hidden"
/>
{imageUrl && !withMimeType && (
<Image
className="mx-auto object-cover"
src={toGateway(imageUrl)}
alt="banner"
fill
/>
)}
{imageUrl &&
withMimeType &&
(inputProps.value as any)?.mime_type?.split("/")[0] === "image" && (
<Image
className="mx-auto object-cover"
src={toGateway(imageUrl)}
alt="banner"
fill
/>
)}
{imageUrl &&
withMimeType &&
(inputProps.value as any)?.mime_type?.split("/")[0] === "video" && (
(typeof inputProps.value === "object" &&
inputProps.value.mime_type?.split("/")[0] === "video" ? (
<video
className="max-w-screen-md mx-auto object-cover h-full w-full"
src={toGateway(imageUrl)}
@@ -130,7 +106,14 @@ export const ImageUploader = forwardRef(function ImageUploader(
muted
playsInline
/>
)}
) : (
<Image
className="mx-auto object-cover"
src={toGateway(imageUrl)}
alt="banner"
fill
/>
))}
{!imageUrl && (
<div className="w-full h-full flex justify-center items-center text-zinc-500 text-center bg-white">
{t("Click to select files")}