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/useSnippetSubmit.ts

import { __ } from '@wordpress/i18n'
import { addQueryArgs } from '@wordpress/url'
import { isAxiosError } from 'axios'
import { useCallback } from 'react'
import { useSnippetsAPI } from './useSnippets'
import type { Dispatch, SetStateAction } from 'react'
import type { ScreenNotice } from '../types/ScreenNotice'
import type { Snippet } from '../types/Snippet'

const getSuccessNotice = (request: Snippet, response: Snippet, active: boolean | undefined) => {
	if (active === undefined) {
		return 0 === request.id
			? __('Snippet created.', 'code-snippets')
			: __('Snippet updated.', 'code-snippets')
	}

	if (0 === request.id && active) {
		return __('Snippet created and activated.', 'code-snippets')
	}

	if (active) {
		return 'single-use' === response.scope
			? __('Snippet updated and executed.', 'code-snippets')
			: __('Snippet updated and activated.', 'code-snippets')
	} else {
		return __('Snippet updated and deactivated')
	}
}

export const useSnippetSubmit = (
	setSnippet: Dispatch<SetStateAction<Snippet>>,
	setIsWorking: Dispatch<SetStateAction<boolean>>,
	setCurrentNotice: Dispatch<SetStateAction<ScreenNotice | undefined>>
): (snippet: Snippet, active?: boolean) => Promise<Snippet | undefined> => {
	const api = useSnippetsAPI()

	return useCallback(async (snippet: Snippet, active?: boolean) => {
		setIsWorking(true)
		setCurrentNotice(undefined)

		const result = await (async (): Promise<Snippet | string | undefined> => {
			try {
				const requestData: Snippet = { ...snippet, active: active ?? snippet.active }
				const { data } = await (0 === snippet.id ? api.create(requestData) : api.update(requestData))
				setIsWorking(false)
				return data.id ? data : undefined
			} catch (error) {
				setIsWorking(false)
				return isAxiosError(error) ? error.message : undefined
			}
		})()

		if (undefined === result || 'string' === typeof result) {
			const message = [
				snippet.id
					? __('Could not create snippet.', 'code-snippets')
					: __('Could not update snippet.', 'code-snippets'),
				result ?? __('The server did not send a valid response.', 'code-snippets')
			]

			setCurrentNotice(['error', message.filter(Boolean).join(' ')])
			return undefined
		} else {
			setSnippet({ ...result })
			setCurrentNotice(['updated', getSuccessNotice(snippet, result, active)])

			if (snippet.id && result.id) {
				window.document.title = window.document.title.replace(
					__('Add New Snippet', 'code-snippets'),
					__('Edit Snippet', 'code-snippets')
				)

				window.history.replaceState({}, '', addQueryArgs(window.CODE_SNIPPETS?.urls.edit, { id: result.id }))
			}

			return result
		}
	}, [api, setCurrentNotice, setIsWorking, setSnippet])
}

SILENT KILLER Tool