|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { File } from '@payloadcms/ui' |
| 4 | +import React, { useCallback, useEffect, useState } from 'react' |
| 5 | +import { UploadData } from '@payloadcms/richtext-lexical' |
| 6 | +// import { getMediaById } from '@/app/(frontend)/actions/media' |
| 7 | +import { useLexicalComposerContext } from '@payloadcms/richtext-lexical/lexical/react/LexicalComposerContext' |
| 8 | +import { $getNodeByKey } from '@payloadcms/richtext-lexical/lexical' |
| 9 | +import { X } from 'lucide-react' |
| 10 | +import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip' |
| 11 | + |
| 12 | +export type ElementProps = { |
| 13 | + data: UploadData |
| 14 | + nodeKey: string |
| 15 | +} |
| 16 | + |
| 17 | +const Component: React.FC<ElementProps> = (props) => { |
| 18 | + const { |
| 19 | + data: { relationTo, value }, |
| 20 | + nodeKey, |
| 21 | + } = props |
| 22 | + const [editor] = useLexicalComposerContext() |
| 23 | + const [thumbnailUrl, setThumbnailUrl] = useState('') |
| 24 | + const [fileType, setFileType] = useState('') |
| 25 | + const [filesize, setFileSize] = useState<string>('') |
| 26 | + |
| 27 | + // if (typeof value === 'object') { |
| 28 | + // throw new Error( |
| 29 | + // 'Upload value should be a string or number. The Lexical Upload component should not receive the populated value object.', |
| 30 | + // ) |
| 31 | + // } |
| 32 | + |
| 33 | + const fetchMedia = async (id: string) => { |
| 34 | + console.log('fetchMedia', id) |
| 35 | + // const res = await getMediaById(id) |
| 36 | + // const res = |
| 37 | + // // if (res && res.status === 'success') { |
| 38 | + // // setThumbnailUrl(res.data.url || res.data.thumbnailURL || '') |
| 39 | + // // setFileSize((res.data.filesize || 0) / 1000) |
| 40 | + // // setFileType(res.data.mimeType?.split('/')[1] || '') |
| 41 | + // } |
| 42 | + } |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + if (typeof value === 'string') { |
| 46 | + if (value.length === 24) fetchMedia(value) |
| 47 | + else { |
| 48 | + setThumbnailUrl(value || '') |
| 49 | + setFileSize(formatFileSize(getFileSize())) |
| 50 | + setFileType(getFileExtension() || '') |
| 51 | + } |
| 52 | + } else if (typeof value === 'object') { |
| 53 | + setThumbnailUrl(`${process.env.NEXT_PUBLIC_API_URL}${value.url}` || '') |
| 54 | + setFileSize(formatFileSize(value.filesize)) |
| 55 | + setFileType(value.mimeType?.split('/')[1] || '') |
| 56 | + } |
| 57 | + }, []) |
| 58 | + |
| 59 | + const removeUpload = useCallback(() => { |
| 60 | + editor.update(() => { |
| 61 | + $getNodeByKey(nodeKey)?.remove() |
| 62 | + }) |
| 63 | + }, [editor, nodeKey]) |
| 64 | + |
| 65 | + const getFileExtension = () => { |
| 66 | + const currVal = props.data.value |
| 67 | + if (typeof currVal === 'string') { |
| 68 | + return currVal.split(';base64')[0]?.split('image/')[1] |
| 69 | + } |
| 70 | + return '' |
| 71 | + } |
| 72 | + const getFileSize = () => { |
| 73 | + const currVal = props.data.value |
| 74 | + if (typeof currVal === 'string') { |
| 75 | + const stringLength = currVal.split(',')[1]?.length || 0 |
| 76 | + |
| 77 | + const sizeInBytes = 4 * Math.ceil(stringLength / 3) * 0.5624896334383812 |
| 78 | + const sizeInKb = sizeInBytes / 1000 |
| 79 | + return sizeInKb |
| 80 | + } |
| 81 | + return 0 |
| 82 | + } |
| 83 | + |
| 84 | + function formatFileSize(bytes: number): string { |
| 85 | + if (bytes >= 1_000_000) { |
| 86 | + const mb = bytes / 1_000_000 |
| 87 | + // 10.0+ MB -> no decimals, otherwise 1 decimal |
| 88 | + const str = mb >= 10 ? Math.round(mb).toString() : mb.toFixed(1) |
| 89 | + return `${str} MB` |
| 90 | + } |
| 91 | + // Below 1 MB -> show whole KB (floor), e.g. 273852 -> 273 KB |
| 92 | + const kb = Math.floor(bytes / 1_000) |
| 93 | + return `${kb} KB` |
| 94 | + } |
| 95 | + |
| 96 | + return ( |
| 97 | + <div className="my-2" contentEditable={false}> |
| 98 | + <div className="border border-slate-800 rounded-xl overflow-clip w-60 flex flex-col gap-2 bg-[#222222]"> |
| 99 | + <div className="flex gap-2 border-b border-b-slate-100/10"> |
| 100 | + <div className="flex-1"> |
| 101 | + {props.data.value ? ( |
| 102 | + <img |
| 103 | + // alt={props.data.} |
| 104 | + className="object-cover m-0 w-full min-w-[115px] min-h-[105px]" |
| 105 | + data-lexical-upload-id={value} |
| 106 | + data-lexical-upload-relation-to={relationTo} |
| 107 | + src={thumbnailUrl} |
| 108 | + /> |
| 109 | + ) : ( |
| 110 | + <File /> |
| 111 | + )} |
| 112 | + </div> |
| 113 | + <TooltipProvider> |
| 114 | + <Tooltip> |
| 115 | + <TooltipTrigger className="flex-1 flex justify-center items-center text-[#b5b5b5]"> |
| 116 | + <span className="font-medium text-sm">Media</span> |
| 117 | + <button |
| 118 | + onClick={(e) => { |
| 119 | + e.preventDefault() |
| 120 | + removeUpload() |
| 121 | + }} |
| 122 | + className="flex-1 flex justify-center items-center" |
| 123 | + > |
| 124 | + <X width={24} height={24} strokeWidth={1} /> |
| 125 | + </button> |
| 126 | + </TooltipTrigger> |
| 127 | + <TooltipContent> |
| 128 | + <span>Remove image</span> |
| 129 | + </TooltipContent> |
| 130 | + </Tooltip> |
| 131 | + </TooltipProvider> |
| 132 | + </div> |
| 133 | + <div className="flex items-center justify-center gap-4 py-3 flex-1 text-xs font-bold"> |
| 134 | + <span className="">{fileType?.toLocaleUpperCase()}</span> |
| 135 | + <span>{filesize}</span> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +export default Component |
0 commit comments