Skip to content

Commit

Permalink
save last state (for navbar collapse) in cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Aug 23, 2024
1 parent 37ae505 commit 09beb3f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
41 changes: 39 additions & 2 deletions ui2/src/slices/navBar.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import {createSlice} from "@reduxjs/toolkit"
import Cookies from "js-cookie"
import {RootState} from "@/app/types"
import type {NavBarCollapsedCookieStrings} from "@/types"

const COLLAPSED_WIDTH = 55
const FULL_WIDTH = 160
const NAVBAR_COLLAPSED_COOKIE = "navbar_collapsed"
const NAVBAR_WIDTH_COOKIE = "navbar_width"

interface NavBar {
collapsed: boolean
width: number
}

const initialState: NavBar = {
collapsed: false,
width: FULL_WIDTH
collapsed: initial_collapse_value(),
width: initial_width_value()
}

const navBarSlice = createSlice({
Expand All @@ -22,14 +26,47 @@ const navBarSlice = createSlice({
if (state.collapsed) {
state.collapsed = false
state.width = FULL_WIDTH
Cookies.set(NAVBAR_COLLAPSED_COOKIE, "false")
Cookies.set(NAVBAR_WIDTH_COOKIE, `${FULL_WIDTH}`)
} else {
state.collapsed = true
state.width = COLLAPSED_WIDTH
Cookies.set(NAVBAR_COLLAPSED_COOKIE, "true")
Cookies.set(NAVBAR_WIDTH_COOKIE, `${COLLAPSED_WIDTH}`)
}
}
}
})

/* Load initial collapse state value from cookie */
function initial_collapse_value(): boolean {
const collapsed = Cookies.get(
NAVBAR_COLLAPSED_COOKIE
) as NavBarCollapsedCookieStrings

if (collapsed == "true") {
return true
}

return false
}

/* Load initial width value from cookie */
function initial_width_value(): number {
const width = Cookies.get(NAVBAR_WIDTH_COOKIE)

if (width) {
const ret = parseInt(width)
if (ret > 0) {
return ret
} else {
return FULL_WIDTH
}
}

return FULL_WIDTH
}

export default navBarSlice.reducer

export const {toggleNavBar} = navBarSlice.actions
Expand Down
2 changes: 2 additions & 0 deletions ui2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,5 @@ export type ThumbnailPageDroppedArgs = {
target: PageType
position: DroppedThumbnailPosition
}

export type NavBarCollapsedCookieStrings = "true" | "false"

0 comments on commit 09beb3f

Please sign in to comment.