Handle open/close correctly

This commit is contained in:
Adria Navarro 2023-11-10 10:39:48 +01:00
parent 226d287584
commit 3f814c6a9b
1 changed files with 31 additions and 18 deletions

View File

@ -23,7 +23,7 @@
import { userSelectedResourceMap } from "builderStore" import { userSelectedResourceMap } from "builderStore"
export let searchTerm export let searchTerm
let openDataSources = [] let dataSourcesVisibility = {}
$: enrichedDataSources = enrichDatasources( $: enrichedDataSources = enrichDatasources(
$datasources, $datasources,
@ -33,10 +33,27 @@
$queries, $queries,
$views, $views,
$viewsV2, $viewsV2,
openDataSources, dataSourcesVisibility,
searchTerm searchTerm
) )
const showDatasourceOpen = ({
selected,
containsSelected,
dataSourceVisibility,
searchTerm,
}) => {
if (searchTerm) {
return true
}
if (dataSourceVisibility !== undefined) {
return dataSourceVisibility
}
return selected || containsSelected
}
const enrichDatasources = ( const enrichDatasources = (
datasources, datasources,
params, params,
@ -45,7 +62,7 @@
queries, queries,
views, views,
viewsV2, viewsV2,
openDataSources, dataSourcesVisibility,
searchTerm searchTerm
) => { ) => {
if (!datasources?.list?.length) { if (!datasources?.list?.length) {
@ -56,7 +73,6 @@
const selected = const selected =
isActive("./datasource") && isActive("./datasource") &&
datasources.selectedDatasourceId === datasource._id datasources.selectedDatasourceId === datasource._id
const open = openDataSources.includes(datasource._id)
const containsSelected = containsActiveEntity( const containsSelected = containsActiveEntity(
datasource, datasource,
params, params,
@ -80,11 +96,18 @@
(!searchTerm || (!searchTerm ||
query.name?.toLowerCase()?.indexOf(searchTerm.toLowerCase()) > -1) query.name?.toLowerCase()?.indexOf(searchTerm.toLowerCase()) > -1)
) )
const open = showDatasourceOpen({
selected,
containsSelected,
dataSourceVisibility: dataSourcesVisibility[datasource._id],
searchTerm,
})
return { return {
...datasource, ...datasource,
selected, selected,
containsSelected, containsSelected,
open: containsSelected || open || onlySource || !!searchTerm, open,
queries: dsQueries, queries: dsQueries,
tables: dsTables, tables: dsTables,
} }
@ -103,23 +126,13 @@
} }
} }
function closeNode(datasource) {
openDataSources = openDataSources.filter(id => datasource._id !== id)
}
function openNode(datasource) { function openNode(datasource) {
if (!openDataSources.includes(datasource._id)) { dataSourcesVisibility[datasource._id] = true
openDataSources = [...openDataSources, datasource._id]
}
} }
function toggleNode(datasource) { function toggleNode(datasource) {
const isOpen = openDataSources.includes(datasource._id) dataSourcesVisibility[datasource._id] =
if (isOpen) { !dataSourcesVisibility[datasource._id]
closeNode(datasource)
} else {
openNode(datasource)
}
} }
const containsActiveEntity = ( const containsActiveEntity = (