Skip to content

Commit

Permalink
fix(sqllab): avoid unexpected re-rendering on DatabaseSelector (#21316)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpark authored Sep 23, 2022
1 parent 2ec744d commit e2b77a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
31 changes: 20 additions & 11 deletions superset-frontend/src/components/DatabaseSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,7 @@ export default function DatabaseSelector({
}: DatabaseSelectorProps) {
const [loadingSchemas, setLoadingSchemas] = useState(false);
const [schemaOptions, setSchemaOptions] = useState<SchemaValue[]>([]);
const [currentDb, setCurrentDb] = useState<DatabaseValue | undefined>(
db
? {
label: (
<SelectLabel backend={db.backend} databaseName={db.database_name} />
),
value: db.id,
...db,
}
: undefined,
);
const [currentDb, setCurrentDb] = useState<DatabaseValue | undefined>();
const [currentSchema, setCurrentSchema] = useState<SchemaValue | undefined>(
schema ? { label: schema, value: schema } : undefined,
);
Expand Down Expand Up @@ -208,6 +198,25 @@ export default function DatabaseSelector({
[formMode, getDbList, sqlLabMode],
);

useEffect(() => {
setCurrentDb(current =>
current?.id !== db?.id
? db
? {
label: (
<SelectLabel
backend={db.backend}
databaseName={db.database_name}
/>
),
value: db.id,
...db,
}
: undefined
: current,
);
}, [db]);

useEffect(() => {
if (currentDb) {
setLoadingSchemas(true);
Expand Down
17 changes: 9 additions & 8 deletions superset-frontend/src/components/Select/AsyncSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ const AsyncSelect = forwardRef(
const [totalCount, setTotalCount] = useState(0);
const [loadingEnabled, setLoadingEnabled] = useState(!lazyLoading);
const [allValuesLoaded, setAllValuesLoaded] = useState(false);
const selectValueRef = useRef(selectValue);
const fetchedQueries = useRef(new Map<string, number>());
const mappedMode = isSingleMode
? undefined
Expand All @@ -193,10 +194,14 @@ const AsyncSelect = forwardRef(
: 'multiple';
const allowFetch = !fetchOnlyOnSearch || inputValue;

useEffect(() => {
selectValueRef.current = selectValue;
}, [selectValue]);

const sortSelectedFirst = useCallback(
(a: AntdLabeledValue, b: AntdLabeledValue) =>
sortSelectedFirstHelper(a, b, selectValue),
[selectValue],
sortSelectedFirstHelper(a, b, selectValueRef.current),
[],
);

const sortComparatorWithSearch = useCallback(
Expand Down Expand Up @@ -334,6 +339,7 @@ const AsyncSelect = forwardRef(
return;
}
setIsLoading(true);

const fetchOptions = options as SelectOptionsPagePromise;
fetchOptions(search, page, pageSize)
.then(({ data, totalCount }: SelectOptionsTypePage) => {
Expand All @@ -342,7 +348,7 @@ const AsyncSelect = forwardRef(
setTotalCount(totalCount);
if (
!fetchOnlyOnSearch &&
value === '' &&
search === '' &&
mergedData.length >= totalCount
) {
setAllValuesLoaded(true);
Expand All @@ -360,7 +366,6 @@ const AsyncSelect = forwardRef(
internalOnError,
options,
pageSize,
value,
],
);

Expand Down Expand Up @@ -512,10 +517,6 @@ const AsyncSelect = forwardRef(
[ref],
);

useEffect(() => {
setSelectValue(value);
}, [value]);

return (
<StyledContainer>
{header}
Expand Down
1 change: 0 additions & 1 deletion superset-frontend/src/components/TableSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ const TableSelector: FunctionComponent<TableSelectorProps> = ({
function renderDatabaseSelector() {
return (
<DatabaseSelector
key={database?.id}
db={database}
emptyState={emptyState}
formMode={formMode}
Expand Down

0 comments on commit e2b77a7

Please sign in to comment.