'use client' import type { CollectionPermission, GlobalPermission, SanitizedCollectionConfig } from 'payload' import { getTranslation } from '@payloadcms/translations' import React, { Fragment, useEffect } from 'react' import { useComponentMap } from '../../providers/ComponentMap/index.js' import { useConfig } from '../../providers/Config/index.js' import { useTranslation } from '../../providers/Translation/index.js' import { formatAdminURL } from '../../utilities/formatAdminURL.js' import { formatDate } from '../../utilities/formatDate.js' import { Autosave } from '../Autosave/index.js' import { DeleteDocument } from '../DeleteDocument/index.js' import { DuplicateDocument } from '../DuplicateDocument/index.js' import { Gutter } from '../Gutter/index.js' import { Popup, PopupList } from '../Popup/index.js' import { PreviewButton } from '../PreviewButton/index.js' import { PublishButton } from '../PublishButton/index.js' import { SaveButton } from '../SaveButton/index.js' import { SaveDraftButton } from '../SaveDraftButton/index.js' import { Status } from '../Status/index.js' import './index.scss' const baseClass = 'doc-controls' export const DocumentControls: React.FC<{ apiURL: string data?: any disableActions?: boolean hasPublishPermission?: boolean hasSavePermission?: boolean id?: number | string isAccountView?: boolean isEditing?: boolean permissions: CollectionPermission | GlobalPermission | null slug: SanitizedCollectionConfig['slug'] }> = (props) => { const { id, slug, data, disableActions, hasSavePermission, isAccountView, isEditing, permissions, } = props const { i18n } = useTranslation() const config = useConfig() const { getComponentMap } = useComponentMap() const collectionConfig = config.collections.find((coll) => coll.slug === slug) const globalConfig = config.globals.find((global) => global.slug === slug) const componentMap = getComponentMap({ collectionSlug: collectionConfig?.slug, globalSlug: globalConfig?.slug, }) const { admin: { dateFormat }, routes: { admin: adminRoute }, } = config // Settings these in state to avoid hydration issues if there is a mismatch between the server and client const [updatedAt, setUpdatedAt] = React.useState('') const [createdAt, setCreatedAt] = React.useState('') useEffect(() => { if (data?.updatedAt) { setUpdatedAt(formatDate({ date: data.updatedAt, i18n, pattern: dateFormat })) } if (data?.createdAt) { setCreatedAt(formatDate({ date: data.createdAt, i18n, pattern: dateFormat })) } }, [data, i18n, dateFormat]) const hasCreatePermission = permissions && 'create' in permissions && permissions.create?.permission const hasDeletePermission = permissions && 'delete' in permissions && permissions.delete?.permission const showDotMenu = Boolean( collectionConfig && id && !disableActions && (hasCreatePermission || hasDeletePermission), ) const unsavedDraftWithValidations = !id && collectionConfig?.versions?.drafts && collectionConfig.versions?.drafts.validate return (
    {collectionConfig && !isEditing && !isAccountView && (
  • {i18n.t('general:creatingNewLabel', { label: getTranslation( collectionConfig?.labels?.singular ?? i18n.t('general:document'), i18n, ), })}

  • )} {(collectionConfig?.versions?.drafts || globalConfig?.versions?.drafts) && ( {(globalConfig || (collectionConfig && isEditing)) && (
  • )} {((collectionConfig?.versions?.drafts && collectionConfig?.versions?.drafts?.autosave && !unsavedDraftWithValidations) || (globalConfig?.versions?.drafts && globalConfig?.versions?.drafts?.autosave)) && hasSavePermission && (
  • )}
    )} {collectionConfig?.timestamps && (isEditing || isAccountView) && (
  • {i18n.t('general:lastModified')}: 

    {data?.updatedAt &&

    {updatedAt}

    }
  • {i18n.t('general:created')}: 

    {data?.createdAt &&

    {createdAt}

    }
  • )}
{componentMap?.isPreviewEnabled && ( )} {hasSavePermission && ( {collectionConfig?.versions?.drafts || globalConfig?.versions?.drafts ? ( {((collectionConfig?.versions?.drafts && !collectionConfig?.versions?.drafts?.autosave) || unsavedDraftWithValidations || (globalConfig?.versions?.drafts && !globalConfig?.versions?.drafts?.autosave)) && ( )} ) : ( )} )}
{showDotMenu && (
} className={`${baseClass}__popup`} horizontalAlign="right" size="large" verticalAlign="bottom" > {hasCreatePermission && ( {i18n.t('general:createNew')} {!collectionConfig.disableDuplicate && isEditing && ( )} )} {hasDeletePermission && ( )} )}
) }