diff --git a/src/Explorer/Explorer.tsx b/src/Explorer/Explorer.tsx index 367693c99..971ac43a5 100644 --- a/src/Explorer/Explorer.tsx +++ b/src/Explorer/Explorer.tsx @@ -93,6 +93,7 @@ export interface ExplorerParams { closeSidePanel: () => void; closeDialog: () => void; openDialog: (props: DialogProps) => void; + tabsManager: TabsManager; } export default class Explorer { @@ -600,7 +601,7 @@ export default class Explorer { container: this, }); - this.tabsManager = new TabsManager(); + this.tabsManager = params?.tabsManager ?? new TabsManager(); this._panes = [ this.addDatabasePane, diff --git a/src/Explorer/SplashScreen/SplashScreen.tsx b/src/Explorer/SplashScreen/SplashScreen.tsx index 634ba3e44..e5debb883 100644 --- a/src/Explorer/SplashScreen/SplashScreen.tsx +++ b/src/Explorer/SplashScreen/SplashScreen.tsx @@ -50,10 +50,6 @@ export class SplashScreen extends React.Component { this.subscriptions = []; } - public shouldComponentUpdate() { - return this.container.tabsManager.openedTabs.length === 0; - } - public componentWillUnmount() { while (this.subscriptions.length) { this.subscriptions.pop().dispose(); @@ -62,7 +58,6 @@ export class SplashScreen extends React.Component { public componentDidMount() { this.subscriptions.push( - this.container.tabsManager.openedTabs.subscribe(() => this.setState({})), this.container.selectedNode.subscribe(() => this.setState({})), this.container.isNotebookEnabled.subscribe(() => this.setState({})) ); @@ -80,7 +75,13 @@ export class SplashScreen extends React.Component { const tipsItems = this.createTipsItems(); const onClearRecent = this.clearMostRecent; - return ( + const formContainer = (jsx: JSX.Element) => ( +
+
{jsx}
+
+ ); + + return formContainer(
diff --git a/src/Main.tsx b/src/Main.tsx index d829e897b..4137aa59f 100644 --- a/src/Main.tsx +++ b/src/Main.tsx @@ -53,6 +53,7 @@ import "./Explorer/Tabs/QueryTab.less"; import { useConfig } from "./hooks/useConfig"; import { useKnockoutExplorer } from "./hooks/useKnockoutExplorer"; import { useSidePanel } from "./hooks/useSidePanel"; +import { useTabs } from "./hooks/useTabs"; import { KOCommentEnd, KOCommentIfStart } from "./koComment"; import "./Libs/jquery"; import "./Shared/appInsights"; @@ -78,6 +79,7 @@ const App: React.FunctionComponent = () => { }; const { isPanelOpen, panelContent, headerText, openSidePanel, closeSidePanel } = useSidePanel(); + const { tabs, tabsManager } = useTabs(); const explorerParams: ExplorerParams = { setIsNotificationConsoleExpanded, @@ -87,7 +89,9 @@ const App: React.FunctionComponent = () => { closeSidePanel, openDialog, closeDialog, + tabsManager, }; + const config = useConfig(); const explorer = useKnockoutExplorer(config?.platform, explorerParams); @@ -200,11 +204,7 @@ const App: React.FunctionComponent = () => { {/* Splitter - End */}
{/* Collections Tree - End */} -
-
- - -
+ {tabs.length === 0 && }
{/* Collections Tree and Tabs - End */} diff --git a/src/hooks/useObservableState.ts b/src/hooks/useObservableState.ts new file mode 100644 index 000000000..8894499b5 --- /dev/null +++ b/src/hooks/useObservableState.ts @@ -0,0 +1,16 @@ +import { isObservableArray, Observable, ObservableArray } from "knockout"; +import { useEffect, useState } from "react"; + +export function useObservableState(observable: Observable): [T, (s: T) => void]; +export function useObservableState(observable: ObservableArray): [T[], (s: T[]) => void]; +export function useObservableState(observable: ObservableArray | Observable): [T | T[], (s: T | T[]) => void] { + const [value, setValue] = useState(observable()); + + useEffect(() => { + isObservableArray(observable) + ? observable.subscribe((values) => setValue([...values])) + : observable.subscribe(setValue); + }, [observable]); + + return [value, observable]; +} diff --git a/src/hooks/useTabs.ts b/src/hooks/useTabs.ts new file mode 100644 index 000000000..b487accbd --- /dev/null +++ b/src/hooks/useTabs.ts @@ -0,0 +1,16 @@ +import { useState } from "react"; +import TabsBase from "../Explorer/Tabs/TabsBase"; +import { TabsManager } from "../Explorer/Tabs/TabsManager"; +import { useObservableState } from "./useObservableState"; + +export type UseTabs = { + tabs: readonly TabsBase[]; + tabsManager: TabsManager; +}; + +export function useTabs(): UseTabs { + const [tabsManager] = useState(() => new TabsManager()); + const [tabs] = useObservableState(tabsManager.openedTabs); + + return { tabs, tabsManager }; +}