{"version":3,"file":"index.mjs","names":["ConfirmDialog"],"sources":["../../src/screen-revisions/index.tsx"],"sourcesContent":["import { __, sprintf } from '@wordpress/i18n';\nimport {\n\t__experimentalConfirmDialog as ConfirmDialog,\n\tuseNavigator,\n} from '@wordpress/components';\nimport { useCallback, useContext, useMemo, useState } from '@wordpress/element';\nimport { areGlobalStylesEqual } from '@wordpress/global-styles-engine';\nimport type { ActionButton, View } from '@wordpress/dataviews';\nimport { ScreenHeader } from '../screen-header';\nimport { GlobalStylesContext } from '../context';\nimport useGlobalStylesRevisions from './use-global-styles-revisions';\nimport RevisionsList from './revisions-list';\nimport type { Revision } from './types';\n\nconst PAGE_SIZE = 10;\nconst EMPTY_ARRAY: string[] = [];\n\nconst DEFAULT_VIEW: View = {\n\ttype: 'pickerActivity',\n\ttitleField: 'date',\n\tdescriptionField: 'details',\n\tlayout: { density: 'compact' },\n\tpage: 1,\n\tperPage: PAGE_SIZE,\n};\n\nfunction ScreenRevisions() {\n\tconst { user: currentEditorGlobalStyles, onChange: setUserConfig } =\n\t\tuseContext( GlobalStylesContext );\n\tconst { params, goTo } = useNavigator();\n\tconst { revisionId } = params;\n\tconst [ view, setView ] = useState< View >( DEFAULT_VIEW );\n\tconst query = useMemo(\n\t\t() => ( {\n\t\t\tper_page: view.perPage ?? PAGE_SIZE,\n\t\t\tpage: view.page ?? 1,\n\t\t} ),\n\t\t[ view.perPage, view.page ]\n\t);\n\tconst { revisions, isLoading, hasUnsavedChanges, revisionsCount } =\n\t\tuseGlobalStylesRevisions( { query } );\n\n\tconst paginationInfo = useMemo(\n\t\t() => ( {\n\t\t\ttotalItems: revisionsCount,\n\t\t\ttotalPages: Math.ceil( revisionsCount / query.per_page ),\n\t\t} ),\n\t\t[ revisionsCount, query.per_page ]\n\t);\n\n\tconst [\n\t\tisLoadingRevisionWithUnsavedChanges,\n\t\tsetIsLoadingRevisionWithUnsavedChanges,\n\t] = useState( false );\n\n\t// The screen holds a single page of revisions, so the revision the path\n\t// names is selected only while this page carries it. Paginating away from\n\t// it selects nothing, rather than a stand-in the user never picked. With\n\t// no revision in the path, the active entry is selected.\n\tconst currentlySelectedRevision = useMemo( () => {\n\t\tif ( revisionId ) {\n\t\t\treturn revisions.find(\n\t\t\t\t( revision ) => String( revision.id ) === String( revisionId )\n\t\t\t);\n\t\t}\n\t\treturn query.page === 1 ? revisions[ 0 ] : undefined;\n\t}, [ revisionId, revisions, query.page ] );\n\n\t// The entry for the styles the editor shows is the first one of page one:\n\t// the unsaved changes when there are any, else the latest revision.\n\tconst activeRevisionId = query.page === 1 ? revisions[ 0 ]?.id : undefined;\n\n\t// A revision is applicable when it isn't the styles the editor already\n\t// shows. The footer action's eligibility and the list's Active badge both\n\t// read this, so the button and the badge can't contradict each other. The\n\t// active entry is recognised by id as well as by payload, as the two\n\t// endpoints need not serialize the same styles identically.\n\tconst isRevisionApplicable = useCallback(\n\t\t( revision: Revision ) =>\n\t\t\t'unsaved' !== revision.id &&\n\t\t\trevision.id !== activeRevisionId &&\n\t\t\t! areGlobalStylesEqual( revision, currentEditorGlobalStyles ),\n\t\t[ activeRevisionId, currentEditorGlobalStyles ]\n\t);\n\n\tconst isSelectedRevisionApplicable =\n\t\t!! currentlySelectedRevision &&\n\t\tisRevisionApplicable( currentlySelectedRevision );\n\n\t// Both the back arrow and Apply leave the revisions screen. Selecting a\n\t// revision appends its id to the path (`/revisions/12`), so the navigator's\n\t// default back action would only strip the id and leave the user on the\n\t// same screen. Go straight to the root screen instead.\n\tconst closeRevisions = useCallback( () => {\n\t\tgoTo( '/', { isBack: true } );\n\t}, [ goTo ] );\n\n\tconst restoreRevision = useCallback(\n\t\t( revision?: Revision ) => {\n\t\t\t// The confirmation dialog can outlive its revision: the page\n\t\t\t// reloads or changes underneath it, and the selection is gone by\n\t\t\t// the time the user confirms. Close the dialog and apply nothing.\n\t\t\tsetIsLoadingRevisionWithUnsavedChanges( false );\n\t\t\tif ( ! revision ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tsetUserConfig( revision );\n\t\t\tcloseRevisions();\n\t\t},\n\t\t[ setUserConfig, closeRevisions ]\n\t);\n\n\tconst currentlySelectedRevisionId = currentlySelectedRevision?.id;\n\n\tconst selection = useMemo(\n\t\t() =>\n\t\t\tcurrentlySelectedRevisionId !== undefined\n\t\t\t\t? [ String( currentlySelectedRevisionId ) ]\n\t\t\t\t: EMPTY_ARRAY,\n\t\t[ currentlySelectedRevisionId ]\n\t);\n\n\tconst onChangeSelection = useCallback(\n\t\t( newSelection: string[] ) => {\n\t\t\t// The picker's single selection is clearable: clicking the selected\n\t\t\t// item again emits an empty selection. Keep the current revision\n\t\t\t// selected in that case so the timeline never ends up with nothing\n\t\t\t// selected.\n\t\t\tif ( ! newSelection.length ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tgoTo( `/revisions/${ newSelection[ newSelection.length - 1 ] }` );\n\t\t},\n\t\t[ goTo ]\n\t);\n\n\tconst onApplyRevision = useCallback( () => {\n\t\tif ( hasUnsavedChanges ) {\n\t\t\tsetIsLoadingRevisionWithUnsavedChanges( true );\n\t\t\treturn;\n\t\t}\n\t\trestoreRevision( currentlySelectedRevision );\n\t}, [ hasUnsavedChanges, restoreRevision, currentlySelectedRevision ] );\n\n\t// Apply / Reset to defaults render in the picker footer, outside the\n\t// timeline listbox, so the options hold no interactive content.\n\tconst actions: ActionButton< Revision >[] = useMemo(\n\t\t() => [\n\t\t\t{\n\t\t\t\tid: 'apply-revision',\n\t\t\t\tlabel: ( items: Revision[] ) =>\n\t\t\t\t\titems[ 0 ]?.id === 'parent' ? __( 'Reset' ) : __( 'Apply' ),\n\t\t\t\tisPrimary: true,\n\t\t\t\tisEligible: isRevisionApplicable,\n\t\t\t\tcallback: onApplyRevision,\n\t\t\t},\n\t\t],\n\t\t[ isRevisionApplicable, onApplyRevision ]\n\t);\n\n\treturn (\n\t\t<>\n\t\t\t<ScreenHeader\n\t\t\t\ttitle={\n\t\t\t\t\trevisionsCount\n\t\t\t\t\t\t? sprintf(\n\t\t\t\t\t\t\t\t// translators: %d: number of revisions.\n\t\t\t\t\t\t\t\t__( 'Revisions (%d)' ),\n\t\t\t\t\t\t\t\trevisionsCount\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t: __( 'Revisions' )\n\t\t\t\t}\n\t\t\t\tdescription={ __(\n\t\t\t\t\t'Click on previously saved styles to preview them. To restore a selected version to the editor, hit \"Apply.\" When you\\'re ready, use the Save button to save your changes.'\n\t\t\t\t) }\n\t\t\t\tonBack={ closeRevisions }\n\t\t\t/>\n\t\t\t<RevisionsList\n\t\t\t\trevisions={ revisions }\n\t\t\t\tview={ view }\n\t\t\t\tonChangeView={ setView }\n\t\t\t\tselection={ selection }\n\t\t\t\tonChangeSelection={ onChangeSelection }\n\t\t\t\tisLoading={ isLoading }\n\t\t\t\tpaginationInfo={ paginationInfo }\n\t\t\t\tcanApplyRevision={ isSelectedRevisionApplicable }\n\t\t\t\tactions={ actions }\n\t\t\t/>\n\t\t\t{ isLoadingRevisionWithUnsavedChanges && (\n\t\t\t\t<ConfirmDialog\n\t\t\t\t\tisOpen={ isLoadingRevisionWithUnsavedChanges }\n\t\t\t\t\tconfirmButtonText={ __( 'Apply' ) }\n\t\t\t\t\tonConfirm={ () =>\n\t\t\t\t\t\trestoreRevision( currentlySelectedRevision )\n\t\t\t\t\t}\n\t\t\t\t\tonCancel={ () =>\n\t\t\t\t\t\tsetIsLoadingRevisionWithUnsavedChanges( false )\n\t\t\t\t\t}\n\t\t\t\t\tsize=\"medium\"\n\t\t\t\t>\n\t\t\t\t\t{ __(\n\t\t\t\t\t\t'Are you sure you want to apply this revision? Any unsaved changes will be lost.'\n\t\t\t\t\t) }\n\t\t\t\t</ConfirmDialog>\n\t\t\t) }\n\t\t</>\n\t);\n}\n\nexport default ScreenRevisions;\n"],"mappings":";;;;;;;;;;AAcA,MAAM,YAAY;AAClB,MAAM,cAAwB,CAAC;AAE/B,MAAM,eAAqB;CAC1B,MAAM;CACN,YAAY;CACZ,kBA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