| 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 { 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 getNotice = (result: string): { text: string, type: NoticeType } | undefined => { |
| 28 |
switch (result) { |
| 29 |
case 'deleted': |
| 30 |
return { text: __('Snippet <strong>deleted</strong>.', 'code-snippets'), type: 'success' } |
| 31 |
|
| 32 |
case 'executed': |
| 33 |
return { text: __('Snippet <strong>executed</strong>.', 'code-snippets'), type: 'success' } |
| 34 |
|
| 35 |
case 'run-once-failed': |
| 36 |
return { |
| 37 |
text: __('The snippet could not be run. Check that its code is valid and try again.', 'code-snippets'), |
| 38 |
type: 'error' |
| 39 |
} |
| 40 |
|
| 41 |
case 'run-once-safe-mode': |
| 42 |
return { |
| 43 |
text: __('Safe mode is active, so the snippet was not run.', 'code-snippets'), |
| 44 |
type: 'warning' |
| 45 |
} |
| 46 |
|
| 47 |
default: |
| 48 |
return undefined |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
const PageNotices = () => { |
| 53 |
const [notice, setNotice] = useState(() => { |
| 54 |
const result = fetchQueryParam('result') |
| 55 |
updateQueryParams({ result: undefined }) |
| 56 |
return result ? getNotice(result) : undefined |
| 57 |
}) |
| 58 |
|
| 59 |
return notice |
| 60 |
? <DismissibleNotice |
| 61 |
className="code-snippets-notice" |
| 62 |
onDismiss={() => { |
| 63 |
setNotice(undefined) |
| 64 |
}} |
| 65 |
type={notice.type}> |
| 66 |
<p>{createInterpolateElement(notice.text, { strong: <strong /> })}</p> |
| 67 |
</DismissibleNotice> |
| 68 |
: null |
| 69 |
} |
| 70 |
|
| 71 |
interface PageContentParams { |
| 72 |
subpage: typeof SUBPAGES[number] |
| 73 |
} |
| 74 |
|
| 75 |
const PageContent: React.FC<PageContentParams> = ({ subpage }) => { |
| 76 |
switch (subpage) { |
| 77 |
case 'snippets': |
| 78 |
return <SnippetsTable /> |
| 79 |
|
| 80 |
case 'cloud-community': |
| 81 |
return <CommunityCloud /> |
| 82 |
|
| 83 |
case 'blueprints': |
| 84 |
case 'cloud-library': |
| 85 |
return <UpsellPage /> |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
export const ManageMenu = () => { |
| 90 |
const subpage = useMemo(() => |
| 91 |
fetchConstQueryParam('subpage', SUBPAGES) ?? SUBPAGES[0], []) |
| 92 |
|
| 93 |
useEffect(() => { |
| 94 |
if ('snippets' === subpage) { |
| 95 |
repositionTableOptionsSettings() |
| 96 |
} |
| 97 |
}, [subpage]) |
| 98 |
|
| 99 |
return ( |
| 100 |
<> |
| 101 |
<Toolbar /> |
| 102 |
<PageNotices /> |
| 103 |
<PageContent subpage={subpage} /> |
| 104 |
</> |
| 105 |
) |
| 106 |
} |
| 107 |
|