Skip to content

Commit

Permalink
fix: apply apodization on the fly when opening its options panel
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Jul 9, 2024
1 parent 1b8ebdc commit 506bc4b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/component/header/ApodizationOptionsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Filter,
ApodizationOptions as BaseApodizationOptions,
} from 'nmr-processing';
import { useRef, memo } from 'react';
import { useRef, memo, useCallback, useEffect } from 'react';
import { useForm } from 'react-hook-form';
import * as Yup from 'yup';

Expand Down Expand Up @@ -43,19 +43,28 @@ function ApodizationOptionsInnerPanel(
const dispatch = useDispatch();
const previousPreviewRef = useRef<boolean>(initialValues.livePreview);

const onChange = useCallback(
(values) => {
const { livePreview, ...options } = values;

if (livePreview || previousPreviewRef.current !== livePreview) {
dispatch({
type: 'CALCULATE_APODIZATION_FILTER',
payload: { livePreview, options },
});
}
},
[dispatch],
);

function handleApplyFilter(
values,
triggerSource: 'apply' | 'onChange' = 'apply',
) {
const { livePreview, ...options } = values;
switch (triggerSource) {
case 'onChange': {
if (livePreview || previousPreviewRef.current !== livePreview) {
dispatch({
type: 'CALCULATE_APODIZATION_FILTER',
payload: { livePreview, options },
});
}
onChange(values);
break;
}

Expand Down Expand Up @@ -102,6 +111,10 @@ function ApodizationOptionsInnerPanel(
const { onChange: onLivePreviewChange, ...otherLivePreviewOptions } =
register('livePreview');

useEffect(() => {
void handleSubmit((values) => onChange(values))();
}, [handleSubmit, onChange]);

return (
<HeaderContainer>
<Label title="Line broadening:" shortTitle="LB:" style={headerLabelStyle}>
Expand Down

0 comments on commit 506bc4b

Please sign in to comment.