| 1 |
import { __, _x } from '@wordpress/i18n' |
| 2 |
import React from 'react' |
| 3 |
import { useSnippetForm } from '../WithSnippetFormContext' |
| 4 |
import { createSnippetObject, isCondition } from '../../../../utils/snippets/snippets' |
| 5 |
import type { Snippet } from '../../../../types/Snippet' |
| 6 |
|
| 7 |
const OPTIONS = window.CODE_SNIPPETS_EDIT |
| 8 |
|
| 9 |
const getAddNewHeading = (snippet: Snippet): string => |
| 10 |
isCondition(snippet) |
| 11 |
? __('Add New Condition', 'code-snippets') |
| 12 |
: __('Create New Snippet', 'code-snippets') |
| 13 |
|
| 14 |
const getPageHeading = (snippet: Snippet): string => { |
| 15 |
if (isCondition(snippet)) { |
| 16 |
return snippet.locked |
| 17 |
? __('View Condition', 'code-snippets') |
| 18 |
: __('Edit Condition', 'code-snippets') |
| 19 |
} |
| 20 |
|
| 21 |
return snippet.locked |
| 22 |
? __('View Snippet', 'code-snippets') |
| 23 |
: __('Edit Snippet', 'code-snippets') |
| 24 |
} |
| 25 |
|
| 26 |
export const PageHeading: React.FC = () => { |
| 27 |
const { snippet, updateSnippet, setCurrentNotice } = useSnippetForm() |
| 28 |
|
| 29 |
return ( |
| 30 |
<> |
| 31 |
<h1> |
| 32 |
{snippet.id |
| 33 |
? <> |
| 34 |
{`${getPageHeading(snippet)} `} |
| 35 |
|
| 36 |
<a |
| 37 |
href={window.CODE_SNIPPETS?.urls.addNew} |
| 38 |
className="page-title-action" |
| 39 |
onClick={event => { |
| 40 |
event.preventDefault() |
| 41 |
updateSnippet(({ scope }) => createSnippetObject({ scope })) |
| 42 |
setCurrentNotice(undefined) |
| 43 |
|
| 44 |
window.document.title = window.document.title |
| 45 |
.replace(__('Edit Snippet', 'code-snippets'), getAddNewHeading(snippet)) |
| 46 |
.replace(__('Edit Condition', 'code-snippets'), getAddNewHeading(snippet)) |
| 47 |
|
| 48 |
window.history.pushState({}, '', window.CODE_SNIPPETS?.urls.addNew) |
| 49 |
}} |
| 50 |
> |
| 51 |
{_x('Add New', 'snippet', 'code-snippets')} |
| 52 |
</a> |
| 53 |
</> |
| 54 |
: getAddNewHeading(snippet)} |
| 55 |
|
| 56 |
{OPTIONS?.pageTitleActions && Object.entries(OPTIONS.pageTitleActions).map(([label, url]) => |
| 57 |
<> |
| 58 |
<a key={label} href={url} className="page-title-action">{label}</a>{' '} |
| 59 |
</> |
| 60 |
)} |
| 61 |
</h1> |
| 62 |
|
| 63 |
<hr className="wp-header-end" /> |
| 64 |
</> |
| 65 |
) |
| 66 |
} |
| 67 |
|