Skip to content

Commit

Permalink
fix: update domain
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Dec 4, 2024
1 parent e6bd8bf commit 29b3a18
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
18 changes: 16 additions & 2 deletions src/component/context/FilterSyncOptionsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,30 @@ export function useFilterSyncOptions<T>(): FilterSyncOptionsState<T> {
return context;
}

export function useSyncedFilterOptions(onWatch: (options: any) => void) {
export function useSyncedFilterOptions(
onWatch: (options: any) => void,
onReset?: () => void,
) {
const { sharedFilterOptions, updateFilterOptions } = useFilterSyncOptions();
const isSyncOptionsDirty = useRef(true);

const watchRef = useRef(onWatch);
const resetRef = useRef(onReset);

// Update the ref when onWatch changes
useEffect(() => {
watchRef.current = onWatch;
}, [onWatch]);
// Update the ref when onWatch changes
useEffect(() => {
resetRef.current = onReset;
}, [onReset]);

useEffect(() => {
if (sharedFilterOptions === null) {
resetRef.current?.();
}

if (sharedFilterOptions && isSyncOptionsDirty.current) {
watchRef.current(sharedFilterOptions);
} else {
Expand All @@ -52,6 +64,7 @@ export function useSyncedFilterOptions(onWatch: (options: any) => void) {

const clearSyncFilterOptions = useCallback(() => {
isSyncOptionsDirty.current = true;
resetRef.current?.();
updateFilterOptions(null);
}, [updateFilterOptions]);

Expand All @@ -78,7 +91,8 @@ export function FilterSyncOptionsProvider({
const state = useMemo(() => {
return {
sharedFilterOptions,
updateFilterOptions: (options) => updateFilterOptions({ ...options }),
updateFilterOptions: (options) =>
updateFilterOptions(structuredClone(options)),
};
}, [sharedFilterOptions]);

Expand Down
15 changes: 12 additions & 3 deletions src/component/panels/filtersPanel/Filters/FiltersSectionsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getFilterLabel } from '../../../../data/getFilterLabel.js';
import type { FilterEntry as BaseFilterEntry } from '../../../../data/types/common/FilterEntry.js';
import { useChartData } from '../../../context/ChartContext.js';
import { useDispatch } from '../../../context/DispatchContext.js';
import { useFilterSyncOptions } from '../../../context/FilterSyncOptionsContext.js';
import { useToaster } from '../../../context/ToasterContext.js';
import type { AlertButton } from '../../../elements/Alert.js';
import { useAlert } from '../../../elements/Alert.js';
Expand Down Expand Up @@ -170,21 +171,29 @@ function FiltersInner(props: FiltersInnerProps) {
toolOptions: { selectedTool },
} = useChartData();
const [newFilter, setNewFilter] = useState<FilterEntry | null>();
const { updateFilterOptions } = useFilterSyncOptions();

const [selectedSection, openSection] = useState('');
const [selectedSection, openSection] = useState<string | null>();
const dispatch = useDispatch();
const toaster = useToaster();
const selectedFilterIndex = useRef<number>();

function toggleSection(sectionKey) {
openSection(selectedSection === sectionKey ? null : sectionKey);
openSection(selectedSection === sectionKey ? '' : sectionKey);
}

function filterSnapShotHandler(filter, index) {
selectedFilterIndex.current =
selectedFilterIndex.current && index === selectedFilterIndex.current
? null
: index;

if (activeFilterID) {
//Clear the filter sync object
updateFilterOptions(null);
//Close the opened section
openSection(null);
}
const hideLoading = toaster.showLoading({
message: 'Filter snapshot process in progress',
});
Expand Down Expand Up @@ -248,7 +257,7 @@ function FiltersInner(props: FiltersInnerProps) {
}

function handleClose() {
openSection('');
openSection(null);
}

return (
Expand Down
11 changes: 6 additions & 5 deletions src/component/reducer/actions/FiltersActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,26 +398,27 @@ function rollbackSpectrumByFilter(

// re-implement all filters and rest all view property that related to filters
if (reset) {
draft.toolOptions.data.activeFilterID = null;
draft.tempData = null;

if (filterIndex !== -1 && datum.filters.length > 0) {
updateDomainOptions = getFilterDomain(datum, {
startIndex: filterIndex,
lastIndex: datum.filters.length - 1,
});
} else {
} else if (draft.toolOptions.data.activeFilterID) {
updateDomainOptions = { updateXDomain: true, updateYDomain: true };
} else {
updateDomainOptions = { updateXDomain: false, updateYDomain: false };
}

draft.toolOptions.data.activeFilterID = null;
draft.tempData = null;

const {
toolOptions: { data },
} = getInitialState();
draft.toolOptions.data = data;
currentIsFid = isFid1DSpectrum(datum) || isFid2DSpectrum(datum);
}
}

setDomain(draft, updateDomainOptions);
if (previousIsFid !== currentIsFid) {
setMode(draft);
Expand Down

0 comments on commit 29b3a18

Please sign in to comment.