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(sqllab): avoid unexpected re-rendering on DatabaseSelector #21316

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
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