'use client'; import { BriefcaseBusiness, Check, Copy, Mail, MessageCircle, Send, Share2, X, } from 'lucide-react'; import { useCallback, useEffect, useRef, useState, type MouseEvent, } from 'react'; import { messagesByLocale, type Locale } from '@/i18n/content'; import styles from './CaseStudyDrawer.module.css'; type CopyState = 'idle' | 'copied' | 'error'; export function CaseStudyShare({ locale }: { locale: Locale }) { const copy = messagesByLocale[locale].projects; const triggerRef = useRef(null); const dialogRef = useRef(null); const closeRef = useRef(null); const skipMotionRef = useRef(false); const [fallbackOpen, setFallbackOpen] = useState(false); const [copyState, setCopyState] = useState('idle'); const [shareData, setShareData] = useState({ title: '', url: '' }); useEffect(() => { const dialog = dialogRef.current; if (!dialog) return; if (fallbackOpen && !dialog.open) { dialog.dataset.motion = skipMotionRef.current ? 'none' : 'standard'; dialog.showModal(); requestAnimationFrame(() => { dialog.dataset.state = 'open'; closeRef.current?.focus(); }); } if (!fallbackOpen && dialog.open) dialog.close(); }, [fallbackOpen]); const openFallback = useCallback((skipMotion: boolean) => { skipMotionRef.current = skipMotion; setCopyState('idle'); setShareData({ title: document.title, url: window.location.href }); setFallbackOpen(true); }, []); const closeFallback = useCallback(() => { const dialog = dialogRef.current; if (!dialog?.open) return; const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; if (reduceMotion || dialog.dataset.motion === 'none') { dialog.close(); setFallbackOpen(false); triggerRef.current?.focus({ preventScroll: true }); return; } dialog.dataset.state = 'closing'; window.setTimeout(() => { dialog.close(); setFallbackOpen(false); triggerRef.current?.focus({ preventScroll: true }); }, 140); }, []); const handleShare = async (event: MouseEvent) => { const data = { title: document.title, url: window.location.href }; const useNativeShare = window.matchMedia('(max-width: 1100px)').matches && typeof navigator.share === 'function'; if (useNativeShare) { try { await navigator.share(data); return; } catch (error) { if (error instanceof DOMException && error.name === 'AbortError') return; } } openFallback(event.detail === 0); }; const copyCurrentLink = async () => { try { await navigator.clipboard.writeText(shareData.url); setCopyState('copied'); } catch { setCopyState('error'); } }; const encodedUrl = encodeURIComponent(shareData.url); const encodedTitle = encodeURIComponent(shareData.title); const encodedMessage = encodeURIComponent(`${shareData.title} ${shareData.url}`); const feedback = copyState === 'copied' ? copy.linkCopied : copyState === 'error' ? copy.copyFailed : ''; return ( <> { event.preventDefault(); closeFallback(); }} onClick={(event) => { if (event.target === event.currentTarget) closeFallback(); }} >
event.stopPropagation()}>

{copy.shareDialogTitle}

{copy.shareDialogDescription}

{feedback}
); }