|
| 1 | +import { |
| 2 | + AnimatePresence, |
| 3 | + motion, |
| 4 | + useIsPresent, |
| 5 | + useReducedMotion, |
| 6 | +} from "motion/react"; |
| 7 | +import { galleryMotion } from "./use-image-gallery-motion"; |
| 8 | +import { useRef, useState, useLayoutEffect, type RefObject } from "react"; |
| 9 | +import { MediaReviewFrame } from "./MediaReviewFrame"; |
| 10 | +import { ImageReviewStage } from "./ImageReviewStage"; |
| 11 | +import { useMediaElementSource } from "./use-media-element-source"; |
| 12 | +import { VideoPlayer } from "./VideoPlayer"; |
| 13 | +import { AudioAttachment } from "./AudioAttachment"; |
| 14 | +import { FileAttachment } from "./FileAttachment"; |
| 15 | +import { AttachmentView } from "../conversation/AttachmentView"; |
| 16 | +import type { |
| 17 | + AttachmentRenderer, |
| 18 | + ContributionReader, |
| 19 | +} from "../conversation/contracts"; |
| 20 | +import type { Attachment } from "../relay/contracts"; |
| 21 | +import type { DraftAttachment } from "./attachment-draft"; |
| 22 | +import { |
| 23 | + draftAttachmentMedia, |
| 24 | + useDraftAttachmentSource, |
| 25 | +} from "./use-draft-attachment-source"; |
| 26 | +import { readReviewOrigin } from "./use-review-entrance"; |
| 27 | +import { IconButton } from "../../shared/design-system/ui/IconButton"; |
| 28 | +import { |
| 29 | + CaretLeftIcon, |
| 30 | + CaretRightIcon, |
| 31 | +} from "../../shared/design-system/icons"; |
| 32 | +import styles from "./Messages.module.css"; |
| 33 | + |
| 34 | +export function ComposerAttachmentViewer({ |
| 35 | + items, |
| 36 | + selected, |
| 37 | + select, |
| 38 | + media, |
| 39 | + renderers, |
| 40 | + restoreFocus, |
| 41 | + close, |
| 42 | +}: { |
| 43 | + items: readonly DraftAttachment[]; |
| 44 | + selected: string; |
| 45 | + select(id: string): void; |
| 46 | + media(url: string): string | undefined; |
| 47 | + renderers?: ContributionReader<AttachmentRenderer> | undefined; |
| 48 | + restoreFocus: RefObject<HTMLElement | null>; |
| 49 | + close(): void; |
| 50 | +}) { |
| 51 | + const index = items.findIndex((item) => item.id === selected); |
| 52 | + const item = items[index]; |
| 53 | + const frame = useRef<HTMLElement>(null); |
| 54 | + const video = useRef<HTMLVideoElement>(null); |
| 55 | + const [origin] = useState(() => readReviewOrigin(restoreFocus.current)); |
| 56 | + const [initial] = useState(selected); |
| 57 | + const [previewList] = useState(() => restoreFocus.current?.closest("ul")); |
| 58 | + const [initialSource] = useState( |
| 59 | + () => |
| 60 | + restoreFocus.current?.querySelector<HTMLImageElement | HTMLVideoElement>( |
| 61 | + "img, video", |
| 62 | + )?.src, |
| 63 | + ); |
| 64 | + const selectedPreview = () => |
| 65 | + Array.from( |
| 66 | + previewList?.querySelectorAll<HTMLButtonElement>( |
| 67 | + "[data-draft-preview]", |
| 68 | + ) ?? [], |
| 69 | + ).find((button) => button.dataset.draftPreview === selected); |
| 70 | + const returnOrigin = () => { |
| 71 | + const button = selectedPreview(); |
| 72 | + if (!button || !previewList) return undefined; |
| 73 | + const bounds = button.getBoundingClientRect(); |
| 74 | + const visible = previewList.getBoundingClientRect(); |
| 75 | + if ( |
| 76 | + bounds.left < visible.left || |
| 77 | + bounds.right > visible.right || |
| 78 | + bounds.top < visible.top || |
| 79 | + bounds.bottom > visible.bottom |
| 80 | + ) |
| 81 | + return undefined; |
| 82 | + button.dataset.reviewMotion = "pointer"; |
| 83 | + return readReviewOrigin(button); |
| 84 | + }; |
| 85 | + const [direction, setDirection] = useState(1); |
| 86 | + const instant = useReducedMotion(); |
| 87 | + if (!item) return null; |
| 88 | + const { video: isVideo } = draftAttachmentMedia(item); |
| 89 | + const navigate = (direction: -1 | 1) => { |
| 90 | + const next = items[index + direction]; |
| 91 | + if (!next) return; |
| 92 | + const target = Array.from( |
| 93 | + previewList?.querySelectorAll<HTMLButtonElement>( |
| 94 | + "[data-draft-preview]", |
| 95 | + ) ?? [], |
| 96 | + ).find((button) => button.dataset.draftPreview === next.id); |
| 97 | + if (target) restoreFocus.current = target; |
| 98 | + setDirection(direction); |
| 99 | + frame.current?.focus({ preventScroll: true }); |
| 100 | + select(next.id); |
| 101 | + }; |
| 102 | + return ( |
| 103 | + <MediaReviewFrame |
| 104 | + title={item.file.name} |
| 105 | + label="Attachment preview" |
| 106 | + isVideo={isVideo} |
| 107 | + video={video} |
| 108 | + frame={frame} |
| 109 | + origin={origin} |
| 110 | + returnOrigin={returnOrigin} |
| 111 | + restoreFocus={restoreFocus} |
| 112 | + close={close} |
| 113 | + navigate={items.length > 1 ? navigate : undefined} |
| 114 | + actions={ |
| 115 | + items.length > 1 && ( |
| 116 | + <> |
| 117 | + <IconButton |
| 118 | + size="compact" |
| 119 | + aria-label="Previous attachment" |
| 120 | + disabled={index === 0} |
| 121 | + onClick={() => navigate(-1)} |
| 122 | + icon={<CaretLeftIcon size={20} />} |
| 123 | + /> |
| 124 | + <span aria-live="polite"> |
| 125 | + {index + 1} / {items.length} |
| 126 | + </span> |
| 127 | + <IconButton |
| 128 | + size="compact" |
| 129 | + aria-label="Next attachment" |
| 130 | + disabled={index === items.length - 1} |
| 131 | + onClick={() => navigate(1)} |
| 132 | + icon={<CaretRightIcon size={20} />} |
| 133 | + /> |
| 134 | + </> |
| 135 | + ) |
| 136 | + } |
| 137 | + > |
| 138 | + <AnimatePresence initial={false} custom={direction}> |
| 139 | + <motion.div |
| 140 | + key={item.id} |
| 141 | + className={styles.draftMediaSlide} |
| 142 | + custom={direction} |
| 143 | + variants={{ |
| 144 | + enter: (direction: number) => ({ |
| 145 | + x: instant ? 0 : direction * galleryMotion.distance, |
| 146 | + opacity: instant ? 1 : 0, |
| 147 | + }), |
| 148 | + exit: (direction: number) => ({ |
| 149 | + x: instant ? 0 : -direction * galleryMotion.distance, |
| 150 | + opacity: 0, |
| 151 | + }), |
| 152 | + }} |
| 153 | + initial="enter" |
| 154 | + animate={{ x: 0, opacity: 1 }} |
| 155 | + exit="exit" |
| 156 | + transition={{ |
| 157 | + duration: instant ? 0 : galleryMotion.duration, |
| 158 | + ease: galleryMotion.ease, |
| 159 | + }} |
| 160 | + > |
| 161 | + <DraftSlide |
| 162 | + item={item} |
| 163 | + initialSource={initial === selected ? initialSource : undefined} |
| 164 | + media={media} |
| 165 | + renderers={renderers} |
| 166 | + video={video} |
| 167 | + /> |
| 168 | + </motion.div> |
| 169 | + </AnimatePresence> |
| 170 | + </MediaReviewFrame> |
| 171 | + ); |
| 172 | +} |
| 173 | + |
| 174 | +function DraftSlide(props: Parameters<typeof DraftMedia>[0]) { |
| 175 | + const present = useIsPresent(); |
| 176 | + const localVideo = useRef<HTMLVideoElement>(null); |
| 177 | + useLayoutEffect(() => { |
| 178 | + const element = localVideo.current; |
| 179 | + if (present) props.video.current = element; |
| 180 | + else element?.pause(); |
| 181 | + return () => { |
| 182 | + if (props.video.current === element) props.video.current = null; |
| 183 | + }; |
| 184 | + }, [present, props.video]); |
| 185 | + return ( |
| 186 | + <div |
| 187 | + className={styles.draftMediaContent} |
| 188 | + inert={!present} |
| 189 | + aria-hidden={!present || undefined} |
| 190 | + > |
| 191 | + <DraftMedia |
| 192 | + {...props} |
| 193 | + video={localVideo} |
| 194 | + activeVideo={present ? props.video : undefined} |
| 195 | + /> |
| 196 | + </div> |
| 197 | + ); |
| 198 | +} |
| 199 | + |
| 200 | +function DraftMedia({ |
| 201 | + item, |
| 202 | + media, |
| 203 | + renderers, |
| 204 | + video, |
| 205 | + activeVideo, |
| 206 | + initialSource, |
| 207 | +}: { |
| 208 | + item: DraftAttachment; |
| 209 | + media(url: string): string | undefined; |
| 210 | + renderers?: ContributionReader<AttachmentRenderer> | undefined; |
| 211 | + video: RefObject<HTMLVideoElement | null>; |
| 212 | + activeVideo?: RefObject<HTMLVideoElement | null> | undefined; |
| 213 | + initialSource?: string | undefined; |
| 214 | +}) { |
| 215 | + const [openedItem] = useState(item); |
| 216 | + const [failed, setFailed] = useState<string>(); |
| 217 | + const openedMedia = draftAttachmentMedia(openedItem); |
| 218 | + // A mounted slide owns its local URL until it closes. Failed local decoding |
| 219 | + // can recover through the uploaded representation once it is available. |
| 220 | + const keepLocal = |
| 221 | + !openedItem.uploaded && |
| 222 | + !failed && |
| 223 | + (openedMedia.image || |
| 224 | + openedMedia.video || |
| 225 | + openedMedia.type.startsWith("audio/") || |
| 226 | + !!openedItem.voice); |
| 227 | + const previewItem = keepLocal ? openedItem : item; |
| 228 | + const { |
| 229 | + source: resolvedSource, |
| 230 | + type, |
| 231 | + image, |
| 232 | + video: isVideo, |
| 233 | + } = useDraftAttachmentSource(previewItem, media); |
| 234 | + const source = previewItem.uploaded |
| 235 | + ? resolvedSource |
| 236 | + : (resolvedSource ?? initialSource); |
| 237 | + const sourceUnavailable = useMediaElementSource( |
| 238 | + isVideo ? source : undefined, |
| 239 | + ).unavailable; |
| 240 | + useLayoutEffect(() => { |
| 241 | + if (!activeVideo || !source) return; |
| 242 | + const element = video.current; |
| 243 | + activeVideo.current = element; |
| 244 | + return () => { |
| 245 | + if (activeVideo.current === element) activeVideo.current = null; |
| 246 | + }; |
| 247 | + }, [source, activeVideo, video]); |
| 248 | + const attachment: Attachment = { |
| 249 | + url: previewItem.uploaded?.url ?? source ?? item.id, |
| 250 | + name: item.uploaded?.name ?? item.file.name, |
| 251 | + mime: type, |
| 252 | + size: item.uploaded?.size ?? item.file.size, |
| 253 | + kind: image |
| 254 | + ? "image" |
| 255 | + : isVideo |
| 256 | + ? "video" |
| 257 | + : type.startsWith("audio/") |
| 258 | + ? "audio" |
| 259 | + : "file", |
| 260 | + ...(item.voice ? { voiceNote: true, ...item.voice } : {}), |
| 261 | + }; |
| 262 | + if (!source && attachment.kind === "file") |
| 263 | + return ( |
| 264 | + <p className={styles.mediaReviewUnavailable}> |
| 265 | + {item.file.name} |
| 266 | + <br /> |
| 267 | + No preview available for this file type. |
| 268 | + </p> |
| 269 | + ); |
| 270 | + if (!source || sourceUnavailable || failed === source) |
| 271 | + return ( |
| 272 | + <p className={styles.mediaReviewUnavailable} role="status"> |
| 273 | + Preview unavailable |
| 274 | + </p> |
| 275 | + ); |
| 276 | + if (image) |
| 277 | + return ( |
| 278 | + <ImageReviewStage |
| 279 | + attachments={[attachment]} |
| 280 | + selectedUrl={attachment.url} |
| 281 | + select={() => {}} |
| 282 | + media={() => source} |
| 283 | + onOpenLink={() => false} |
| 284 | + /> |
| 285 | + ); |
| 286 | + if (isVideo) |
| 287 | + return ( |
| 288 | + <VideoPlayer |
| 289 | + key={source} |
| 290 | + source={source} |
| 291 | + videoRef={video} |
| 292 | + onTime={() => {}} |
| 293 | + onError={() => setFailed(source)} |
| 294 | + /> |
| 295 | + ); |
| 296 | + const fallback = |
| 297 | + attachment.kind === "audio" ? ( |
| 298 | + <AudioAttachment key={source} attachment={attachment} source={source} /> |
| 299 | + ) : item.uploaded ? ( |
| 300 | + <FileAttachment |
| 301 | + attachment={attachment} |
| 302 | + source={source} |
| 303 | + onOpenLink={() => false} |
| 304 | + /> |
| 305 | + ) : ( |
| 306 | + <p className={styles.mediaReviewUnavailable}> |
| 307 | + {item.file.name} |
| 308 | + <br /> |
| 309 | + No preview available for this file type. |
| 310 | + </p> |
| 311 | + ); |
| 312 | + return renderers ? ( |
| 313 | + <AttachmentView |
| 314 | + registry={renderers} |
| 315 | + attachment={attachment} |
| 316 | + source={source} |
| 317 | + fallback={fallback} |
| 318 | + /> |
| 319 | + ) : ( |
| 320 | + fallback |
| 321 | + ); |
| 322 | +} |
0 commit comments