import { Classes, FormGroup, MenuItem, NumericInput } from '@blueprintjs/core'; import type { ItemRenderer } from '@blueprintjs/select'; import { Select } from '@blueprintjs/select'; import styled from '@emotion/styled'; import { revalidateLogic } from '@tanstack/react-form'; import { AppForm, Button, TooltipHelpContent, coerceNumberInput, useForm, } from 'react-science/ui'; import { z } from 'zod/v4'; import { useDispatch } from '../context/DispatchContext.js'; import { useToaster } from '../context/ToasterContext.js'; import { useActiveNucleusTab } from '../hooks/useActiveNucleusTab.ts'; import { useActiveSpectra } from '../hooks/useActiveSpectra.ts'; import { MIN_AREA_POINTS, useCheckPointsNumberInSelectedSpectra, } from '../hooks/useCheckPointsNumberInWindowArea.js'; import { usePanelPreferences } from '../hooks/usePanelPreferences.ts'; type Direction = 'positive' | 'negative' | 'both'; interface Item { label: string; value: Direction; } const LookFor: Item[] = [ { label: 'Positive', value: 'positive', }, { label: 'Negative', value: 'negative', }, { label: 'Both', value: 'both', }, ]; const validationSchema = z.object({ maxNumberOfPeaks: coerceNumberInput(z.number().min(1)), minMaxRatio: coerceNumberInput(z.number().min(0)), noiseFactor: coerceNumberInput(z.number().min(0)), direction: z.enum(['both', 'negative', 'positive']), }); type AutoPeakPickingOptionsInput = z.input; type AutoPeakPickingOptions = z.output; const defaultValues: AutoPeakPickingOptionsInput = { maxNumberOfPeaks: '500', minMaxRatio: '0.05', noiseFactor: '3', direction: 'both', }; const AppFormContainer = styled(AppForm)` display: flex; flex-direction: row; gap: 10px; align-items: center; .${Classes.FORM_GROUP} { margin-bottom: 0; } `; const renderDirection: ItemRenderer = ( direction, { handleClick, handleFocus, modifiers }, ) => { if (!modifiers.matchesPredicate) { return null; } return ( ); }; export function AutoPeakPickingOptionPanel() { const dispatch = useDispatch(); const hasEnoughPoints = useCheckPointsNumberInSelectedSpectra(); const toaster = useToaster(); const nucleus = useActiveNucleusTab(); const activeSpectra = useActiveSpectra(); const { defaultPeakShape } = usePanelPreferences('peaks', nucleus); const isSpectraSelected = activeSpectra && activeSpectra.length > 0; const form = useForm({ defaultValues, validationLogic: revalidateLogic({ modeAfterSubmission: 'change' }), onSubmit: ({ value }) => { const parsedValues = validationSchema.parse(value); handlePeakPicking(parsedValues); }, validators: { onDynamic: validationSchema, }, }); function handlePeakPicking(values: AutoPeakPickingOptions) { if (hasEnoughPoints) { dispatch({ type: 'AUTO_PEAK_PICKING', payload: { options: values, defaultPeakShape, }, }); } else { toaster.show({ message: `Auto peak picking only available for area more than ${MIN_AREA_POINTS} points`, intent: 'danger', }); } } return ( {(field) => { const activeItem = LookFor.find( (item) => item.value === field.state.value, ); return ( filterable={false} items={LookFor} itemRenderer={renderDirection} activeItem={activeItem} onItemSelect={(element) => { field.handleChange(element.value); }} >