import type { SpectrumProcessingOperation } from '@zakodium/nmr-types'; import type { Spectrum } from '@zakodium/nmrium-core'; import { isSpectrum1D, isSpectrum2D, sliceData1D, sliceData2D, sliceSpectrum, } from '@zakodium/nmrium-core'; import { assertDefined, assertNotNullish, assertUnreachable, noop, } from '@zakodium/utils'; import type { Draft } from 'immer'; import { useMemo } from 'react'; import { useAccordionControls } from 'react-science/ui'; import { match } from 'ts-pattern'; import { useEventCallback } from 'usehooks-ts'; import { initializeContoursLevels } from '../../data/data2d/Spectrum2D/contours.ts'; import type { LiveEdit, State } from '../reducer/Reducer.ts'; import { setDomain, setMode } from '../reducer/actions/DomainActions.ts'; import { updateView } from '../reducer/actions/FiltersActions.ts'; import { restoreLastZoomDomain } from '../reducer/helper/ZoomHistoryManager.ts'; import { getActiveSpectrum } from '../reducer/helper/getActiveSpectrum.ts'; import { useChartData } from './ChartContext.tsx'; import { useCore } from './CoreContext.tsx'; import { useDispatch } from './DispatchContext.tsx'; export type ProcessingsMutations = ReturnType< typeof useProcessingsMutationsAPI >; interface PrepareLiveChangeOptions { operationUid: string; shouldProcessAll: boolean; shouldUpdateView: boolean; liveOperation?: SpectrumProcessingOperation; preProcessOnly?: boolean; enforceSpectrum?: Spectrum; } export function useProcessingsMutationsAPI() { const core = useCore(); const state = useChartData(); const dispatch = useDispatch(); const { open: openPanel } = useAccordionControls(); function setIsLoading(isLoading: boolean) { dispatch({ type: 'SET_LOADING_FLAG', payload: { isLoading }, }); } /** * Return sliced spectrum allowing free mutations */ function getSpectrum() { const activeSpectrum = getActiveSpectrum(state); if (!activeSpectrum) return {}; const id = activeSpectrum.id; const indexSpectrum = state.data.findIndex((s) => s.id === id); if (indexSpectrum === -1) return {}; const spectrum = sliceSpectrum(state.data[indexSpectrum]); return { spectrum, indexSpectrum }; } /** * Return sliced spectrum live processed allowing free mutations */ function getSpectrumLiveProcessed() { const spectrum = state.spectrumLiveProcessed; if (!spectrum) return; return sliceSpectrum(spectrum); } async function submit( spectrum: Spectrum, index: number, onProduce: (draft: Draft, processedSpectrum: Spectrum) => void, ) { setIsLoading(true); const processedSpectrum = await core .processSpectrum(spectrum) .finally(() => setIsLoading(false)); dispatch({ type: 'SET_SPECTRUM', payload: { index, spectrum: processedSpectrum, onProduce: (draft) => onProduce(draft, processedSpectrum), }, }); return spectrum; } function resetSelectedTool() { dispatch({ type: 'SELECT_PROCESSING_OPERATOR', payload: { operatorUI: undefined }, }); } // --- API --- // const resetLiveChange = useEventCallback(function resetLiveChange( shouldUpdateView: boolean, ) { dispatch({ type: 'SET_SPECTRUM_LIVE_PROCESSED', payload: { spectrumLiveProcessed: undefined, shouldUpdateView, }, }); }); const apply = useEventCallback(async function apply( operation: SpectrumProcessingOperation, indexOperation: number, ) { const { spectrum, indexSpectrum } = getSpectrum(); if ( !spectrum?.processings || indexOperation > spectrum.processings.length ) { return; } spectrum.processings[indexOperation] = operation; resetSelectedTool(); await submit(spectrum, indexSpectrum, (draft) => { const { domainUpdateRules = { updateXDomain: false, updateYDomain: false, }, } = core.getOperator(operation.operatorId) ?? {}; updateView(draft, domainUpdateRules); }); }); const reorder = useEventCallback(async function reorder( sourceIndex: number, targetIndex: number, ) { const { spectrum, indexSpectrum } = getSpectrum(); if (!spectrum?.processings) return; [spectrum.processings[sourceIndex], spectrum.processings[targetIndex]] = [ spectrum.processings[targetIndex], spectrum.processings[sourceIndex], ]; await submit(spectrum, indexSpectrum, noop); }); const remove = useEventCallback(async function remove(uid: string) { const { spectrum, indexSpectrum } = getSpectrum(); if (!spectrum?.processings) return; spectrum.processings = spectrum.processings.filter((p) => p.uid !== uid); resetSelectedTool(); await submit(spectrum, indexSpectrum, (draft) => { draft.toolOptions.data.activeFilterID = null; setDomain(draft); setMode(draft); }); }); const removeAll = useEventCallback(async function removeAll() { const { spectrum, indexSpectrum } = getSpectrum(); if (!spectrum?.processings || spectrum.processings.length === 0) return; spectrum.processings = []; resetSelectedTool(); await submit(spectrum, indexSpectrum, (draft) => { setDomain(draft); setMode(draft); }); }); const switchEnabled = useEventCallback(async function switchEnabled( uid: string, ) { const { spectrum, indexSpectrum } = getSpectrum(); if (!spectrum?.processings) return; const operationIndex = spectrum.processings.findIndex((p) => p.uid === uid); if (operationIndex === -1) return; const operation = spectrum.processings[operationIndex]; operation.enabled = !operation.enabled; resetSelectedTool(); await submit(spectrum, indexSpectrum, (draft, processedSpectrum) => { if (isSpectrum2D(processedSpectrum) && processedSpectrum.info.isFt) { draft.view.spectraContourLevels[processedSpectrum.id] = initializeContoursLevels(processedSpectrum); } setDomain(draft); setMode(draft); restoreLastZoomDomain(draft); }); }); // Related to live update async function prepareLiveChange(options: PrepareLiveChangeOptions) { const { operationUid: uid, shouldProcessAll, shouldUpdateView, liveOperation, preProcessOnly = false, enforceSpectrum, } = options; const { spectrum } = enforceSpectrum ? { spectrum: sliceSpectrum(enforceSpectrum) } : getSpectrum(); if (!spectrum?.processings) return; const preProcessings: typeof spectrum.processings = []; const processings: typeof spectrum.processings = []; let didFindUid = false; for (const operation of spectrum.processings) { if (uid === operation.uid) { didFindUid = true; processings.push(structuredClone(liveOperation ?? operation)); continue; } if (didFindUid) processings.push(structuredClone(operation)); else preProcessings.push(structuredClone(operation)); } // apply preProcessings spectrum.processings = preProcessings; const preProcessedSpectrum = await core.processSpectrum(spectrum); // prepare processings for live-update preProcessedSpectrum.processings = processings; preProcessedSpectrum.originalInfo = structuredClone( preProcessedSpectrum.info, ); preProcessedSpectrum.originalProcessingInfo = structuredClone( preProcessedSpectrum.processingInfo, ); if (isSpectrum1D(preProcessedSpectrum)) { preProcessedSpectrum.originalData = sliceData1D( preProcessedSpectrum.data, ); } else if (isSpectrum2D(preProcessedSpectrum)) { preProcessedSpectrum.originalData = sliceData2D( preProcessedSpectrum.data, ); } else { assertUnreachable(preProcessedSpectrum); } // stop here preProcessOnly if (preProcessOnly) { dispatch({ type: 'SET_SPECTRUM_LIVE_PROCESSED', payload: { spectrumLiveProcessed: preProcessedSpectrum, shouldUpdateView, }, }); return; } // apply the rest of processings const savedProcessings = structuredClone(preProcessedSpectrum.processings); if (!shouldProcessAll) { preProcessedSpectrum.processings.splice(1); } const processedSpectrum = await core.processSpectrum(preProcessedSpectrum); assertDefined(processedSpectrum.processings); const processedOperation = processedSpectrum.processings[0]; processedSpectrum.processings = savedProcessings; spectrum.processings[0] = processedOperation; dispatch({ type: 'SET_SPECTRUM_LIVE_PROCESSED', payload: { spectrumLiveProcessed: processedSpectrum, shouldUpdateView, }, }); } const applyLiveChange = useEventCallback(async function applyLiveChange( operation: SpectrumProcessingOperation, shouldProcessAll: boolean, ) { const spectrum = getSpectrumLiveProcessed(); if (!spectrum?.processings) return; const indexOperation = spectrum.processings.findIndex( (p) => p.uid === operation.uid, ); if (indexOperation === -1) return; const savedProcessings = structuredClone(spectrum.processings); spectrum.processings[indexOperation] = structuredClone(operation); if (!shouldProcessAll) { spectrum.processings.splice(indexOperation + 1); } const processedSpectrum = await core.processSpectrum(spectrum); assertDefined(processedSpectrum.processings); const processedOperation = processedSpectrum.processings[indexOperation]; processedSpectrum.processings = savedProcessings; spectrum.processings[indexOperation] = processedOperation; dispatch({ type: 'SET_SPECTRUM_LIVE_PROCESSED', payload: { spectrumLiveProcessed: processedSpectrum, shouldUpdateView: false, }, }); }); const handleLiveChange = useEventCallback(async function handleLiveChange( liveEdit: LiveEdit, operationUid: string, ) { const ui = core.slotOperator(operationUid); const previousLiveEdit = state.processingOperators.liveEdit ?? { checked: ui?.isLiveEditable ?? false, shouldProcessNext: ui?.defaultShouldProcessAll ?? liveEdit.shouldProcessNext, }; const shouldUpdateView = previousLiveEdit.shouldProcessNext !== liveEdit.shouldProcessNext; const liveOperation = state.processingOperators.liveOperation; await match(liveEdit) // spectrum fully processed with live operation .with({ checked: true, shouldProcessNext: true }, async () => { await prepareLiveChange({ operationUid, shouldProcessAll: true, shouldUpdateView, liveOperation, }); }) // spectrum fully processed with original operation // ie: no `state.spectrumLiveProcessed` .with({ checked: false, shouldProcessNext: true }, async () => { resetLiveChange(true); }) // spectrum processed until the operation (stop before) then apply the live operation .with({ checked: true, shouldProcessNext: false }, async () => { await prepareLiveChange({ operationUid, shouldProcessAll: false, shouldUpdateView, liveOperation, }); }) // spectrum processed until the operation (stop before) .with({ checked: false, shouldProcessNext: false }, async () => { await prepareLiveChange({ operationUid, shouldProcessAll: false, shouldUpdateView, preProcessOnly: true, }); }) .exhaustive(); }); const triggerOperation = useEventCallback(async function triggerOperation( operation: SpectrumProcessingOperation, ) { const { spectrum, indexSpectrum } = getSpectrum(); if (!spectrum) return; spectrum.processings ??= []; let operationIndex = spectrum.processings.findIndex( (processing) => processing.uid === operation.uid || processing.operatorId === operation.operatorId, ); if (operationIndex === -1) { spectrum.processings.push(operation); operationIndex = spectrum.processings.length - 1; } operation = spectrum.processings[operationIndex]; const operatorUI = core.slotOperator(operation.operatorId); const operator = core.getOperator(operation.operatorId); assertNotNullish(operatorUI); assertNotNullish(operator); openPanel('processingsPanel'); resetLiveChange(false); const processedSpectrum = await submit(spectrum, indexSpectrum, (draft) => updateView(draft, operator.domainUpdateRules), ); dispatch({ type: 'SELECT_PROCESSING_OPERATOR', payload: { operatorUI }, }); if (operatorUI.isEditable) { dispatch({ type: 'SET_LIVE_OPERATION', payload: { liveOperation: operation }, }); } if (operatorUI.isLiveEditable) { await prepareLiveChange({ operationUid: operation.uid, liveOperation: operation, shouldProcessAll: operatorUI.defaultShouldProcessAll ?? false, shouldUpdateView: true, enforceSpectrum: processedSpectrum, }); } }); return useMemo( () => ({ apply, reorder, remove, removeAll, switchEnabled, resetLiveChange, applyLiveChange, handleLiveChange, triggerOperation, }), [ apply, applyLiveChange, handleLiveChange, remove, removeAll, reorder, resetLiveChange, switchEnabled, triggerOperation, ], ); }