Skip to content

Commit 4951581

Browse files
committed
fix: use url hash for active tab state
Signed-off-by: Nick Mitchell <[email protected]> Signed-off-by: Nick Mitchell <[email protected]>
1 parent 8bed4ae commit 4951581

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

pdl-live-react/src/Viewer.tsx

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { useCallback, useContext, useMemo, useState } from "react"
1+
import { useLocation } from "react-router"
2+
import { useContext, useMemo } from "react"
23

3-
import { Tabs, Tab, TabTitleText, type TabsProps } from "@patternfly/react-core"
4+
import { Tabs, Tab, TabTitleText } from "@patternfly/react-core"
45

56
import Code from "./view/Code"
67
import Transcript from "./view/transcript/Transcript"
@@ -12,6 +13,9 @@ import "./Viewer.css"
1213

1314
/** This is the main view component */
1415
export default function Viewer({ value }: { value: string }) {
16+
// We will use this to find the current active tab (below)
17+
let { hash: activeTab } = useLocation()
18+
1519
// DarkMode state
1620
const darkMode = useContext(DarkModeContext)
1721

@@ -20,28 +24,46 @@ export default function Viewer({ value }: { value: string }) {
2024
[value],
2125
)
2226

23-
const [activeTab, setActiveTab] = useState<string | number>("transcript")
24-
const handleTabClick = useCallback<Required<TabsProps>["onSelect"]>(
25-
(_event, tab) => setActiveTab(tab),
26-
[setActiveTab],
27-
)
27+
if (!data) {
28+
return "Invalid trace content"
29+
}
30+
31+
// Note: please keep eventKey===href
32+
const tabs = [
33+
<Tab
34+
key="#transcript"
35+
href="#transcript"
36+
eventKey="#transcript"
37+
title={<TabTitleText>Transcript</TabTitleText>}
38+
>
39+
<Transcript data={data} />
40+
</Tab>,
41+
<Tab
42+
key="#source"
43+
href="#source"
44+
eventKey="#source"
45+
title={<TabTitleText>Source</TabTitleText>}
46+
>
47+
<Code block={data} darkMode={darkMode} limitHeight={false} />
48+
</Tab>,
49+
<Tab
50+
key="#raw"
51+
href="#raw"
52+
eventKey="#raw"
53+
title={<TabTitleText>Raw Trace</TabTitleText>}
54+
>
55+
<Code block={data} darkMode={darkMode} limitHeight={false} raw />
56+
</Tab>,
57+
]
58+
59+
if (!tabs.find((tab) => tab.props.href === activeTab)) {
60+
// User provided bogus hash, default to first tab
61+
activeTab = tabs[0].props.href
62+
}
2863

2964
return (
30-
data && (
31-
<Tabs activeKey={activeTab} onSelect={handleTabClick}>
32-
<Tab
33-
eventKey="transcript"
34-
title={<TabTitleText>Transcript</TabTitleText>}
35-
>
36-
<Transcript data={data} />
37-
</Tab>
38-
<Tab eventKey="source" title={<TabTitleText>Source</TabTitleText>}>
39-
<Code block={data} darkMode={darkMode} limitHeight={false} />
40-
</Tab>
41-
<Tab eventKey="rawtrace" title={<TabTitleText>Raw Trace</TabTitleText>}>
42-
<Code block={data} darkMode={darkMode} limitHeight={false} raw />
43-
</Tab>
44-
</Tabs>
45-
)
65+
<Tabs activeKey={activeTab} component="nav">
66+
{tabs}
67+
</Tabs>
4668
)
4769
}

0 commit comments

Comments
 (0)