|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { type AsyncListData, useAsyncList } from "@react-stately/data"; |
| 4 | +import type { Key, Selection } from "@react-types/shared"; |
| 5 | +import { CameraIcon } from "lucide-react"; |
| 6 | +import { Fragment, type ReactNode, useCallback, useState } from "react"; |
| 7 | +import { ListBox, ListBoxItem } from "react-aria-components"; |
| 8 | + |
| 9 | +import { UploadImageForm } from "@/components/ui/blocks/upload-image-form"; |
| 10 | +import { Button } from "@/components/ui/button"; |
| 11 | +import { |
| 12 | + Dialog, |
| 13 | + DialogCancelButton, |
| 14 | + DialogFooter, |
| 15 | + DialogHeader, |
| 16 | + DialogTitle, |
| 17 | + DialogTrigger, |
| 18 | +} from "@/components/ui/dialog"; |
| 19 | +import { IconButton } from "@/components/ui/icon-button"; |
| 20 | +import { Label } from "@/components/ui/label"; |
| 21 | +import { Modal, ModalOverlay } from "@/components/ui/modal"; |
| 22 | +import { getImageUrls } from "@/lib/actions/images"; |
| 23 | +import { cn } from "@/lib/styles"; |
| 24 | + |
| 25 | +interface ImageSelectorProps { |
| 26 | + defaultValue: string | undefined; |
| 27 | + name: string; |
| 28 | +} |
| 29 | + |
| 30 | +export function ImageSelector(props: ImageSelectorProps): ReactNode { |
| 31 | + const { defaultValue, name } = props; |
| 32 | + |
| 33 | + const [imageUrl, setImageUrl] = useState(defaultValue); |
| 34 | + |
| 35 | + return ( |
| 36 | + <div className="group grid content-start gap-y-1.5"> |
| 37 | + <Label>Logo</Label> |
| 38 | + {imageUrl ? ( |
| 39 | + <figure className="w-1/2"> |
| 40 | + { |
| 41 | + // eslint-disable-next-line @next/next/no-img-element |
| 42 | + <img alt="" src={imageUrl} /> |
| 43 | + } |
| 44 | + </figure> |
| 45 | + ) : undefined} |
| 46 | + <input name={name} type="hidden" value={imageUrl} /> |
| 47 | + <DialogTrigger> |
| 48 | + <IconButton variant="outline"> |
| 49 | + <CameraIcon aria-hidden={true} className="size-5 shrink-0" /> |
| 50 | + <span className="sr-only">select logo</span> |
| 51 | + </IconButton> |
| 52 | + <ImageDialog handleUpdate={setImageUrl} /> |
| 53 | + </DialogTrigger> |
| 54 | + </div> |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +interface ImagesDialogProps { |
| 59 | + handleUpdate: (value: string) => void; |
| 60 | +} |
| 61 | + |
| 62 | +export function ImageDialog(props: ImagesDialogProps): ReactNode { |
| 63 | + const { handleUpdate } = props; |
| 64 | + |
| 65 | + const [selectedImage, setSelectedImage] = useState<Key | null>(null); |
| 66 | + |
| 67 | + const imagesList = useAsyncList<{ objectName: string } & { url: string }>({ |
| 68 | + async load(_signal) { |
| 69 | + const data = await getImageUrls(); |
| 70 | + return { items: data }; |
| 71 | + }, |
| 72 | + getKey: (item) => { |
| 73 | + return item.objectName; |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + const onSelectionChange = (keys: Selection) => { |
| 78 | + const image = Array.from(keys)[0] ?? null; |
| 79 | + if (image === "all") { |
| 80 | + setSelectedImage(null); |
| 81 | + } else { |
| 82 | + setSelectedImage(image); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + const handleUploadSuccess = useCallback(() => { |
| 87 | + imagesList.reload(); |
| 88 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 89 | + }, []); |
| 90 | + |
| 91 | + return ( |
| 92 | + <ModalOverlay> |
| 93 | + <Modal isDismissable={true}> |
| 94 | + <Dialog> |
| 95 | + {({ close }) => { |
| 96 | + const update = () => { |
| 97 | + if (selectedImage) { |
| 98 | + handleUpdate(selectedImage.toString()); |
| 99 | + } |
| 100 | + close(); |
| 101 | + }; |
| 102 | + |
| 103 | + return ( |
| 104 | + <Fragment> |
| 105 | + <DialogHeader> |
| 106 | + <DialogTitle>Choose Image</DialogTitle> |
| 107 | + </DialogHeader> |
| 108 | + <UploadImageForm onUploadSuccess={handleUploadSuccess} /> |
| 109 | + <ImageGrid imagesList={imagesList} onSelectionChange={onSelectionChange} /> |
| 110 | + <DialogFooter> |
| 111 | + <DialogCancelButton>Cancel</DialogCancelButton> |
| 112 | + <Button onPress={update}>Update</Button> |
| 113 | + </DialogFooter> |
| 114 | + </Fragment> |
| 115 | + ); |
| 116 | + }} |
| 117 | + </Dialog> |
| 118 | + </Modal> |
| 119 | + </ModalOverlay> |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +interface ImageGridProps { |
| 124 | + onSelectionChange: (key: Selection) => void; |
| 125 | + imagesList: AsyncListData<{ objectName: string; url: string }>; |
| 126 | +} |
| 127 | + |
| 128 | +export function ImageGrid(props: ImageGridProps): ReactNode { |
| 129 | + const { imagesList, onSelectionChange } = props; |
| 130 | + |
| 131 | + return ( |
| 132 | + <ListBox |
| 133 | + aria-label="Logo Images" |
| 134 | + className="grid grid-cols-[repeat(auto-fill,minmax(16rem,1fr))] gap-8" |
| 135 | + items={imagesList.items} |
| 136 | + layout="grid" |
| 137 | + onSelectionChange={onSelectionChange} |
| 138 | + selectionMode="single" |
| 139 | + > |
| 140 | + {(item) => { |
| 141 | + const { objectName, url } = item; |
| 142 | + return ( |
| 143 | + <ListBoxItem id={url}> |
| 144 | + {({ isSelected }) => { |
| 145 | + return ( |
| 146 | + <figure |
| 147 | + className={cn( |
| 148 | + "grid grid-rows-[12rem_auto] gap-y-2", |
| 149 | + isSelected ? "ring-4" : undefined, |
| 150 | + )} |
| 151 | + > |
| 152 | + {/* FIXME: add a custom loader for `next/image` if possible, otherwise create our own wrapper which generates a `srcset`. */} |
| 153 | + {/* eslint-disable-next-line @next/next/no-img-element */} |
| 154 | + <img |
| 155 | + alt="" |
| 156 | + className={cn("size-full object-cover", isSelected ? undefined : "border")} |
| 157 | + src={url} |
| 158 | + /> |
| 159 | + <figcaption className="truncate">{objectName}</figcaption> |
| 160 | + </figure> |
| 161 | + ); |
| 162 | + }} |
| 163 | + </ListBoxItem> |
| 164 | + ); |
| 165 | + }} |
| 166 | + </ListBox> |
| 167 | + ); |
| 168 | +} |
0 commit comments