Skip to content

Commit 0ad48be

Browse files
committed
Refactor SecondarySplitHeader for improved readability and consistency
1 parent 8c68abe commit 0ad48be

File tree

1 file changed

+79
-104
lines changed

1 file changed

+79
-104
lines changed
Lines changed: 79 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,49 @@
1-
"use client";
1+
"use client"
22

3-
import { useState, useEffect, useContext } from "react";
4-
import { useSessionStorage } from "usehooks-ts";
5-
import {
6-
Box,
7-
IconButton,
8-
Stack,
9-
Tooltip,
10-
Collapse,
11-
Divider,
12-
} from "@mui/material";
13-
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
14-
import {
15-
faArrowRightArrowLeft,
16-
faBars,
17-
faChevronLeft,
18-
faChevronRight,
19-
} from "@fortawesome/free-solid-svg-icons";
20-
import { isMac as checkIsMac, SidebarTogglableContext } from "@/common";
21-
import { useSidebarOpen } from "@/features/sidebar/data";
22-
import useDiffbarOpen from "@/features/sidebar/data/useDiffbarOpen";
23-
import ToggleMobileToolbarButton from "./internal/secondary/ToggleMobileToolbarButton";
24-
import { useProjectSelection } from "@/features/projects/data";
3+
import { useState, useEffect, useContext } from "react"
4+
import { useSessionStorage } from "usehooks-ts"
5+
import { Box, IconButton, Stack, Tooltip, Collapse, Divider } from "@mui/material"
6+
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
7+
import { faBars, faChevronLeft, faChevronRight, faArrowRightArrowLeft } from "@fortawesome/free-solid-svg-icons"
8+
import { isMac as checkIsMac, SidebarTogglableContext } from "@/common"
9+
import { useSidebarOpen } from "@/features/sidebar/data"
10+
import useDiffbarOpen from "@/features/sidebar/data/useDiffbarOpen"
11+
import ToggleMobileToolbarButton from "./internal/secondary/ToggleMobileToolbarButton"
12+
import { useProjectSelection } from "@/features/projects/data"
2513

2614
const SecondarySplitHeader = ({
2715
mobileToolbar,
28-
children,
16+
children
2917
}: {
30-
mobileToolbar?: React.ReactNode;
31-
children?: React.ReactNode;
18+
mobileToolbar?: React.ReactNode
19+
children?: React.ReactNode
3220
}) => {
33-
const [isSidebarOpen, setSidebarOpen] = useSidebarOpen();
34-
const [isDiffbarOpen, setDiffbarOpen] = useDiffbarOpen();
35-
const [isMobileToolbarVisible, setMobileToolbarVisible] = useSessionStorage(
36-
"isMobileToolbarVisible",
37-
true
38-
);
39-
const { specification } = useProjectSelection();
21+
const [isSidebarOpen, setSidebarOpen] = useSidebarOpen()
22+
const [isDiffbarOpen, setDiffbarOpen] = useDiffbarOpen()
23+
const [isMobileToolbarVisible, setMobileToolbarVisible] = useSessionStorage("isMobileToolbarVisible", true)
24+
const { specification } = useProjectSelection()
4025
return (
4126
<Box>
42-
<Box
43-
sx={{
44-
display: "flex",
45-
alignItems: "center",
46-
paddingLeft: 2,
47-
paddingRight: 2,
48-
height: 64,
49-
margin: "auto",
50-
}}
51-
>
27+
<Box sx={{
28+
display: "flex",
29+
alignItems: "center",
30+
paddingLeft: 2,
31+
paddingRight: 2,
32+
height: 64,
33+
margin: "auto"
34+
}}>
5235
<ToggleSidebarButton
5336
isSidebarOpen={isSidebarOpen}
5437
onClick={setSidebarOpen}
5538
/>
56-
<Box sx={{ display: "flex", flexGrow: 1, justifyContent: "end" }}>
39+
<Box sx={{ display: "flex", flexGrow: 1, justifyContent: "end" }}>
5740
<Stack direction="row" alignItems="center">
5841
{children}
59-
<Divider
60-
orientation="vertical"
61-
flexItem
62-
sx={{ marginLeft: 0.5, marginRight: 0.5 }}
63-
/>
42+
<Divider orientation="vertical" flexItem sx={{ marginLeft: 0.5, marginRight: 0.5 }} />
6443
{mobileToolbar && (
6544
<ToggleMobileToolbarButton
6645
direction={isMobileToolbarVisible ? "up" : "down"}
67-
onToggle={() =>
68-
setMobileToolbarVisible(!isMobileToolbarVisible)
69-
}
46+
onToggle={() => setMobileToolbarVisible(!isMobileToolbarVisible) }
7047
/>
7148
)}
7249
</Stack>
@@ -79,86 +56,84 @@ const SecondarySplitHeader = ({
7956
</Box>
8057
{mobileToolbar && (
8158
<Collapse in={isMobileToolbarVisible}>
82-
<Box
83-
sx={{
84-
padding: 2,
85-
paddingTop: 0,
86-
display: { sm: "block", md: "none" },
87-
}}
88-
>
59+
<Box sx={{
60+
padding: 2,
61+
paddingTop: 0,
62+
display: { sm: "block", md: "none" }
63+
}}>
8964
{mobileToolbar}
9065
</Box>
9166
</Collapse>
9267
)}
9368
</Box>
94-
);
95-
};
69+
)
70+
}
9671

