| 1 |
import { isAxiosError } from 'axios' |
| 2 |
import React, { useCallback, useMemo, useState } from 'react' |
| 3 |
import { createContextHook } from '../utils/hooks' |
| 4 |
import { isLicensed } from '../utils/screen' |
| 5 |
import { isProSnippet } from '../utils/snippets/snippets' |
| 6 |
import type { Dispatch, PropsWithChildren, SetStateAction } from 'react' |
| 7 |
import type { ScreenNotice } from '../types/ScreenNotice' |
| 8 |
import type { Snippet } from '../types/Snippet' |
| 9 |
import type { CodeEditorInstance } from '../types/WordPressCodeEditor' |
| 10 |
|
| 11 |
export interface SnippetFormContext { |
| 12 |
snippet: Snippet |
| 13 |
isWorking: boolean |
| 14 |
isReadOnly: boolean |
| 15 |
setSnippet: Dispatch<SetStateAction<Snippet>> |
| 16 |
updateSnippet: Dispatch<SetStateAction<Snippet>> |
| 17 |
setIsWorking: Dispatch<SetStateAction<boolean>> |
| 18 |
currentNotice: ScreenNotice | undefined |
| 19 |
setCurrentNotice: Dispatch<SetStateAction<ScreenNotice | undefined>> |
| 20 |
codeEditorInstance: CodeEditorInstance | undefined |
| 21 |
handleRequestError: (error: unknown, message?: string) => void |
| 22 |
setCodeEditorInstance: Dispatch<SetStateAction<CodeEditorInstance | undefined>> |
| 23 |
} |
| 24 |
|
| 25 |
export const [SnippetFormContext, useSnippetForm] = createContextHook<SnippetFormContext>('SnippetForm') |
| 26 |
|
| 27 |
export interface WithSnippetFormContextProps extends PropsWithChildren { |
| 28 |
initialSnippet: () => Snippet |
| 29 |
} |
| 30 |
|
| 31 |
export const WithSnippetFormContext: React.FC<WithSnippetFormContextProps> = ({ children, initialSnippet }) => { |
| 32 |
const [snippet, setSnippet] = useState<Snippet>(initialSnippet) |
| 33 |
const [isWorking, setIsWorking] = useState(false) |
| 34 |
const [currentNotice, setCurrentNotice] = useState<ScreenNotice>() |
| 35 |
const [codeEditorInstance, setCodeEditorInstance] = useState<CodeEditorInstance>() |
| 36 |
|
| 37 |
const isReadOnly = useMemo(() => !isLicensed() && isProSnippet({ scope: snippet.scope }), [snippet.scope]) |
| 38 |
|
| 39 |
const handleRequestError = useCallback((error: unknown, message?: string) => { |
| 40 |
console.error('Request failed', error) |
| 41 |
setIsWorking(false) |
| 42 |
setCurrentNotice(['error', [message, isAxiosError(error) ? error.message : ''].filter(Boolean).join(' ')]) |
| 43 |
}, [setIsWorking, setCurrentNotice]) |
| 44 |
|
| 45 |
const updateSnippet: Dispatch<SetStateAction<Snippet>> = useCallback((value: SetStateAction<Snippet>) => { |
| 46 |
setSnippet(previous => { |
| 47 |
const updated = 'object' === typeof value ? value : value(previous) |
| 48 |
codeEditorInstance?.codemirror.setValue(updated.code) |
| 49 |
window.tinymce?.activeEditor.setContent(updated.desc) |
| 50 |
return updated |
| 51 |
}) |
| 52 |
}, [codeEditorInstance?.codemirror]) |
| 53 |
|
| 54 |
const value: SnippetFormContext = { |
| 55 |
snippet, |
| 56 |
isWorking, |
| 57 |
isReadOnly, |
| 58 |
setSnippet, |
| 59 |
setIsWorking, |
| 60 |
updateSnippet, |
| 61 |
currentNotice, |
| 62 |
setCurrentNotice, |
| 63 |
codeEditorInstance, |
| 64 |
handleRequestError, |
| 65 |
setCodeEditorInstance |
| 66 |
} |
| 67 |
|
| 68 |
return <SnippetFormContext.Provider value={value}>{children}</SnippetFormContext.Provider> |
| 69 |
} |
| 70 |
|