kgabryje commented on code in PR #33104: URL: https://github.com/apache/superset/pull/33104#discussion_r2042336967
########## superset-frontend/src/explore/components/DatasourcePanel/DatasourceItems.tsx: ########## @@ -0,0 +1,165 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { VariableSizeList as List } from 'react-window'; +import { FlattenedItem, Folder } from './types'; +import DatasourcePanelItem from './DatasourcePanelItem'; + +const BORDER_WIDTH = 2; +const HEADER_ITEM_HEIGHT = 50; +const METRIC_OR_COLUMN_ITEM_HEIGHT = 32; +const SUBTITLE_ITEM_HEIGHT = 32; +const DIVIDER_ITEM_HEIGHT = 16; + +const flattenFolderStructure = ( + foldersToFlatten: Folder[], + collapsedFolderIds: Set<string>, + depth = 0, + folderMap: Map<string, Folder> = new Map(), +): { flattenedItems: FlattenedItem[]; folderMap: Map<string, Folder> } => { + const flattenedItems: FlattenedItem[] = []; + + foldersToFlatten.forEach((folder, idx) => { + folderMap.set(folder.id, folder); + + flattenedItems.push({ + type: 'header', + folderId: folder.id, + depth, + height: HEADER_ITEM_HEIGHT, + }); + + if (!collapsedFolderIds.has(folder.id)) { + flattenedItems.push({ + type: 'subtitle', + folderId: folder.id, + depth, + height: SUBTITLE_ITEM_HEIGHT, + totalItems: folder.totalItems, + showingItems: folder.showingItems, + }); + folder.items.forEach(item => { + flattenedItems.push({ + type: 'item', + folderId: folder.id, + depth, + item, + height: METRIC_OR_COLUMN_ITEM_HEIGHT, + }); + }); + + if (folder.subFolders && folder.subFolders.length > 0) { + const { flattenedItems: subItems } = flattenFolderStructure( + folder.subFolders, + collapsedFolderIds, + depth + 1, + folderMap, + ); + + flattenedItems.push(...subItems); + } + } + if (depth === 0 && idx !== foldersToFlatten.length - 1) { + flattenedItems.push({ + type: 'divider', + folderId: folder.id, + depth, + height: DIVIDER_ITEM_HEIGHT, + }); + } + }); + + return { flattenedItems, folderMap }; +}; + +interface DatasourceItemsProps { + width: number; + height: number; + folders: Folder[]; +} +export const DatasourceItems = ({ + width, + height, + folders, +}: DatasourceItemsProps) => { + const listRef = useRef<List>(null); + const [collapsedFolderIds, setCollapsedFolderIds] = useState<Set<string>>( Review Comment: @justinpark per your comment from https://github.com/apache/superset/pull/32548, we store collapsed folder ids instead of deep cloning the folders -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
