Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix requestId race condition and actually dispatch bool columnMeta #211

Merged
merged 3 commits into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/features/collections/CollectionDetail.tsx
Original file line number Diff line number Diff line change
@@ -1015,15 +1015,27 @@ const BooleanFilterControls = ({
setSelectValue(event.target.value as string);
};

// In order not to create a dependency loop when filter changes within the below effect,
// use a filter ref
const filterRef = useRef(filter);
filterRef.current = filter;

useEffect(() => {
let value;
if (selectValue === 'true') {
value = 1;
} else if (selectValue === 'false') {
value = 0;
}
dispatch(setFilter([collectionId, context, column, { ...filter, value }]));
}, [selectValue, collectionId, context, column, filter, dispatch]);
dispatch(
setFilter([
collectionId,
context,
column,
{ ...filterRef.current, value },
])
);
}, [selectValue, collectionId, context, column, dispatch]);
Comment on lines +1018 to +1038
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes infinite render loop

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh very nice thank you for catching that


return (
<FormControl fullWidth>
59 changes: 29 additions & 30 deletions src/features/collections/Filters.tsx
Original file line number Diff line number Diff line change
@@ -148,25 +148,22 @@ export const useContextFilterQueryManagement = (
<T extends CommonResult>(
result: T,
collectionId: string | undefined
): { filterData?: T['data']; context?: FilterContext } => {
): { filterData?: T['data'] } => {
if (result.isError) {
const err = parseError(result.error);
if (err.name !== 'AbortError')
toast('Filter Loading failed: ' + parseError(result.error).message);
}
const context = result.requestId
? requestContext.current[result.requestId]
: undefined;
if (!collectionId || !result?.data || !context || result.isError)
return {};
return { filterData: result.data, context };
Comment on lines -157 to -162
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed unnescary hacky context state (requestContext) that was causing issues, prefer just canceling the requests when the context changes (line 317)


if (!collectionId || !result?.data || result.isError) return {};
return { filterData: result.data };
},
[]
);

const handleTableFilters = useCallback(
<T extends typeof genomeResult | typeof sampleResult>(result: T) => {
const { filterData, context } = handleResult(result, collectionId);
const { filterData } = handleResult(result, collectionId);
if (!filterData || !collectionId || !context) return;
dispatch(clearFiltersAndColumnMeta([collectionId, context]));
filterData.columns.forEach((column) => {
@@ -215,35 +212,37 @@ export const useContextFilterQueryManagement = (
}
});
},
[collectionId, dispatch, handleResult]
[collectionId, dispatch, handleResult, context]
);

const handleHeatmapFilters = useCallback(
<T extends typeof biologResult | typeof microtraitResult>(result: T) => {
const { filterData, context } = handleResult(result, collectionId);
const { filterData } = handleResult(result, collectionId);
if (!filterData || !collectionId || !context) return;
dispatch(clearFiltersAndColumnMeta([collectionId, context]));
filterData.categories.forEach((category) => {
category.columns.forEach((column) => {
const current =
filtersRef.current && filtersRef.current[column.col_id];
if (column.type === 'bool') {
setColumnMeta([
collectionId,
context,
column.col_id,
{
type: 'bool',
key: column.col_id,
max_value: undefined,
min_value: undefined,
category: category.category,
description: column.description,
display_name: column.name,
filter_strategy: undefined,
enum_values: undefined,
},
]);
dispatch(
setColumnMeta([
collectionId,
context,
column.col_id,
{
type: 'bool',
key: column.col_id,
max_value: undefined,
min_value: undefined,
category: category.category,
description: column.description,
display_name: column.name,
filter_strategy: undefined,
enum_values: undefined,
},
])
);
Comment on lines +228 to +245
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filters were not showing up because I forgot the dispatch...

dispatch(
setFilter([
collectionId,
@@ -253,7 +252,9 @@ export const useContextFilterQueryManagement = (
type: 'bool',
value:
current?.type === column.type
? Boolean(current.value)
? typeof current.value !== 'undefined'
? Boolean(current.value)
: undefined
Comment on lines +255 to +257
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes accidentally setting all filters to false if they were undefined

: undefined,
},
])
@@ -291,7 +292,7 @@ export const useContextFilterQueryManagement = (
});
});
},
[collectionId, dispatch, handleResult]
[collectionId, dispatch, handleResult, context]
);

// When the context (or collection) changes, set the filter context and trigger appropriate query
@@ -311,8 +312,6 @@ export const useContextFilterQueryManagement = (
} else {
throw new Error(`No filter query matches filter context "${context}"`);
}
requestContext.current[request.requestId] = context;

return () => {
// Abort request if context changes while running (prevents race conditions)
if (request) {