Skip to content

Commit 3f556f9

Browse files
committed
game sidebar width is independent of lobby
1 parent 2892b33 commit 3f556f9

File tree

6 files changed

+38
-17
lines changed

6 files changed

+38
-17
lines changed

src/main/client/src/Router.jsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import {
1414
import {
1515
Toaster,
1616
} from "react-hot-toast"
17+
import {
18+
twJoin,
19+
} from "tailwind-merge"
1720
import {
1821
useAuthStore,
1922
} from "./store.js"
@@ -30,18 +33,17 @@ import {
3033
base,
3134
StompContext,
3235
} from "./util.js"
36+
import {
37+
useLayoutStore,
38+
} from "./layout.js"
3339

3440
export const Router = createBrowserRouter(
3541
createRoutesFromElements(
3642
<>
3743
<Route
3844
element={<WithConnection />}>
3945
<Route
40-
element={
41-
<>
42-
<Outlet />
43-
<Toaster position="top-right" />
44-
</>}>
46+
element={<Frame />}>
4547
<Route
4648
path={base + "/lobby"}
4749
element={<Lobby />} />
@@ -60,6 +62,19 @@ export const Router = createBrowserRouter(
6062
)
6163
)
6264

65+
function Frame() {
66+
let dragging = useLayoutStore(state => state.dragging)
67+
return <>
68+
<div className={twJoin(
69+
"h-full",
70+
dragging && "cursor-col-resize",
71+
)}>
72+
<Outlet />
73+
</div>
74+
<Toaster position="top-right" />
75+
</>
76+
}
77+
6378
function WithConnection() {
6479
let auth = useAuthStore(state => state.auth)
6580
let stompClient = useContext(StompContext)

src/main/client/src/component/SideBar.jsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import {
77
useLayoutStore,
88
} from "../layout.js"
99

10-
export const SideBar = ({children}) => {
10+
export const SideBar = ({page, children}) => {
1111
let vw = useLayoutStore(state => state.vw)
1212
let dragging = useLayoutStore(state => state.dragging)
1313
let setDragging = useLayoutStore(state => state.setDragging)
14-
let sidebarWidth = useLayoutStore(state => state.sidebarWidth)
14+
let sidebarWidth = useLayoutStore(state => state.sidebarWidth[page])
1515
let setSidebarWidth = useLayoutStore(state => state.setSidebarWidth)
1616
let sanitizeSidebarWidth = useLayoutStore(state => state.sanitizeSidebarWidth)
1717
let [ghostWidth, setGhostWidth] = useState(sidebarWidth)
@@ -24,15 +24,16 @@ export const SideBar = ({children}) => {
2424
if (!draggingRef.current) {
2525
return
2626
}
27-
setGhostWidth(Math.trunc(vw - e.clientX))
27+
let width = sanitizeSidebarWidth(vw - e.clientX)
28+
setGhostWidth(width)
2829
}
2930
let mouseup = () => {
3031
if (!draggingRef.current) {
3132
return
3233
}
3334
let width = sanitizeSidebarWidth(ghostWidthRef.current)
34-
setSidebarWidth(width)
35-
setGhostWidth(width)
35+
setSidebarWidth(page, Math.trunc(width))
36+
setGhostWidth(Math.trunc(width))
3637
setDragging(false)
3738
}
3839
window.document.addEventListener("mousemove", mousemove)
@@ -41,7 +42,7 @@ export const SideBar = ({children}) => {
4142
window.document.removeEventListener("mousemove", mousemove)
4243
window.document.removeEventListener("mouseup", mouseup)
4344
}
44-
}, [vw, draggingRef, setDragging, setSidebarWidth, sanitizeSidebarWidth])
45+
}, [vw, page, draggingRef, setDragging, setSidebarWidth, sanitizeSidebarWidth])
4546
return (
4647
<div
4748
style={{width: sidebarWidth + "px"}}

src/main/client/src/feature/game/Game.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const Game = () => {
6262
let [forbidden_x, forbidden_y] = useGameStore(state => state.forbidden)
6363
let canvasRef = useRef()
6464
let countingGroup = !gameHasEnded() && counting ? getCountingGroup(board, cursor_x, cursor_y) : undefined
65-
let sidebarWidth = useLayoutStore(state => state.sidebarWidth)
65+
let sidebarWidth = useLayoutStore(state => state.sidebarWidth.game)
6666
let vw = useLayoutStore(state => state.vw)
6767
let dragging = useLayoutStore(state => state.dragging)
6868

src/main/client/src/feature/game/GamePanel.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242

4343
export const GamePanel = () => {
4444
return (
45-
<SideBar>
45+
<SideBar page="game">
4646
<div className="pr-3 pt-4 pl-2 h-full flex flex-col gap-y-1">
4747
<Panel />
4848
</div>

src/main/client/src/feature/lobby/LobbyPanel.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717

1818
export const LobbyPanel = () => {
1919
return (
20-
<SideBar>
20+
<SideBar page="lobby">
2121
<div className="pt-2">
2222
<Panel />
2323
</div>

src/main/client/src/layout.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export const useLayoutStore = create(
1414
zoom: 0,
1515
dragging: false,
1616
vw: Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0),
17-
sidebarWidth: 24 * getPixelRem(),
17+
sidebarWidth: {
18+
"game": 24 * getPixelRem(),
19+
"lobby": 24 * getPixelRem(),
20+
},
1821
setZoom: (zoom) => {
1922
set(produce(state => {
2023
if (!zoom) {
@@ -34,9 +37,11 @@ export const useLayoutStore = create(
3437
width = Math.max(200, width)
3538
return Math.min(Math.abs(get().vw - vh), width)
3639
},
37-
setSidebarWidth: (width) => {
40+
setSidebarWidth: (page, width) => {
3841
set(produce(state => {
39-
state.sidebarWidth = width
42+
let newSidebarWidth = {...get().sidebarWidth}
43+
newSidebarWidth[page] = width
44+
state.sidebarWidth = newSidebarWidth
4045
if (!get().zoom) {
4146
state.zoom = 0.000244140625 // force re-render
4247
} else {

0 commit comments

Comments
 (0)