|
1 | | -"use client" |
| 1 | +"use client"; |
2 | 2 |
|
3 | | -import { useState, useEffect, useContext } from "react" |
4 | | -import { useSessionStorage } from "usehooks-ts" |
5 | | -import { Box, IconButton, Stack, Tooltip, Collapse } from "@mui/material" |
6 | | -import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" |
7 | | -import { faBars, faChevronLeft, faChevronRight, faCodeCompare } 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" |
| 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 | + faBars, |
| 16 | + faChevronLeft, |
| 17 | + faChevronRight, |
| 18 | + faCodeCompare, |
| 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"; |
12 | 24 |
|
13 | 25 | const SecondarySplitHeader = ({ |
14 | 26 | mobileToolbar, |
15 | | - children |
| 27 | + children, |
16 | 28 | }: { |
17 | | - mobileToolbar?: React.ReactNode |
18 | | - children?: React.ReactNode |
| 29 | + mobileToolbar?: React.ReactNode; |
| 30 | + children?: React.ReactNode; |
19 | 31 | }) => { |
20 | | - const [isSidebarOpen, setSidebarOpen] = useSidebarOpen() |
21 | | - const [isDiffbarOpen, setDiffbarOpen] = useDiffbarOpen() |
22 | | - const [isMobileToolbarVisible, setMobileToolbarVisible] = useSessionStorage("isMobileToolbarVisible", true) |
| 32 | + const [isSidebarOpen, setSidebarOpen] = useSidebarOpen(); |
| 33 | + const [isDiffbarOpen, setDiffbarOpen] = useDiffbarOpen(); |
| 34 | + const [isMobileToolbarVisible, setMobileToolbarVisible] = useSessionStorage( |
| 35 | + "isMobileToolbarVisible", |
| 36 | + true |
| 37 | + ); |
23 | 38 | return ( |
24 | 39 | <Box> |
25 | | - <Box sx={{ |
26 | | - display: "flex", |
27 | | - alignItems: "center", |
28 | | - paddingLeft: 2, |
29 | | - paddingRight: 2, |
30 | | - height: 64, |
31 | | - margin: "auto" |
32 | | - }}> |
| 40 | + <Box |
| 41 | + sx={{ |
| 42 | + display: "flex", |
| 43 | + alignItems: "center", |
| 44 | + paddingLeft: 2, |
| 45 | + paddingRight: 2, |
| 46 | + height: 64, |
| 47 | + margin: "auto", |
| 48 | + }} |
| 49 | + > |
33 | 50 | <ToggleSidebarButton |
34 | 51 | isSidebarOpen={isSidebarOpen} |
35 | 52 | onClick={setSidebarOpen} |
36 | 53 | /> |
37 | | - <Box sx={{ display: "flex", flexGrow: 1, justifyContent: "end" }}> |
| 54 | + <Box sx={{ display: "flex", flexGrow: 1, justifyContent: "end" }}> |
38 | 55 | <Stack direction="row" alignItems="center"> |
39 | 56 | {children} |
40 | | - <ToggleDiffButton |
41 | | - isDiffbarOpen={isDiffbarOpen} |
42 | | - onClick={setDiffbarOpen} |
| 57 | + <Divider |
| 58 | + orientation="vertical" |
| 59 | + flexItem |
| 60 | + sx={{ marginLeft: 0.5, marginRight: 0.5 }} |
43 | 61 | /> |
44 | | - {mobileToolbar && |
| 62 | + {mobileToolbar && ( |
45 | 63 | <ToggleMobileToolbarButton |
46 | 64 | direction={isMobileToolbarVisible ? "up" : "down"} |
47 | | - onToggle={() => setMobileToolbarVisible(!isMobileToolbarVisible) } |
| 65 | + onToggle={() => |
| 66 | + setMobileToolbarVisible(!isMobileToolbarVisible) |
| 67 | + } |
48 | 68 | /> |
49 | | - } |
| 69 | + )} |
50 | 70 | </Stack> |
51 | 71 | </Box> |
| 72 | + <ToggleDiffButton |
| 73 | + isDiffbarOpen={isDiffbarOpen} |
| 74 | + onClick={setDiffbarOpen} |
| 75 | + /> |
52 | 76 | </Box> |
53 | | - {mobileToolbar && |
| 77 | + {mobileToolbar && ( |
54 | 78 | <Collapse in={isMobileToolbarVisible}> |
55 | | - <Box sx={{ |
56 | | - padding: 2, |
57 | | - paddingTop: 0, |
58 | | - display: { sm: "block", md: "none" } |
59 | | - }}> |
| 79 | + <Box |
| 80 | + sx={{ |
| 81 | + padding: 2, |
| 82 | + paddingTop: 0, |
| 83 | + display: { sm: "block", md: "none" }, |
| 84 | + }} |
| 85 | + > |
60 | 86 | {mobileToolbar} |
61 | 87 | </Box> |
62 | 88 | </Collapse> |
63 | | - } |
| 89 | + )} |
64 | 90 | </Box> |
65 | | - ) |
66 | | -} |
| 91 | + ); |
| 92 | +}; |
67 | 93 |
|
68 | | -export default SecondarySplitHeader |
| 94 | +export default SecondarySplitHeader; |
69 | 95 |
|
70 | 96 | const ToggleSidebarButton = ({ |
71 | 97 | isSidebarOpen, |
72 | | - onClick |
| 98 | + onClick, |
73 | 99 | }: { |
74 | | - isSidebarOpen: boolean, |
75 | | - onClick: (isSidebarOpen: boolean) => void |
| 100 | + isSidebarOpen: boolean; |
| 101 | + onClick: (isSidebarOpen: boolean) => void; |
76 | 102 | }) => { |
77 | | - const [isMac, setIsMac] = useState(false) |
| 103 | + const [isMac, setIsMac] = useState(false); |
78 | 104 | useEffect(() => { |
79 | 105 | // checkIsMac uses window so we delay the check. |
80 | | - setIsMac(checkIsMac()) |
81 | | - }, [isMac, setIsMac]) |
82 | | - const isSidebarTogglable = useContext(SidebarTogglableContext) |
83 | | - const openCloseKeyboardShortcut = `(${isMac ? "⌘" : "^"} + .)` |
84 | | - const tooltip = isSidebarOpen ? "Show Projects" : "Hide Projects" |
| 106 | + setIsMac(checkIsMac()); |
| 107 | + }, [isMac, setIsMac]); |
| 108 | + const isSidebarTogglable = useContext(SidebarTogglableContext); |
| 109 | + const openCloseKeyboardShortcut = `(${isMac ? "⌘" : "^"} + .)`; |
| 110 | + const tooltip = isSidebarOpen ? "Show Projects" : "Hide Projects"; |
85 | 111 | return ( |
86 | 112 | <Box sx={{ display: isSidebarTogglable ? "block" : "none" }}> |
87 | | - <Tooltip title={`${tooltip} ${openCloseKeyboardShortcut}`}> |
88 | | - <IconButton |
89 | | - size="medium" |
90 | | - color="primary" |
91 | | - onClick={() => onClick(!isSidebarOpen)} |
92 | | - edge="start" |
93 | | - > |
94 | | - <FontAwesomeIcon |
95 | | - icon={isSidebarOpen ? faChevronLeft : faBars} |
96 | | - size="sm" |
97 | | - style={{ aspectRatio: 1, padding: 2 }} |
98 | | - /> |
99 | | - </IconButton> |
100 | | - </Tooltip> |
| 113 | + <Tooltip title={`${tooltip} ${openCloseKeyboardShortcut}`}> |
| 114 | + <IconButton |
| 115 | + size="medium" |
| 116 | + color="primary" |
| 117 | + onClick={() => onClick(!isSidebarOpen)} |
| 118 | + edge="start" |
| 119 | + > |
| 120 | + <FontAwesomeIcon |
| 121 | + icon={isSidebarOpen ? faChevronLeft : faBars} |
| 122 | + size="sm" |
| 123 | + style={{ aspectRatio: 1, padding: 2 }} |
| 124 | + /> |
| 125 | + </IconButton> |
| 126 | + </Tooltip> |
101 | 127 | </Box> |
102 | | - ) |
103 | | -} |
| 128 | + ); |
| 129 | +}; |
104 | 130 |
|
105 | 131 | const ToggleDiffButton = ({ |
106 | 132 | isDiffbarOpen, |
107 | | - onClick |
| 133 | + onClick, |
108 | 134 | }: { |
109 | | - isDiffbarOpen: boolean, |
110 | | - onClick: (isDiffbarOpen: boolean) => void |
| 135 | + isDiffbarOpen: boolean; |
| 136 | + onClick: (isDiffbarOpen: boolean) => void; |
111 | 137 | }) => { |
112 | | - const [isMac, setIsMac] = useState(false) |
| 138 | + const [isMac, setIsMac] = useState(false); |
113 | 139 | useEffect(() => { |
114 | 140 | // checkIsMac uses window so we delay the check. |
115 | | - setIsMac(checkIsMac()) |
116 | | - }, [isMac, setIsMac]) |
117 | | - const isSidebarTogglable = useContext(SidebarTogglableContext) |
118 | | - const openCloseKeyboardShortcut = `(${isMac ? "⌘" : "^"} + K)` |
119 | | - const tooltip = isDiffbarOpen ? "Hide Diff" : "Show Diff" |
| 141 | + setIsMac(checkIsMac()); |
| 142 | + }, [isMac, setIsMac]); |
| 143 | + const isSidebarTogglable = useContext(SidebarTogglableContext); |
| 144 | + const openCloseKeyboardShortcut = `(${isMac ? "⌘" : "^"} + K)`; |
| 145 | + const tooltip = isDiffbarOpen ? "Hide Diff" : "Show Diff"; |
120 | 146 | return ( |
121 | 147 | <Box sx={{ display: isSidebarTogglable ? "block" : "none" }}> |
122 | | - <Tooltip title={`${tooltip} ${openCloseKeyboardShortcut}`}> |
123 | | - <IconButton |
124 | | - size="medium" |
125 | | - color="primary" |
126 | | - onClick={() => onClick(!isDiffbarOpen)} |
127 | | - edge="end" |
128 | | - > |
129 | | - <FontAwesomeIcon |
130 | | - icon={isDiffbarOpen ? faChevronRight : faCodeCompare} |
131 | | - size="sm" |
132 | | - style={{ aspectRatio: 1, padding: 2 }} |
133 | | - /> |
134 | | - </IconButton> |
135 | | - </Tooltip> |
| 148 | + |
| 149 | + <Tooltip title={`${tooltip} ${openCloseKeyboardShortcut}`}> |
| 150 | + <IconButton |
| 151 | + size="medium" |
| 152 | + color="primary" |
| 153 | + onClick={() => onClick(!isDiffbarOpen)} |
| 154 | + edge="end" |
| 155 | + > |
| 156 | + <FontAwesomeIcon |
| 157 | + icon={isDiffbarOpen ? faChevronRight : faCodeCompare} |
| 158 | + size="sm" |
| 159 | + style={{ aspectRatio: 1, padding: 2 }} |
| 160 | + /> |
| 161 | + </IconButton> |
| 162 | + </Tooltip> |
136 | 163 | </Box> |
137 | | - ) |
138 | | -} |
| 164 | + ); |
| 165 | +}; |
0 commit comments