'use client' import { useEffect, useRef, useState } from 'react' import { createPortal } from 'react-dom' import { CopyLinkButton } from '@/components/CopyLinkButton' import { WhatsAppIcon } from '@/components/socialIcons' import { sendContentPieceEvent } from '@/lib/contentEvents' import type { ContentPiecePublicItem } from '@/lib/contentPieceCatalog' import { buildContentPieceWhatsAppUrl, contentPieceShareLink, contentPieceVoteMessage, } from '@/lib/contentPieceShare' import { cn } from '@/lib/utils' import { CONTENT_PIECE_FOCUS, CONTENT_PIECE_OUTLINE_BUTTON, CONTENT_PIECE_PRIMARY_BUTTON, } from './contentPieceClasses' /** * S42 — what the sheet actually reads: the catalogue passes the full public * item and the home section passes its lean projection (the same structural * precedent as `ContentPieceMedia`), so the one share mechanism serves both * surfaces without the home carrying the search haystack. */ type ContentPieceShareSheetItem = Pick< ContentPiecePublicItem, 'slug' | 'title' | 'type' | 'file' | 'isLink' | 'sourceUrl' > /** * S27 — the share sheet (artefato: cenas 03/08): the vote message prefilled and * EDITABLE, then `wa.me` of the sender's own WhatsApp — no recipient, nothing * sent by the system. "A mídia" opens the native share sheet with the file * where the device supports it and downloads the same file where it does not * (never pretending the file was attached). The sheet portals to the body so * no card overflow can clip it. */ export const ContentPieceShareSheet = ({ item, onClose, }: { item: ContentPieceShareSheetItem onClose: () => void }) => { const [mounted, setMounted] = useState(false) const [preparing, setPreparing] = useState(false) const textareaRef = useRef(null) const shareLink = contentPieceShareLink(item, window.location.origin) const [message, setMessage] = useState(() => contentPieceVoteMessage(item.type, item.title, shareLink), ) useEffect(() => { setMounted(true) // Focus moves into the dialog on open and back to the trigger on close. const previousFocus = document.activeElement as HTMLElement | null textareaRef.current?.focus() const onKeyDown = (event: KeyboardEvent) => { if (event.key === 'Escape') onClose() } window.addEventListener('keydown', onKeyDown) return () => { window.removeEventListener('keydown', onKeyDown) previousFocus?.focus?.() } }, [onClose]) const shareMedia = async () => { const mediaFile = item.file if (!mediaFile || preparing) return setPreparing(true) const path = mediaFile.path try { const response = await fetch(path) if (!response.ok) throw new Error('fetch failed') const blob = await response.blob() const file = new File([blob], mediaFile.downloadFilename, { type: blob.type || 'application/octet-stream', }) if (typeof navigator.canShare === 'function' && navigator.canShare({ files: [file] })) { try { await navigator.share({ files: [file], title: item.title }) return } catch (error) { // A cancelled share sheet is not a failure: never download instead. if (error instanceof DOMException && error.name === 'AbortError') return } } } catch { // The honest fallback below is the download of the same file. } finally { setPreparing(false) } const anchor = document.createElement('a') anchor.href = `${path}?download=1` anchor.download = mediaFile.downloadFilename anchor.click() } if (!mounted) return null return createPortal( // The portal lands outside the `campaign-site` layout, so the sheet carries // its own theme: without it every campaign token resolves to nothing (the // default `:root` foreground is near-white) and the panel fades away.