Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track viewed tabs in TabWidget component #144

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions gui/src/app/components/TabWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { FunctionComponent, useState } from "react";
import { FunctionComponent, useEffect, useState } from "react";
import Tabs from "@mui/material/Tabs";
import Tab from "@mui/material/Tab";
import Box from "@mui/material/Box";
Expand All @@ -15,6 +15,17 @@ const TabWidget: FunctionComponent<TabWidgetProps> = ({ labels, children }) => {
}

const [index, setIndex] = useState(0);
const [tabsThatHaveBeenViewed, setTabsThatHaveBeenViewed] = useState<
number[]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on using a boolean[] of the same length as labels, rather than a number[]?

>([]);
magland marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
WardBrian marked this conversation as resolved.
Show resolved Hide resolved
setTabsThatHaveBeenViewed((prev) => {
if (!prev.includes(index)) {
return [...prev, index];
}
return prev;
});
}, [index]);

const handleChange = (_: React.SyntheticEvent, newValue: number) => {
setIndex(newValue);
Expand All @@ -29,7 +40,12 @@ const TabWidget: FunctionComponent<TabWidgetProps> = ({ labels, children }) => {
</Tabs>
<Box flex="1" overflow="scroll">
{children.map((child, i) => (
<CustomTabPanel key={i} value={index} index={i}>
<CustomTabPanel
key={i}
value={index}
index={i}
tabsThatHaveBeenViewed={tabsThatHaveBeenViewed}
WardBrian marked this conversation as resolved.
Show resolved Hide resolved
>
{child}
</CustomTabPanel>
))}
Expand All @@ -41,11 +57,12 @@ const TabWidget: FunctionComponent<TabWidgetProps> = ({ labels, children }) => {
interface TabPanelProps {
children?: React.ReactNode;
index: number;
tabsThatHaveBeenViewed: number[];
value: number;
}

const CustomTabPanel = (props: TabPanelProps) => {
const { children, value, index, ...other } = props;
const { children, value, index, tabsThatHaveBeenViewed, ...other } = props;

return (
<div
Expand All @@ -56,7 +73,9 @@ const CustomTabPanel = (props: TabPanelProps) => {
style={{ height: "100%", width: "100%" }}
{...other}
>
{value === index && <>{children}</>}
{(value === index || tabsThatHaveBeenViewed.includes(index)) && (
WardBrian marked this conversation as resolved.
Show resolved Hide resolved
<>{children}</>
)}
</div>
);
};
Expand Down