PluginProbe
Code Snippets / 4.0.0-beta.2
Code Snippets v4.0.0-beta.2
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 4.0.0-beta.2, at js/components/ManageMenu/ManageMenu.tsx

97 lines 3.0 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, type NoticeType } from '../common/Notice'
6 import { Toolbar } from '../common/Toolbar'
7 import { SUBPAGES, SUBPAGE_ENTRIES, type SubpageName } from './subpages'
8
9 const repositionTableOptionsSettings = () => {
10 const screenOptionsForm = document.getElementById('adv-settings')
11 const tableOptions = screenOptionsForm?.querySelector<HTMLFieldSetElement>('fieldset.table-options-prefs')
12 // Locate the first column-visibility fieldset that is NOT the table-options one.
13 // This relies on WordPress core rendering #adv-settings with .metabox-prefs fieldsets.
14 // Verified against WP 6.5+ (core/Screen_Options). If WP changes this structure the
15 // reordering will silently no-op, which is acceptable — it is a cosmetic improvement only.
16 const columns = Array.from(
17 screenOptionsForm?.querySelectorAll<HTMLFieldSetElement>('fieldset.metabox-prefs') ?? []
18 ).find(fieldset => !fieldset.classList.contains('table-options-prefs'))
19
20 if (screenOptionsForm && tableOptions && columns) {
21 screenOptionsForm.insertBefore(tableOptions, columns)
22 }
23 }
24
25 const getNotice = (result: string): { text: string, type: NoticeType } | undefined => {
26 switch (result) {
27 case 'deleted':
28 return { text: __('Snippet <strong>deleted</strong>.', 'code-snippets'), type: 'success' }
29
30 case 'executed':
31 return { text: __('Snippet <strong>executed</strong>.', 'code-snippets'), type: 'success' }
32
33 case 'run-once-failed':
34 return {
35 text: __('The snippet could not be run. Check that its code is valid and try again.', 'code-snippets'),
36 type: 'error'
37 }
38
39 case 'run-once-safe-mode':
40 return {
41 text: __('Snippet execution is disabled on this site, so the snippet was not run.', 'code-snippets'),
42 type: 'warning'
43 }
44
45 default:
46 return undefined
47 }
48 }
49
50 const PageNotices = () => {
51 const [notice, setNotice] = useState(() => {
52 const result = fetchQueryParam('result')
53 updateQueryParams({ result: undefined })
54 return result ? getNotice(result) : undefined
55 })
56
57 return notice
58 ? <DismissibleNotice
59 className="code-snippets-notice"
60 onDismiss={() => {
61 setNotice(undefined)
62 }}
63 type={notice.type}>
64 <p>{createInterpolateElement(notice.text, { strong: <strong /> })}</p>
65 </DismissibleNotice>
66 : null
67 }
68
69 interface PageContentParams {
70 subpage: SubpageName
71 }
72
73 const PageContent: React.FC<PageContentParams> = ({ subpage }) => {
74 const { Component } = SUBPAGE_ENTRIES[subpage]
75
76 return <Component />
77 }
78
79 export const ManageMenu = () => {
80 const subpage = useMemo(() =>
81 fetchConstQueryParam('subpage', SUBPAGES) ?? SUBPAGES[0], [])
82
83 useEffect(() => {
84 if ('snippets' === subpage) {
85 repositionTableOptionsSettings()
86 }
87 }, [subpage])
88
89 return (
90 <>
91 <Toolbar />
92 <PageNotices />
93 <PageContent subpage={subpage} />
94 </>
95 )
96 }
97