PluginProbe
Code Snippets / 3.10.0-beta.1
Code Snippets v3.10.0-beta.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / components / ManageMenu / ManageMenu.tsx

ManageMenu.tsx in Code Snippets 3.10.0-beta.1, at js/components/ManageMenu/ManageMenu.tsx

92 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { useEffect, useMemo, useState } from 'react'
2 import { __ } from '@wordpress/i18n'
3 import { createInterpolateElement } from '@wordpress/element'
4 import { fetchConstQueryParam, fetchQueryParam, updateQueryParams } from '../../utils/urls'
5 import { DismissibleNotice } from '../common/Notice'
6 import { SUBPAGES, Toolbar } from '../common/Toolbar'
7 import { UpsellPage } from '../common/UpsellDialog'
8 import { CommunityCloud } from './CommunityCloud/CommunityCloud'
9 import { SnippetsTable } from './SnippetsTable'
10
11 const repositionTableOptionsSettings = () => {
12 const screenOptionsForm = document.getElementById('adv-settings')
13 const tableOptions = screenOptionsForm?.querySelector<HTMLFieldSetElement>('fieldset.table-options-prefs')
14 // Locate the first column-visibility fieldset that is NOT the table-options one.
15 // This relies on WordPress core rendering #adv-settings with .metabox-prefs fieldsets.
16 // Verified against WP 6.5+ (core/Screen_Options). If WP changes this structure the
17 // reordering will silently no-op, which is acceptable — it is a cosmetic improvement only.
18 const columns = Array.from(
19 screenOptionsForm?.querySelectorAll<HTMLFieldSetElement>('fieldset.metabox-prefs') ?? []
20 ).find(fieldset => !fieldset.classList.contains('table-options-prefs'))
21
22 if (screenOptionsForm && tableOptions && columns) {
23 screenOptionsForm.insertBefore(tableOptions, columns)
24 }
25 }
26
27 const getNoticeText = (result: string) => {
28 switch (result) {
29 case 'deleted':
30 return __('Snippet <strong>deleted</strong>.', 'code-snippets')
31
32 default:
33 return undefined
34 }
35 }
36
37 const PageNotices = () => {
38 const [noticeText, setNoticeText] = useState(() => {
39 const result = fetchQueryParam('result')
40 updateQueryParams({ result: undefined })
41 return result && getNoticeText(result)
42 })
43
44 return noticeText
45 ? <DismissibleNotice
46 className="code-snippets-notice"
47 onDismiss={() => {
48 setNoticeText(undefined)
49 }}
50 type="success">
51 <p>{createInterpolateElement(noticeText, { strong: <strong /> })}</p>
52 </DismissibleNotice>
53 : null
54 }
55
56 interface PageContentParams {
57 subpage: typeof SUBPAGES[number]
58 }
59
60 const PageContent: React.FC<PageContentParams> = ({ subpage }) => {
61 switch (subpage) {
62 case 'snippets':
63 return <SnippetsTable />
64
65 case 'cloud-community':
66 return <CommunityCloud />
67
68 case 'blueprints':
69 case 'cloud-library':
70 return <UpsellPage />
71 }
72 }
73
74 export const ManageMenu = () => {
75 const subpage = useMemo(() =>
76 fetchConstQueryParam('subpage', SUBPAGES) ?? SUBPAGES[0], [])
77
78 useEffect(() => {
79 if ('snippets' === subpage) {
80 repositionTableOptionsSettings()
81 }
82 }, [subpage])
83
84 return (
85 <>
86 <Toolbar />
87 <PageNotices />
88 <PageContent subpage={subpage} />
89 </>
90 )
91 }
92