-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pre-select epigenomes if none are selected and metadata api payload c…
…ontains default selection instructions
- Loading branch information
Showing
3 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/content/app/regulatory-activity-viewer/hooks/usePreselectedEpigenomes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* See the NOTICE file distributed with this work for additional information | ||
* regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { useEffect } from 'react'; | ||
|
||
import { useAppDispatch, useAppSelector } from 'src/store'; | ||
|
||
import { getEpigenomeSelectionCriteria } from 'src/content/app/regulatory-activity-viewer/state/epigenome-selection/epigenomeSelectionSelectors'; | ||
import { getActiveGenomeId } from 'src/content/app/regulatory-activity-viewer/state/general/generalSelectors'; | ||
|
||
import { useEpigenomeMetadataDimensionsQuery } from 'src/content/app/regulatory-activity-viewer/state/api/activityViewerApiSlice'; | ||
import { addSelectionCriterion } from 'src/content/app/regulatory-activity-viewer/state/epigenome-selection/epigenomeSelectionSlice'; | ||
|
||
import type { EpigenomeMetadataDimensions } from 'src/content/app/regulatory-activity-viewer/types/epigenomeMetadataDimensions'; | ||
|
||
/** | ||
* Upon the first load of epigenome metadata dimensions, | ||
* check if user has selected any dimensions, | ||
* and if not, then pre-select epigenomes using the default dimensions (if available). | ||
* | ||
* See the CommonMetadataDimensionFields type for epigenome metadata dimensions. | ||
* | ||
* NOTE: this hook should be only run once, at the top level of RegulatoryActivityViewer | ||
*/ | ||
const usePreselectedEpigenomes = () => { | ||
const activeGenomeId = useAppSelector(getActiveGenomeId) ?? ''; | ||
const { currentData: epigenomeMetadataDimensionsResponse } = | ||
useEpigenomeMetadataDimensionsQuery(); | ||
const epigenomeSelectionCriteria = useAppSelector((state) => | ||
getEpigenomeSelectionCriteria(state, activeGenomeId) | ||
); | ||
const dispatch = useAppDispatch(); | ||
|
||
useEffect(() => { | ||
if (!activeGenomeId || !epigenomeMetadataDimensionsResponse) { | ||
// can't do anything until epigenomeMetadataDimensionsResponse is available | ||
return; | ||
} | ||
if (Object.keys(epigenomeSelectionCriteria).length) { | ||
// some epigenomes have already been selected; bail out | ||
return; | ||
} | ||
|
||
const initialEpigenomeFilters = getPreselectedDimensions( | ||
epigenomeMetadataDimensionsResponse.dimensions | ||
); | ||
|
||
if (initialEpigenomeFilters.length) { | ||
for (const { dimensionName, value } of initialEpigenomeFilters) { | ||
dispatch( | ||
addSelectionCriterion({ | ||
genomeId: activeGenomeId, | ||
dimensionName, | ||
value | ||
}) | ||
); | ||
} | ||
} | ||
}, [ | ||
epigenomeMetadataDimensionsResponse, | ||
epigenomeSelectionCriteria, | ||
activeGenomeId | ||
]); | ||
}; | ||
|
||
const getPreselectedDimensions = (dimensions: EpigenomeMetadataDimensions) => { | ||
const result: { dimensionName: string; value: string }[] = []; | ||
|
||
for (const [dimensionName, dimensionData] of Object.entries(dimensions)) { | ||
if (dimensionData.default_values.length) { | ||
for (const value of dimensionData.default_values) { | ||
result.push({ dimensionName, value }); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
export default usePreselectedEpigenomes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters