SILENT KILLERPanel

Current Path: > home > codekrsu > > shopceylon.store > wp-content > plugins > code-snippets > > js > > hooks


Operation   : Linux premium131.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
Software     : Apache
Server IP    : 162.0.232.56 | Your IP: 216.73.216.111
Domains      : 1034 Domain(s)
Permission   : [ 0755 ]

Files and Folders in: /home/codekrsu//shopceylon.store/wp-content/plugins/code-snippets//js//hooks

NameTypeSizeLast ModifiedActions
useAxios.ts File 1368 bytes November 27 2024 21:52:46.
useSnippetForm.tsx File 3408 bytes November 27 2024 21:52:46.
useSnippetSubmit.ts File 2696 bytes February 14 2025 13:16:14.
useSnippets.ts File 2640 bytes November 27 2024 21:52:46.

Reading File: /home/codekrsu//shopceylon.store/wp-content/plugins/code-snippets//js//hooks/useSnippetForm.tsx

import { isAxiosError } from 'axios'
import React, { createContext, useCallback, useContext, useMemo, useState } from 'react'
import { isLicensed } from '../utils/general'
import { isProSnippet } from '../utils/snippets'
import { useSnippetSubmit } from './useSnippetSubmit'
import type { Dispatch, PropsWithChildren, SetStateAction} from 'react'
import type { ScreenNotice } from '../types/ScreenNotice'
import type { Snippet } from '../types/Snippet'
import type { CodeEditorInstance } from '../types/WordPressCodeEditor'

export interface SnippetFormContext {
	snippet: Snippet
	setSnippet: Dispatch<SetStateAction<Snippet>>
	updateSnippet: Dispatch<SetStateAction<Snippet>>
	isReadOnly: boolean
	isWorking: boolean
	setIsWorking: Dispatch<SetStateAction<boolean>>
	currentNotice: ScreenNotice | undefined
	setCurrentNotice: Dispatch<SetStateAction<ScreenNotice | undefined>>
	codeEditorInstance: CodeEditorInstance | undefined
	setCodeEditorInstance: Dispatch<SetStateAction<CodeEditorInstance | undefined>>
	handleRequestError: (error: unknown, message?: string) => void
	submitSnippet: () => Promise<Snippet | undefined>
	submitAndActivateSnippet: () => Promise<Snippet | undefined>
	submitAndDeactivateSnippet: () => Promise<Snippet | undefined>
}

const SnippetFormContext = createContext<SnippetFormContext | undefined>(undefined)

export const useSnippetForm = () => {
	const value = useContext(SnippetFormContext)

	if (value === undefined) {
		throw Error('useSnippetForm can only be used within a SnippetForm context provider')
	}

	return value
}

export interface WithSnippetFormContextProps extends PropsWithChildren {
	initialSnippet: () => Snippet
}

export const WithSnippetFormContext: React.FC<WithSnippetFormContextProps> = ({ children, initialSnippet }) => {
	const [snippet, setSnippet] = useState<Snippet>(initialSnippet)
	const [isWorking, setIsWorking] = useState(false)
	const [currentNotice, setCurrentNotice] = useState<ScreenNotice>()
	const [codeEditorInstance, setCodeEditorInstance] = useState<CodeEditorInstance>()
	const submitSnippet = useSnippetSubmit(setSnippet, setIsWorking, setCurrentNotice)
	const isReadOnly = useMemo(() => !isLicensed() && isProSnippet(snippet.scope), [snippet.scope])

	const handleRequestError = useCallback((error: unknown, message?: string) => {
		console.error('Request failed', error)
		setIsWorking(false)
		setCurrentNotice(['error', [message, isAxiosError(error) ? error.message : ''].filter(Boolean).join(' ')])
	}, [setIsWorking, setCurrentNotice])

	const updateSnippet: Dispatch<SetStateAction<Snippet>> = useCallback((value: SetStateAction<Snippet>) => {
		setSnippet(previous => {
			const updated = 'object' === typeof value ? value : value(previous)
			codeEditorInstance?.codemirror.setValue(updated.code)
			window.tinymce?.activeEditor.setContent(updated.desc)
			return updated
		})
	}, [codeEditorInstance?.codemirror])

	const value: SnippetFormContext = {
		snippet,
		setSnippet,
		updateSnippet,
		isReadOnly,
		isWorking,
		setIsWorking,
		currentNotice,
		setCurrentNotice,
		codeEditorInstance,
		setCodeEditorInstance,
		handleRequestError,
		submitSnippet: () => submitSnippet(snippet),
		submitAndActivateSnippet: () => submitSnippet(snippet, true),
		submitAndDeactivateSnippet: () => submitSnippet(snippet, false)
	}

	return <SnippetFormContext.Provider value={value}>{children}</SnippetFormContext.Provider>
}

SILENT KILLER Tool