Skip to content

Commit

Permalink
Improve Data Importer Select Matching - Post Merge Updates (#6750)
Browse files Browse the repository at this point in the history
This PR was created by [GitStart](https://gitstart.com/) to address the
requirements from this ticket:
[TWNTY-6135-1](https://clients.gitstart.com/twenty/5449/tickets/TWNTY-6135-1).

 --- 
### Description
This [PR](#6338) was merged, but
the reviewr asked for some changes in another PR
#6338 (review)


### Refs
#6338

### Demo
<https://jam.dev/c/80336c7a-4536-4a58-b531-981bac81cb26>

Co-authored-by: gitstart-twenty <[email protected]>
  • Loading branch information
gitstart-app[bot] and gitstart-twenty authored Aug 28, 2024
1 parent 0531eb5 commit 7c894fe
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const SelectFieldInput = ({
fieldDefinition.metadata.isNullable ? handleClearField : undefined
}
clearLabel={fieldDefinition.label}
hotkeyScope={hotkeyScope}
/>
</SelectableList>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';

import { Modal } from '@/ui/layout/modal/components/Modal';

import { initialComputedColumnsSelector } from '@/spreadsheet-import/steps/components/MatchColumnsStep/components/states/initialComputedColumnsState';
import { UnmatchColumn } from '@/spreadsheet-import/steps/components/MatchColumnsStep/components/UnmatchColumn';
import { initialComputedColumnsState } from '@/spreadsheet-import/steps/components/MatchColumnsStep/components/states/initialComputedColumnsState';
import { SpreadsheetImportStep } from '@/spreadsheet-import/steps/types/SpreadsheetImportStep';
import { SpreadsheetImportStepType } from '@/spreadsheet-import/steps/types/SpreadsheetImportStepType';
import { useRecoilState } from 'recoil';
Expand Down Expand Up @@ -63,7 +63,7 @@ export type MatchColumnsStepProps = {
setPreviousStepState: (currentStepState: SpreadsheetImportStep) => void;
currentStepState: SpreadsheetImportStep;
nextStep: () => void;
errorToast: (message: string) => void;
onError: (message: string) => void;
};

export enum ColumnType {
Expand Down Expand Up @@ -136,7 +136,7 @@ export const MatchColumnsStep = <T extends string>({
setPreviousStepState,
currentStepState,
nextStep,
errorToast,
onError,
}: MatchColumnsStepProps) => {
const { enqueueDialog } = useDialogManager();
const { enqueueSnackBar } = useSnackBar();
Expand All @@ -145,7 +145,7 @@ export const MatchColumnsStep = <T extends string>({
useSpreadsheetImportInternal<T>();
const [isLoading, setIsLoading] = useState(false);
const [columns, setColumns] = useRecoilState(
initialComputedColumnsState(headerValues),
initialComputedColumnsSelector(headerValues),
);

const { matchColumnsStepHook } = useSpreadsheetImportInternal();
Expand Down Expand Up @@ -215,7 +215,7 @@ export const MatchColumnsStep = <T extends string>({
],
);

const onContinue = useCallback(
const handleContinue = useCallback(
async (
values: ImportedStructuredRow<string>[],
rawData: ImportedRow[],
Expand All @@ -231,11 +231,11 @@ export const MatchColumnsStep = <T extends string>({
setPreviousStepState(currentStepState);
nextStep();
} catch (e) {
errorToast((e as Error).message);
onError((e as Error).message);
}
},
[
errorToast,
onError,
matchColumnsStepHook,
nextStep,
setPreviousStepState,
Expand Down Expand Up @@ -263,9 +263,13 @@ export const MatchColumnsStep = <T extends string>({

const handleAlertOnContinue = useCallback(async () => {
setIsLoading(true);
await onContinue(normalizeTableData(columns, data, fields), data, columns);
await handleContinue(
normalizeTableData(columns, data, fields),
data,
columns,
);
setIsLoading(false);
}, [onContinue, columns, data, fields]);
}, [handleContinue, columns, data, fields]);

const handleOnContinue = useCallback(async () => {
if (unmatchedRequiredFields.length > 0) {
Expand Down Expand Up @@ -293,7 +297,7 @@ export const MatchColumnsStep = <T extends string>({
});
} else {
setIsLoading(true);
await onContinue(
await handleContinue(
normalizeTableData(columns, data, fields),
data,
columns,
Expand All @@ -304,7 +308,7 @@ export const MatchColumnsStep = <T extends string>({
unmatchedRequiredFields,
enqueueDialog,
handleAlertOnContinue,
onContinue,
handleContinue,
columns,
data,
fields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { SelectOption } from '@/spreadsheet-import/types';

import { getFieldOptions } from '@/spreadsheet-import/utils/getFieldOptions';

import { SelectFieldHotkeyScope } from '@/object-record/select/types/SelectFieldHotkeyScope';
import { SelectInput } from '@/ui/input/components/SelectInput';
import { useState } from 'react';
import { useSetHotkeyScope } from '@/ui/utilities/hotkey/hooks/useSetHotkeyScope';
import { useEffect, useState } from 'react';
import { IconChevronDown, Tag, TagColor } from 'twenty-ui';
import {
MatchedOptions,
Expand Down Expand Up @@ -83,6 +85,12 @@ export const SubMatchingSelect = <T extends string>({
setIsOpen(false);
};

const setHotkeyScope = useSetHotkeyScope();

useEffect(() => {
setHotkeyScope(SelectFieldHotkeyScope.SelectField);
}, [setHotkeyScope]);

return (
<StyledContainer>
<StyledControlContainer cursor="default">
Expand Down Expand Up @@ -113,6 +121,7 @@ export const SubMatchingSelect = <T extends string>({
options={options}
onOptionSelected={handleSelect}
onCancel={() => setIsOpen(false)}
hotkeyScope={SelectFieldHotkeyScope.SelectField}
/>
)}
</StyledControlContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export const matchColumnsState = atom({
default: [] as Columns<string>,
});

export const initialComputedColumnsState = selectorFamily<
export const initialComputedColumnsSelector = selectorFamily<
Columns<string>,
ImportedRow
>({
key: 'InitialComputedColumnsState',
key: 'initialComputedColumnsSelector',
get:
(headerValues: ImportedRow) =>
({ get }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type SelectHeaderStepProps = {
setCurrentStepState: (currentStepState: SpreadsheetImportStep) => void;
nextStep: () => void;
setPreviousStepState: (currentStepState: SpreadsheetImportStep) => void;
errorToast: (message: string) => void;
onError: (message: string) => void;
onBack: () => void;
currentStepState: SpreadsheetImportStep;
};
Expand All @@ -37,7 +37,7 @@ export const SelectHeaderStep = ({
setCurrentStepState,
nextStep,
setPreviousStepState,
errorToast,
onError,
onBack,
currentStepState,
}: SelectHeaderStepProps) => {
Expand All @@ -49,7 +49,7 @@ export const SelectHeaderStep = ({

const { selectHeaderStepHook } = useSpreadsheetImportInternal();

const onContinue = useCallback(
const handleContinue = useCallback(
async (...args: Parameters<typeof selectHeaderStepHook>) => {
try {
const { importedRows: data, headerRow: headerValues } =
Expand All @@ -62,11 +62,11 @@ export const SelectHeaderStep = ({
setPreviousStepState(currentStepState);
nextStep();
} catch (e) {
errorToast((e as Error).message);
onError((e as Error).message);
}
},
[
errorToast,
onError,
nextStep,
selectHeaderStepHook,
setPreviousStepState,
Expand All @@ -75,17 +75,17 @@ export const SelectHeaderStep = ({
],
);

const handleContinue = useCallback(async () => {
const handleOnContinue = useCallback(async () => {
const [selectedRowIndex] = Array.from(new Set(selectedRowIndexes));
// We consider data above header to be redundant
const trimmedData = importedRows.slice(selectedRowIndex + 1);

setIsLoading(true);

await onContinue(importedRows[selectedRowIndex], trimmedData);
await handleContinue(importedRows[selectedRowIndex], trimmedData);

setIsLoading(false);
}, [onContinue, importedRows, selectedRowIndexes]);
}, [handleContinue, importedRows, selectedRowIndexes]);

return (
<>
Expand All @@ -100,7 +100,7 @@ export const SelectHeaderStep = ({
</StyledTableContainer>
</Modal.Content>
<StepNavigationButton
onClick={handleContinue}
onClick={handleOnContinue}
onBack={onBack}
title="Continue"
isLoading={isLoading}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type SelectSheetStepProps = {
sheetNames: string[];
onBack: () => void;
setCurrentStepState: (data: SpreadsheetImportStep) => void;
errorToast: (message: string) => void;
onError: (message: string) => void;
setPreviousStepState: (data: SpreadsheetImportStep) => void;
currentStepState: {
type: SpreadsheetImportStepType.selectSheet;
Expand All @@ -46,7 +46,7 @@ type SelectSheetStepProps = {
export const SelectSheetStep = ({
sheetNames,
setCurrentStepState,
errorToast,
onError,
setPreviousStepState,
onBack,
currentStepState,
Expand All @@ -57,7 +57,7 @@ export const SelectSheetStep = ({

const { maxRecords, uploadStepHook } = useSpreadsheetImportInternal();

const onContinue = useCallback(
const handleContinue = useCallback(
async (sheetName: string) => {
if (
maxRecords > 0 &&
Expand All @@ -66,7 +66,7 @@ export const SelectSheetStep = ({
maxRecords,
)
) {
errorToast(`Too many records. Up to ${maxRecords.toString()} allowed`);
onError(`Too many records. Up to ${maxRecords.toString()} allowed`);
return;
}
try {
Expand All @@ -79,11 +79,11 @@ export const SelectSheetStep = ({
});
setPreviousStepState(currentStepState);
} catch (e) {
errorToast((e as Error).message);
onError((e as Error).message);
}
},
[
errorToast,
onError,
maxRecords,
currentStepState,
setPreviousStepState,
Expand All @@ -95,10 +95,10 @@ export const SelectSheetStep = ({
const handleOnContinue = useCallback(
async (data: typeof value) => {
setIsLoading(true);
await onContinue(data);
await handleContinue(data);
setIsLoading(false);
},
[onContinue],
[handleContinue],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const SpreadsheetImportStepper = ({

const { enqueueSnackBar } = useSnackBar();

const errorToast = useCallback(
const handleError = useCallback(
(description: string) => {
enqueueSnackBar(description, {
title: 'Error',
Expand All @@ -58,7 +58,7 @@ export const SpreadsheetImportStepper = ({
[enqueueSnackBar],
);

const onBack = useCallback(() => {
const handleBack = useCallback(() => {
setCurrentStepState(previousStepState);
prevStep();
}, [prevStep, previousStepState]);
Expand All @@ -71,7 +71,7 @@ export const SpreadsheetImportStepper = ({
currentStepState={currentStepState}
setPreviousStepState={setPreviousStepState}
setCurrentStepState={setCurrentStepState}
errorToast={errorToast}
onError={handleError}
nextStep={nextStep}
/>
);
Expand All @@ -81,9 +81,9 @@ export const SpreadsheetImportStepper = ({
sheetNames={currentStepState.workbook.SheetNames}
setCurrentStepState={setCurrentStepState}
currentStepState={currentStepState}
errorToast={errorToast}
onError={handleError}
setPreviousStepState={setPreviousStepState}
onBack={onBack}
onBack={handleBack}
/>
);
case SpreadsheetImportStepType.selectHeader:
Expand All @@ -93,8 +93,8 @@ export const SpreadsheetImportStepper = ({
setCurrentStepState={setCurrentStepState}
nextStep={nextStep}
setPreviousStepState={setPreviousStepState}
errorToast={errorToast}
onBack={onBack}
onError={handleError}
onBack={handleBack}
currentStepState={currentStepState}
/>
);
Expand All @@ -107,10 +107,8 @@ export const SpreadsheetImportStepper = ({
setPreviousStepState={setPreviousStepState}
currentStepState={currentStepState}
nextStep={nextStep}
onBack={() => {
onBack();
}}
errorToast={errorToast}
onBack={handleBack}
onError={handleError}
/>
);
case SpreadsheetImportStepType.validateData:
Expand All @@ -124,7 +122,7 @@ export const SpreadsheetImportStepper = ({
file={uploadedFile}
setCurrentStepState={setCurrentStepState}
onBack={() => {
onBack();
handleBack();
setPreviousStepState(
initialStepState || { type: SpreadsheetImportStepType.upload },
);
Expand Down
Loading

0 comments on commit 7c894fe

Please sign in to comment.