import * as React from 'react'; import { DescriptionComponentRenderer, useClipboard, useHistory, useNotification, useQueryParams, useStrapiApp, } from '@strapi/admin/strapi-admin'; import { IconButton, Tabs, Typography, Flex } from '@strapi/design-system'; import { Cross, Link as LinkIcon } from '@strapi/icons'; import { stringify } from 'qs'; import { useIntl } from 'react-intl'; import { Link, type To } from 'react-router-dom'; import { styled } from 'styled-components'; import { InjectionZone } from '../../components/InjectionZone'; import { DocumentActionButton } from '../../pages/EditView/components/DocumentActions'; import { DocumentStatus } from '../../pages/EditView/components/DocumentStatus'; import { getDocumentStatus } from '../../pages/EditView/EditViewPage'; import { usePreviewContext } from '../pages/Preview'; import type { ContentManagerPlugin, DocumentActionProps } from '../../content-manager'; /* ------------------------------------------------------------------------------------------------- * ClosePreviewButton * -----------------------------------------------------------------------------------------------*/ const ClosePreviewButton = () => { const [{ query }] = useQueryParams<{ plugins?: Record; }>(); const { formatMessage } = useIntl(); const canGoBack = useHistory('BackButton', (state) => state.canGoBack); const goBack = useHistory('BackButton', (state) => state.goBack); const history = useHistory('BackButton', (state) => state.history); const locationIndex = useHistory('BackButton', (state) => state.currentLocationIndex); /** * Get the link destination from the history. * Rely on a fallback (the parent edit view page) if there's no page to go back . */ const historyTo = canGoBack ? history.at(locationIndex - 2) : undefined; const fallback = { pathname: '..', search: stringify(query, { encode: false }), } satisfies To; const toWithFallback = historyTo ?? fallback; const handleClick = (e: React.MouseEvent) => { if (canGoBack) { // Prevent normal link behavior, go back in the history stack instead e.preventDefault(); goBack(); return; } // Otherwise rely on native link behavior to go back to the edit view. We don't use navigate() // here in order to get the relative="path" functionality from the Link component. }; return ( ); }; /* ------------------------------------------------------------------------------------------------- * Status * -----------------------------------------------------------------------------------------------*/ const Status = () => { // Get status const document = usePreviewContext('PreviewHeader', (state) => state.document); const schema = usePreviewContext('PreviewHeader', (state) => state.schema); const meta = usePreviewContext('PreviewHeader', (state) => state.meta); const hasDraftAndPublished = schema?.options?.draftAndPublish ?? false; if (!hasDraftAndPublished) { return null; } const status = getDocumentStatus(document, meta); return ; }; const PreviewTabs = () => { const { formatMessage } = useIntl(); // URL query params const [{ query }, setQuery] = useQueryParams<{ status: 'draft' | 'published' }>(); // Get status const document = usePreviewContext('PreviewHeader', (state) => state.document); const schema = usePreviewContext('PreviewHeader', (state) => state.schema); const meta = usePreviewContext('PreviewHeader', (state) => state.meta); const hasDraftAndPublish = schema?.options?.draftAndPublish ?? false; const documentStatus = getDocumentStatus(document, meta); const handleTabChange = (status: string) => { if (status === 'published' || status === 'draft') { setQuery({ status }, 'push', true); } }; if (!hasDraftAndPublish) { return null; } return ( {formatMessage({ id: 'content-manager.containers.List.draft', defaultMessage: 'draft', })} {formatMessage({ id: 'content-manager.containers.List.published', defaultMessage: 'published', })} ); }; /* ------------------------------------------------------------------------------------------------- * PreviewHeader * -----------------------------------------------------------------------------------------------*/ const PreviewHeader = () => { const title = usePreviewContext('PreviewHeader', (state) => state.title); const document = usePreviewContext('PreviewHeader', (state) => state.document); const schema = usePreviewContext('PreviewHeader', (state) => state.schema); const meta = usePreviewContext('PreviewHeader', (state) => state.meta); const plugins = useStrapiApp('PreviewHeader', (state) => state.plugins); const [{ query }] = useQueryParams<{ status?: 'draft' | 'published'; }>(); const { formatMessage } = useIntl(); const { toggleNotification } = useNotification(); const { copy } = useClipboard(); const handleCopyLink = () => { copy(window.location.href); toggleNotification({ message: formatMessage({ id: 'content-manager.preview.copy.success', defaultMessage: 'Copied preview link', }), type: 'success', }); }; const activeTab = query.status ?? null; const hasDraftAndPublish = schema.options?.draftAndPublish ?? false; const documentActionProps = { activeTab, collectionType: schema.kind === 'collectionType' ? 'collection-types' : 'single-types', model: schema.uid, documentId: schema.kind === 'collectionType' ? document.documentId : undefined, document, meta, } satisfies DocumentActionProps; return ( {/* Title and status */} {title} {/* Tabs and actions */} {(actions) => { const filteredActions = actions.filter((action) => [action.position].flat().includes('preview') ); const [primaryAction, secondaryAction] = filteredActions; if (!primaryAction && !secondaryAction) return null; // Both actions are available when draft and publish enabled if (primaryAction && secondaryAction) { return ( <> {/* Save */} {/* Publish */} ); } // Otherwise we just have the save action return ( ); }} ); }; const PreviewTitle = styled(Typography)` overflow: hidden; text-overflow: ellipsis; white-space: nowrap; `; const StatusTab = styled(Tabs.Trigger)` text-transform: uppercase; `; const TitleContainer = styled(Flex)` border-right: 1px solid ${({ theme }) => theme.colors.neutral150}; `; export { PreviewHeader };