import * as React from 'react' import {__, _x} from '@wordpress/i18n' import { useReducer, } from '@wordpress/element' import { __experimentalUseNavigator as useNavigator, } from '@wordpress/components' import { cog as cogIcon, seen as eyeIcon, details as detailsIcon, } from '@wordpress/icons' import { Button, ButtonGroup, } from '@ska/components' import { IS_SETTINGS_PAGE, sanitizeURIComponent, } from '@ska/utils' import { RecordEditor, } from '../../../components' import { useSkaBlocksDispatch, } from '../../../tailwind/reducer' import { usePluginPreference, } from '../../../store' import type { tBlockAttributes, tBlockEditProps, } from '@ska/shared' import { getSelectorDisplayName, } from '..' import { SelectorsDetailsModal, } from '.' const NO_SELECTORS = {} export interface SelectorNavProps { attributes: tBlockAttributes setAttributes: tBlockEditProps['setAttributes'] } const IS_SMALL = !IS_SETTINGS_PAGE const SelectorNav: React.FC = ({ attributes, setAttributes, }) => { const navigator = useNavigator() const [isOpen, toggleOpen] = useReducer(isOpen => !isOpen, false) const [detailsModalOpen, toggleDetailsModalOpen] = useReducer(isOpen => !isOpen, false) const [showNestedSelectors, setShowNestedSelectors] = usePluginPreference('showNestedSelectors') const { skaBlocksSelectors = {}, } = attributes const dispatch = useSkaBlocksDispatch(attributes, setAttributes) const selectors = Object.keys(skaBlocksSelectors).filter(v => v) if(!selectors.length) { return null } const currentPath = navigator?.location?.path || '/' const nextPath = currentPath === '/' ? '/selector' : currentPath const OPTIONS = selectors.map(selector => ({ label: getSelectorDisplayName(selector), value: `${nextPath}/${sanitizeURIComponent(selector)}`, })) return <> { if(nextSelector) { navigator.goTo(nextSelector) } else { navigator.goTo('/') } }} prepend={<>