feat: banner setting

This commit is contained in:
DIYgod
2022-11-10 00:59:55 +00:00
parent accd9ff183
commit 7d8b85bb0e
5 changed files with 144 additions and 10 deletions
+1 -1
View File
@@ -63,7 +63,7 @@ export const Avatar: React.FC<
}}
>
<Image
className="overflow-hidden"
className="overflow-hidden object-cover"
src={image}
width={size}
height={size}
+81 -6
View File
@@ -3,6 +3,8 @@ import React, { ChangeEvent, forwardRef, useEffect } from "react"
import { useState } from "react"
import { useUploadFile } from "~/hooks/useUploadFile"
import { toGateway } from "~/lib/ipfs-parser"
import { Image } from "~/components/ui/Image"
import { XMarkIcon } from "@heroicons/react/20/solid"
const getBase64 = (img: File, callback: (url: string) => void) => {
const reader = new FileReader()
@@ -16,12 +18,23 @@ export const ImageUploader = forwardRef(function ImageUploader(
image,
uploadStart,
uploadEnd,
withMimeType,
hasClose,
...inputProps
}: {
className?: string
image?: string
uploadStart?: () => void
uploadEnd?: (key: string) => void
uploadEnd?: (
key:
| string
| {
address?: string
mime_type?: string
},
) => void
withMimeType?: boolean
hasClose?: boolean
} & React.ComponentPropsWithRef<"input">,
ref: React.ForwardedRef<HTMLInputElement>,
) {
@@ -38,32 +51,94 @@ export const ImageUploader = forwardRef(function ImageUploader(
setLoading(true)
uploadStart?.()
const key = (await uploadFile(event.target.files[0])).key
uploadEnd?.(key)
uploadEnd?.(
withMimeType
? {
address: key,
mime_type: event.target.files[0].type,
}
: key,
)
setLoading(false)
} else if (event.target.value) {
setImageUrl(event.target.value)
}
}
const clear = () => {
setImageUrl(undefined)
uploadEnd?.(withMimeType ? {} : "")
}
useEffect(() => {
if (!imageUrl) {
setImageUrl((inputProps.value as string) || "")
setImageUrl(
((withMimeType
? (inputProps.value as any)?.address
: inputProps.value) as string) || "",
)
}
}, [inputProps.value])
return (
<div
className={clsx(
"relative flex-col overflow-hidden bg-gray-400",
"relative flex-col overflow-hidden border border-gray-100",
className,
)}
>
<input {...inputProps} onChange={handleChange} className="hidden" />
{imageUrl && <img src={toGateway(imageUrl)} className="w-full h-full" />}
<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" && (
<video
className="max-w-screen-md mx-auto object-cover h-full w-full"
src={toGateway(imageUrl)}
autoPlay
muted
playsInline
/>
)}
<input
onChange={handleChange}
type="file"
className="absolute top-0 bottom-0 left-0 right-0 opacity-0"
/>
{hasClose && (
<div
onClick={clear}
className="w-8 h-8 absolute top-4 right-4 shadow-lg bg-white rounded-full cursor-pointer"
>
<XMarkIcon />
</div>
)}
{loading && (
<div className="absolute top-0 bottom-0 left-0 right-0 flex justify-center items-center bg-gray-500 opacity-50">
<div className="loading flex justify-center items-center relative text-white"></div>