Skip to content

Commit e87f50b

Browse files
authored
fix: add failsafe for how tabs are stored (#23)
1 parent efa191b commit e87f50b

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/components/databrowser/components/databrowser-tabs.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
import { IconPlus } from "@tabler/icons-react"
2-
import { Button } from "@/components/ui/button"
31
import type { TabId } from "@/store"
42
import { useDatabrowserStore } from "@/store"
53
import { TabIdProvider } from "@/tab-provider"
4+
import { IconPlus } from "@tabler/icons-react"
5+
6+
import { Button } from "@/components/ui/button"
7+
68
import { Tab } from "./tab"
79

810
export const DatabrowserTabs = () => {
9-
const { tabs, addTab } = useDatabrowserStore()
11+
const { tabs, addTab, selectedTab } = useDatabrowserStore()
1012

1113
return (
1214
<div className="relative mb-2 shrink-0">
1315
<div className="absolute bottom-0 left-0 right-0 -z-10 h-[1px] w-full bg-zinc-200" />
1416

1517
<div className="scrollbar-hide flex translate-y-[1px] items-center gap-1 overflow-x-scroll pb-[1px] [&::-webkit-scrollbar]:hidden">
16-
{tabs.map(([id]) => (
17-
<TabIdProvider key={id} value={id as TabId}>
18-
<Tab id={id} />
19-
</TabIdProvider>
20-
))}
18+
{selectedTab &&
19+
tabs.map(([id]) => (
20+
<TabIdProvider key={id} value={id as TabId}>
21+
<Tab id={id} />
22+
</TabIdProvider>
23+
))}
2124
<Button
2225
variant="secondary"
2326
size="icon-sm"

src/components/databrowser/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ export const RedisBrowser = ({
6969
}
7070

7171
const DatabrowserInstances = () => {
72-
const { tabs, selectedTab, addTab } = useDatabrowserStore()
72+
const { tabs, selectedTab, selectTab, addTab } = useDatabrowserStore()
7373

7474
useEffect(() => {
7575
if (tabs.length === 0) addTab()
76-
}, [tabs])
76+
else if (!selectedTab) selectTab(tabs[0][0])
77+
}, [tabs, selectedTab, addTab, selectTab])
78+
79+
if (!selectedTab) return
7780

7881
return tabs.map(([id]) => (
7982
<TabIdProvider key={id} value={id as TabId}>

src/tab-provider.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createContext, useContext, useMemo } from "react"
2+
23
import type { SearchFilter, SelectedItem } from "./store"
34
import { useDatabrowserStore, type TabId } from "./store"
45
import type { DataType } from "./types"
@@ -30,7 +31,8 @@ export const useTab = () => {
3031
const tabId = useTabId()
3132
const tabData = useMemo(() => tabs.find(([id]) => id === tabId)?.[1], [tabs, tabId])
3233

33-
if (!selectedTab || !tabData) throw new Error("selectedTab is undefined when using useTab()")
34+
if (!selectedTab) throw new Error("selectedTab is undefined when using useTab()")
35+
if (!tabData) throw new Error("tabData is undefined when using useTab()")
3436

3537
return useMemo(
3638
() => ({

0 commit comments

Comments
 (0)