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