| 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 |
|