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

CDE-74 fix: Use useMemo for additionalDatasetMappings calculation #45

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 11 additions & 8 deletions lib/components/steps/mapping/MappingStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ interface CustomTabPanelProps extends BoxProps {
}


const CustomTabPanel: React.FC<CustomTabPanelProps> = ({ children, value, index, ...other }) => {
const CustomTabPanel: React.FC<CustomTabPanelProps> = ({children, value, index, ...other}) => {
return (
<Box
height={1}
Expand Down Expand Up @@ -93,7 +93,7 @@ function MappingStep() {
setTabIndex((prevValue) => (prevValue + 1) % tabsArr.length);
};

const renderTabComponent = () => {
const renderTabComponent = React.useMemo(() => {
switch (tabIndex) {
case TabsEnum.Collection:
return <CollectionsTab
Expand All @@ -104,11 +104,12 @@ function MappingStep() {
case TabsEnum.Suggestions:
return <SuggestionsStep changeToNextTab={changeToNextTab}/>;
case TabsEnum.Mapping:
return <MappingTab defaultCollection={defaultCollection} numberOfUnmappedRows={getUnmappedRowsCount()}/>;
return <MappingTab defaultCollection={defaultCollection}
numberOfUnmappedRows={getUnmappedRowsCount()}/>;
default:
return <div>Unknown step</div>;
}
};
}, [defaultCollection, getUnmappedRowsCount, tabIndex]);

return (
<Fragment>
Expand Down Expand Up @@ -139,14 +140,16 @@ function MappingStep() {
</>
}
>
<Tab disableRipple label={`${index + 1}. ${tab.label}`} {...a11yProps(index)} className={`${tab?.className}`} />
<Tab disableRipple label={`${index + 1}. ${tab.label}`} {...a11yProps(index)}
className={`${tab?.className}`}/>
</Tooltip>
))}
</Tabs>

<Box display='flex' gap='0.625rem' alignItems='center'>
{tabIndex === TabsEnum.Suggestions ? (
<Button variant='text' onClick={() => setTabIndex(TabsEnum.Mapping)} className='suggestions__cancel-btn'>
<Button variant='text' onClick={() => setTabIndex(TabsEnum.Mapping)}
className='suggestions__cancel-btn'>
Continue without suggestions
</Button>) : tabIndex === TabsEnum.Mapping && (<>
<Typography className='mapping-header__indicator' sx={{
Expand All @@ -158,7 +161,7 @@ function MappingStep() {
{getUnmappedRowsCount()}/{getTotalRowsCount()} column headers still unmapped
</Typography>

<Divider sx={{ height: '1.875rem', background: gray100, width: '0.0625rem' }} />
<Divider sx={{height: '1.875rem', background: gray100, width: '0.0625rem'}}/>

<Button variant='contained' onClick={onClose}>
Save mapping
Expand All @@ -170,7 +173,7 @@ function MappingStep() {
key={index}
value={tabIndex}
index={index}>
{renderTabComponent()}
{renderTabComponent}
</CustomTabPanel>
))}
</Fragment>
Expand Down
2 changes: 1 addition & 1 deletion lib/components/steps/suggestions/SuggestionsStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function SuggestionsStep({changeToNextTab}: SuggestionsStepProps) {
const [stepIndex, setStepIndex] = useState<number>(0);

const [selectedSuggestion, setSelectedSuggestion] = useState<string[] | null>(null);
const [suggestionsToProcess, setSuggestionsToProcess] = useState<string[]>(getColumnsWithSuggestions());
const [suggestionsToProcess, setSuggestionsToProcess] = useState<string[]>([]);

const handleNext = useCallback(() => {
setCurrentKeyIndex((prevIndex) => (prevIndex + 1) % suggestionsToProcess.length);
Expand Down
30 changes: 16 additions & 14 deletions lib/contexts/data/DataContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,23 @@ export const DataContextProvider = ({
const [datasetMapping, setDatasetMapping] = useState<DatasetMapping>(initialDatasetMapping);
const [datasetMappingHeader, setDatasetMappingHeader] = useState<string[]>(initialDatasetMappingHeader);

const additionalDatasetMappings: DatasetMapping[] = rawAdditionalDatasetMappings.map((additionalMapping, index) => {
try {
validateDatasetMapping(additionalMapping, headerIndexes.variableName);
} catch (error) {
if (error instanceof Error) {
console.warn(`Skipping invalid additionalDatasetMapping at index ${index}: ${error.message}`);
} else {
console.warn(`Skipping invalid additionalDatasetMapping at index ${index}: Unknown error`);
const additionalDatasetMappings: DatasetMapping[] = useMemo(() => {
return rawAdditionalDatasetMappings.map((additionalMapping, index) => {
try {
validateDatasetMapping(additionalMapping, headerIndexes.variableName);
} catch (error) {
if (error instanceof Error) {
console.warn(`Skipping invalid additionalDatasetMapping at index ${index}: ${error.message}`);
} else {
console.warn(`Skipping invalid additionalDatasetMapping at index ${index}: Unknown error`);
}
return null;
}
return null;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [mappedAdditionalMapping, _] = getDatasetMapping(additionalMapping, headerIndexes);
return mappedAdditionalMapping;
}).filter(mapping => mapping !== null) as DatasetMapping[];
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [mappedAdditionalMapping, _] = getDatasetMapping(additionalMapping, headerIndexes);
return mappedAdditionalMapping;
}).filter(mapping => mapping !== null) as DatasetMapping[];
}, [rawAdditionalDatasetMappings, headerIndexes]);

const suggestions = useMemo(() => {
return computeSuggestions(initialDatasetMapping, additionalDatasetMappings, headerIndexes);
Expand Down