import { Check, Copy, Download, Grid3X3, Link2 } from 'lucide-react'; import { useEffect, useMemo, useRef, useState } from 'react'; import { resolveAssetReadUrl } from '../../services/assetReadUrlService'; import { isWechatMiniProgramWebViewRuntime } from '../../services/authService'; import { copyTextToClipboard } from '../../services/clipboard'; import { canUseWechatMiniProgramShareGrid, openWechatMiniProgramShareGridPage, } from '../../services/wechatMiniProgramShareGrid'; import { useAuthUi } from '../auth/AuthUiContext'; import { ResolvedAssetImage } from '../ResolvedAssetImage'; import { downloadPublishShareCardImage } from './publishShareCardImage'; import { buildPublishShareCopyUrl, type PublishShareModalPayload, } from './publishShareModalModel'; import { PlatformUtilityInfoModal } from './PlatformUtilityInfoModal'; type PublishShareModalProps = { open: boolean; payload: PublishShareModalPayload | null; onClose: () => void; }; type ActionState = 'idle' | 'success' | 'failed'; function normalizePayloadTitle(payload: PublishShareModalPayload | null) { return payload?.title.trim() || '我的作品'; } function resolvePayloadCoverImageSrc(payload: PublishShareModalPayload | null) { return ( payload?.coverImageSrc?.trim() || payload?.fallbackCoverImageSrc?.trim() || '' ); } function resolvePayloadWorkTypeLabel(payload: PublishShareModalPayload | null) { return payload?.workTypeLabel?.trim() || '互动作品'; } /** * 发布完成后的通用分享弹窗。 * 分享事实仍来自公开作品号与 stage;弹窗只负责把它表现成可复制、可下载的分享卡。 */ export function PublishShareModal({ open, payload, onClose, }: PublishShareModalProps) { const platformTheme = useAuthUi()?.platformTheme ?? 'light'; const [copyState, setCopyState] = useState('idle'); const [downloadState, setDownloadState] = useState('idle'); const [gridState, setGridState] = useState('idle'); const resetTimerRef = useRef(null); const shareCopyUrl = useMemo( () => payload ? buildPublishShareCopyUrl(payload, { miniProgramRuntime: isWechatMiniProgramWebViewRuntime(), }) : '', [payload], ); const title = normalizePayloadTitle(payload); const coverImageSrc = resolvePayloadCoverImageSrc(payload); const workTypeLabel = resolvePayloadWorkTypeLabel(payload); const showMiniProgramGridButton = canUseWechatMiniProgramShareGrid() && Boolean(coverImageSrc); useEffect( () => () => { if (resetTimerRef.current !== null) { window.clearTimeout(resetTimerRef.current); } }, [], ); useEffect(() => { setCopyState('idle'); setDownloadState('idle'); setGridState('idle'); }, [payload?.publicWorkCode]); const scheduleStateReset = () => { if (resetTimerRef.current !== null) { window.clearTimeout(resetTimerRef.current); } resetTimerRef.current = window.setTimeout(() => { resetTimerRef.current = null; setCopyState('idle'); setDownloadState('idle'); setGridState('idle'); }, 1400); }; const copyShareLink = () => { if (!shareCopyUrl) { return; } void copyTextToClipboard(shareCopyUrl).then((copied) => { setCopyState(copied ? 'success' : 'failed'); scheduleStateReset(); }); }; const resolveMiniProgramGridCover = async () => { if (!coverImageSrc) { return ''; } return await resolveAssetReadUrl(coverImageSrc, { expireSeconds: 600, }).catch(() => coverImageSrc); }; const downloadShareCard = () => { if (!payload) { return; } setDownloadState('idle'); void downloadPublishShareCardImage( { ...payload, title, workTypeLabel, coverImageSrc, }, coverImageSrc, ) .then((downloaded) => { setDownloadState(downloaded ? 'success' : 'failed'); scheduleStateReset(); }) .catch(() => { setDownloadState('failed'); scheduleStateReset(); }); }; const openMiniProgramGridDownload = () => { if (!payload || !coverImageSrc) { return; } setGridState('idle'); void resolveMiniProgramGridCover() .then((resolvedCoverImageSrc) => openWechatMiniProgramShareGridPage({ imageUrl: resolvedCoverImageSrc, title, publicWorkCode: payload.publicWorkCode, }), ) .then((opened) => { setGridState(opened ? 'success' : 'failed'); scheduleStateReset(); }) .catch(() => { setGridState('failed'); scheduleStateReset(); }); }; return ( {showMiniProgramGridButton ? ( ) : null} } >
{coverImageSrc ? ( ) : (
{Array.from(title)[0] ?? '陶'}
)}
{workTypeLabel}

{title}

{payload?.publicWorkCode}
); }