97-
export default SecondarySplitHeader;
72+
export default SecondarySplitHeader
9873

9974
const ToggleSidebarButton = ({
10075
isSidebarOpen,
101-
onClick,
76+
onClick
10277
}: {
103-
isSidebarOpen: boolean;
104-
onClick: (isSidebarOpen: boolean) => void;
78+
isSidebarOpen: boolean,
79+
onClick: (isSidebarOpen: boolean) => void
10580
}) => {
106-
const [isMac, setIsMac] = useState(false);
81+
const [isMac, setIsMac] = useState(false)
10782
useEffect(() => {
10883
// checkIsMac uses window so we delay the check.
10984
const timeout = window.setTimeout(() => {
110-
setIsMac(checkIsMac());
111-
}, 0);
112-
return () => window.clearTimeout(timeout);
113-
}, [setIsMac]);
114-
const isSidebarTogglable = useContext(SidebarTogglableContext);
115-
const openCloseKeyboardShortcut = `(${isMac ? "⌘" : "^"} + .)`;
116-
const tooltip = isSidebarOpen ? "Hide Projects" : "Show Projects";
85+
setIsMac(checkIsMac())
86+
}, 0)
87+
return () => window.clearTimeout(timeout)
88+
}, [setIsMac])
89+
const isSidebarTogglable = useContext(SidebarTogglableContext)
90+
const openCloseKeyboardShortcut = `(${isMac ? "⌘" : "^"} + .)`
91+
const tooltip = isSidebarOpen ? "Hide Projects" : "Show Projects"
11792
return (
11893
<Box sx={{ display: isSidebarTogglable ? "block" : "none" }}>
119-
<Tooltip title={`${tooltip} ${openCloseKeyboardShortcut}`}>
120-
<IconButton
121-
size="medium"
122-
color="primary"
123-
onClick={() => onClick(!isSidebarOpen)}
124-
edge="start"
125-
>
126-
<FontAwesomeIcon
127-
icon={isSidebarOpen ? faChevronLeft : faBars}
128-
size="sm"
129-
style={{ aspectRatio: 1, padding: 2 }}
130-
/>
131-
</IconButton>
132-
</Tooltip>
94+
<Tooltip title={`${tooltip} ${openCloseKeyboardShortcut}`}>
95+
<IconButton
96+
size="medium"
97+
color="primary"
98+
onClick={() => onClick(!isSidebarOpen)}
99+
edge="start"
100+
>
101+
<FontAwesomeIcon
102+
icon={isSidebarOpen ? faChevronLeft : faBars}
103+
size="sm"
104+
style={{ aspectRatio: 1, padding: 2 }}
105+
/>
106+
</IconButton>
107+
</Tooltip>
133108
</Box>
134-
);
135-
};
109+
)
110+
}
136111

137112
const ToggleDiffButton = ({
138113
isDiffbarOpen,
139114
onClick,
140-
isDiffAvailable,
115+
isDiffAvailable
141116
}: {
142-
isDiffbarOpen: boolean;
143-
onClick: (isDiffbarOpen: boolean) => void;
144-
isDiffAvailable: boolean;
117+
isDiffbarOpen: boolean,
118+
onClick: (isDiffbarOpen: boolean) => void,
119+
isDiffAvailable: boolean
145120
}) => {
146-
const [isMac, setIsMac] = useState(false);
121+
const [isMac, setIsMac] = useState(false)
147122
useEffect(() => {
148123
// checkIsMac uses window so we delay the check.
149124
const timeout = window.setTimeout(() => {
150-
setIsMac(checkIsMac());
151-
}, 0);
152-
return () => window.clearTimeout(timeout);
153-
}, []);
154-
const isSidebarTogglable = useContext(SidebarTogglableContext);
155-
const openCloseKeyboardShortcut = `(${isMac ? "⌘" : "^"} + K)`;
156-
const isDisabled = !isDiffAvailable && !isDiffbarOpen;
125+
setIsMac(checkIsMac())
126+
}, 0)
127+
return () => window.clearTimeout(timeout)
128+
}, [])
129+
const isSidebarTogglable = useContext(SidebarTogglableContext)
130+
const openCloseKeyboardShortcut = `(${isMac ? "⌘" : "^"} + K)`
131+
const isDisabled = !isDiffAvailable && !isDiffbarOpen
157132
const tooltip = isDisabled
158133
? "Changes cannot be displayed"
159134
: isDiffbarOpen
160135
? "Hide changes"
161-
: "Show changes";
136+
: "Show changes"
162137
return (
163138
<Box sx={{ display: isSidebarTogglable ? "block" : "none" }}>
164139

@@ -180,5 +155,5 @@ const ToggleDiffButton = ({
180155
</span>
181156
</Tooltip>
182157
</Box>
183-
);
184-
};
158+
)
159+
}

0 commit comments

Comments
 (0)