MCPcopy Create free account
hub / github.com/bytebase/bytebase / useTabs

Function useTabs

frontend/src/react/components/SchemaEditorLite/useTabs.ts:6–149  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4import type { TabsContext } from "./types";
5
6export function useTabs(): TabsContext {
7 const tabMapRef = useRef(new Map<string, TabContext>());
8 const [currentTabId, setCurrentTabId] = useState("");
9 // Version counter to trigger re-renders when tabMap mutates.
10 const [, setVersion] = useState(0);
11 const bump = useCallback(() => setVersion((v) => v + 1), []);
12
13 // tabMapRef.current.size and currentTabId are deps to re-derive on mutations
14 const tabList = useMemo(
15 () => Array.from(tabMapRef.current.values()),
16 [tabMapRef.current.size, currentTabId]
17 );
18
19 const currentTab = useMemo(
20 () => (currentTabId ? tabMapRef.current.get(currentTabId) : undefined),
21 [currentTabId]
22 );
23
24 const findTab = useCallback(
25 (target: CoreTabContext): TabContext | undefined => {
26 for (const tab of tabMapRef.current.values()) {
27 if (tab.type !== target.type) continue;
28 if (tab.database.name !== target.database.name) continue;
29 if (tab.type === "database") {
30 if (tab.metadata.database.name === target.metadata.database.name) {
31 return tab;
32 }
33 }
34 // After the type guard above, both tab and target have the same type.
35 // We use `as any` on target.metadata to access the narrowed fields
36 // since TS can't narrow two variables from the same discriminant check.
37 const tabMeta = tab.metadata as Record<string, { name: string }>;
38 const targetMeta = target.metadata as Record<string, { name: string }>;
39 if (tab.type === "table") {
40 if (
41 tabMeta.schema.name === targetMeta.schema.name &&
42 tabMeta.table.name === targetMeta.table.name
43 ) {
44 return tab;
45 }
46 }
47 if (tab.type === "view") {
48 if (
49 tabMeta.schema.name === targetMeta.schema.name &&
50 tabMeta.view.name === targetMeta.view.name
51 ) {
52 return tab;
53 }
54 }
55 if (tab.type === "procedure") {
56 if (
57 tabMeta.schema.name === targetMeta.schema.name &&
58 tabMeta.procedure.name === targetMeta.procedure.name
59 ) {
60 return tab;
61 }
62 }
63 if (tab.type === "function") {

Callers 1

Calls 6

setCurrentTabIdFunction · 0.85
clearMethod · 0.80
getMethod · 0.45
setMethod · 0.45
findIndexMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected