Skip to content

Commit

Permalink
remember last state open/close of thumbnails panel
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Aug 24, 2024
1 parent 09beb3f commit 51dd903
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
54 changes: 47 additions & 7 deletions ui2/src/slices/dualPanel/dualPanel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Cookies from "js-cookie"
import {
createSlice,
PayloadAction,
Expand Down Expand Up @@ -41,7 +42,8 @@ import type {
SearchResultNode,
PaginatedSearchResult,
DroppedThumbnailPosition,
PageType
PageType,
BooleanString
} from "@/types"
import {
DualPanelState,
Expand All @@ -59,6 +61,10 @@ import {
ZOOM_FACTOR_STEP
} from "@/cconstants"

const MAIN_THUMBNAILS_PANEL_OPENED_COOKIE = "main_thumbnails_panel_opened"
const SECONDARY_THUMBNAILS_PANEL_OPENED_COOKIE =
"secondary_thumbnails_panel_opened"

const initialState: DualPanelState = {
mainPanel: {
commander: commanderInitialState(null),
Expand Down Expand Up @@ -296,15 +302,25 @@ const dualPanelSlice = createSlice({

if (mode == "main") {
if (state.mainPanel.viewer) {
state.mainPanel.viewer.thumbnailsPanelOpen =
!state.mainPanel.viewer.thumbnailsPanelOpen
const new_value = !state.mainPanel.viewer.thumbnailsPanelOpen
state.mainPanel.viewer.thumbnailsPanelOpen = new_value
if (new_value) {
Cookies.set(MAIN_THUMBNAILS_PANEL_OPENED_COOKIE, "true")
} else {
Cookies.set(MAIN_THUMBNAILS_PANEL_OPENED_COOKIE, "false")
}
}
}

if (mode == "secondary") {
if (state.secondaryPanel?.viewer) {
state.secondaryPanel.viewer.thumbnailsPanelOpen =
!state.secondaryPanel.viewer.thumbnailsPanelOpen
const new_value = !state.secondaryPanel.viewer.thumbnailsPanelOpen
state.secondaryPanel.viewer.thumbnailsPanelOpen = new_value
if (new_value) {
Cookies.set(SECONDARY_THUMBNAILS_PANEL_OPENED_COOKIE, "true")
} else {
Cookies.set(SECONDARY_THUMBNAILS_PANEL_OPENED_COOKIE, "false")
}
}
}
},
Expand Down Expand Up @@ -509,7 +525,7 @@ const dualPanelSlice = createSlice({
versions: action.payload.versions,
currentVersion: Math.max(...versionNumbers),
currentPage: action.meta.arg.page || 1,
thumbnailsPanelOpen: false,
thumbnailsPanelOpen: mainThumbnailsPanelInitialState(),
zoomFactor: 100,
selectedIds: []
}
Expand All @@ -526,7 +542,7 @@ const dualPanelSlice = createSlice({
versions: action.payload.versions,
currentVersion: Math.max(...versionNumbers),
currentPage: action.meta.arg.page || 1,
thumbnailsPanelOpen: false,
thumbnailsPanelOpen: secondaryThumbnailsPanelInitialState(),
zoomFactor: 100,
selectedIds: []
}
Expand Down Expand Up @@ -966,3 +982,27 @@ export const selectZoomFactor = (state: RootState, mode: PanelMode) => {

return state.dualPanel.secondaryPanel?.viewer?.zoomFactor
}

function mainThumbnailsPanelInitialState(): boolean {
const is_opened = Cookies.get(
MAIN_THUMBNAILS_PANEL_OPENED_COOKIE
) as BooleanString

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

return false
}

function secondaryThumbnailsPanelInitialState(): boolean {
const is_opened = Cookies.get(
SECONDARY_THUMBNAILS_PANEL_OPENED_COOKIE
) as BooleanString

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

return false
}
6 changes: 2 additions & 4 deletions ui2/src/slices/navBar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {createSlice} from "@reduxjs/toolkit"
import Cookies from "js-cookie"
import {RootState} from "@/app/types"
import type {NavBarCollapsedCookieStrings} from "@/types"
import type {BooleanString} from "@/types"

const COLLAPSED_WIDTH = 55
const FULL_WIDTH = 160
Expand Down Expand Up @@ -40,9 +40,7 @@ const navBarSlice = createSlice({

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

if (collapsed == "true") {
return true
Expand Down
2 changes: 1 addition & 1 deletion ui2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,4 @@ export type ThumbnailPageDroppedArgs = {
position: DroppedThumbnailPosition
}

export type NavBarCollapsedCookieStrings = "true" | "false"
export type BooleanString = "true" | "false"

0 comments on commit 51dd903

Please sign in to comment.