import { __, sprintf } from '@wordpress/i18n' import { createInterpolateElement } from '@wordpress/element' import React, { useCallback, useMemo } from 'react' import classnames from 'classnames' import { useHorizontalScrollOverflow } from '../../../hooks/useHorizontalScrollOverflow' import { WithRestAPIContext } from '../../../hooks/useRestAPI' import { useSnippetView } from '../../../hooks/useSnippetView' import { WithSnippetsAPIContext } from '../../../hooks/useSnippetsAPI' import { WithSnippetsListContext, useSnippetsList } from '../../../hooks/useSnippetsList' import { SNIPPET_TYPES } from '../../../types/Snippet' import { isLicensed } from '../../../utils/screen' import { SNIPPET_TYPE_LABELS, getSnippetAddNewUrl, getSnippetType, isProType } from '../../../utils/snippets/snippets' import { buildUrl } from '../../../utils/urls' import { Badge } from '../../common/Badge' import { Notice } from '../../common/Notice' import { ScreenMetaSlot } from '../../common/ScreenMetaSlot' import { UpsellPage } from '../../common/UpsellDialog' import { WithSnippetsTableFilters, useSnippetsFilters } from './WithSnippetsTableFilters' import { WithFilteredSnippetsContext } from './WithFilteredSnippetsContext' import { SnippetsListTable } from './SnippetsListTable' import type { SnippetType } from '../../../types/Snippet' interface SnippetTypeTabProps { type?: SnippetType count?: number } const SnippetTypeTab: React.FC = ({ type, count }) => { const { currentType, setCurrentType } = useSnippetsFilters() const tabName = type ?? 'all' const isActive = type === currentType const isProLocked = type && isProType(type) && !isLicensed() return (
  • { event.preventDefault() setCurrentType(type) }} > {type && } {type ? SNIPPET_TYPE_LABELS[type] : <> {__('All Snippets', 'code-snippets')} {__('All', 'code-snippets')} } {count && !isProLocked && {count}} {isProLocked && {__('Pro', 'code-snippets')}}
  • ) } const SafeModeNotice = () => window.CODE_SNIPPETS_MANAGE?.isSafeModeActive ?

    {__('Warning:', 'code-snippets')}{'\n'} {__('Safe mode is active and snippets will not execute!', 'code-snippets')}{'\n'} {createInterpolateElement( __('Remove the CODE_SNIPPETS_SAFE_MODE constant from wp-config.php file to turn off safe mode.', 'code-snippets'), { code: } )}{'\n'} {__('Read more', 'code-snippets')}

    : null // Counts render immediately from the values localized with the page, then // switch to live values derived from the snippets list once it has loaded. const useSnippetTypeCounts = () => { const { snippetsList } = useSnippetsList() const countedSnippets = useMemo( () => snippetsList?.filter(snippet => !snippet.trashed), [snippetsList] ) const typeCounts = useMemo( () => countedSnippets?.reduce((counts, snippet) => { const type = getSnippetType(snippet) return counts.set(type, (counts.get(type) ?? 0) + 1) }, new Map()), [countedSnippets] ) const localized = window.CODE_SNIPPETS_MANAGE?.typeCounts const getCount = useCallback( (type?: SnippetType) => countedSnippets ? type ? typeCounts?.get(type) ?? 0 : countedSnippets.length : localized?.[type ?? 'all'], [countedSnippets, typeCounts, localized] ) return { getCount } } const SnippetsTableInner = () => { const { snippetView, setSnippetView } = useSnippetView() const { currentType } = useSnippetsFilters() const { getCount } = useSnippetTypeCounts() const { atStart, atEnd, scrollRef } = useHorizontalScrollOverflow() return ( <>

    {sprintf( // translators: %s: label of the currently selected snippet type. __('Local Snippets: %s', 'code-snippets'), currentType ? SNIPPET_TYPE_LABELS[currentType] : __('All Snippets', 'code-snippets') )}

    {__('Add Snippet', 'code-snippets')}

    {currentType && !isLicensed() && isProType(currentType) ? : } ) } export const SnippetsTable: React.FC = () =>