2024-11-29 14:05:10 +01:00
|
|
|
import { t } from "@lingui/core/macro"
|
2024-06-13 21:54:14 +02:00
|
|
|
import { showNotification } from "@mantine/notifications"
|
2026-05-03 19:14:50 +02:00
|
|
|
import { createSlice, type PayloadAction } from "@reduxjs/toolkit"
|
2025-06-27 16:29:31 +02:00
|
|
|
import type { LocalSettings, Settings, UserModel, ViewMode } from "@/app/types"
|
2026-05-03 19:14:50 +02:00
|
|
|
import { changeSettings, reloadProfile, reloadSettings, reloadTags } from "./thunks"
|
2024-06-13 21:54:14 +02:00
|
|
|
|
|
|
|
|
interface UserState {
|
|
|
|
|
settings?: Settings
|
2024-09-10 18:11:16 +02:00
|
|
|
localSettings: LocalSettings
|
2024-06-13 21:54:14 +02:00
|
|
|
profile?: UserModel
|
|
|
|
|
tags?: string[]
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 18:11:16 +02:00
|
|
|
export const initialLocalSettings: LocalSettings = {
|
|
|
|
|
viewMode: "detailed",
|
|
|
|
|
sidebarWidth: 360,
|
|
|
|
|
announcementHash: "no-hash",
|
2025-05-03 22:17:55 +02:00
|
|
|
fontSizePercentage: 100,
|
2024-09-10 18:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-10 16:22:23 +02:00
|
|
|
const initialState: UserState = {
|
2024-09-10 18:11:16 +02:00
|
|
|
localSettings: initialLocalSettings,
|
2024-09-10 16:22:23 +02:00
|
|
|
}
|
2024-06-13 21:54:14 +02:00
|
|
|
|
|
|
|
|
export const userSlice = createSlice({
|
|
|
|
|
name: "user",
|
|
|
|
|
initialState,
|
2024-09-10 16:22:23 +02:00
|
|
|
reducers: {
|
|
|
|
|
setViewMode: (state, action: PayloadAction<ViewMode>) => {
|
|
|
|
|
state.localSettings.viewMode = action.payload
|
|
|
|
|
},
|
2025-05-03 22:17:55 +02:00
|
|
|
setFontSizePercentage: (state, action: PayloadAction<number>) => {
|
|
|
|
|
state.localSettings.fontSizePercentage = action.payload
|
|
|
|
|
},
|
2024-09-10 18:11:16 +02:00
|
|
|
setSidebarWidth: (state, action: PayloadAction<number>) => {
|
|
|
|
|
state.localSettings.sidebarWidth = action.payload
|
|
|
|
|
},
|
|
|
|
|
setAnnouncementHash: (state, action: PayloadAction<string>) => {
|
|
|
|
|
state.localSettings.announcementHash = action.payload
|
|
|
|
|
},
|
2024-09-10 16:22:23 +02:00
|
|
|
},
|
2024-06-13 21:54:14 +02:00
|
|
|
extraReducers: builder => {
|
|
|
|
|
builder.addCase(reloadSettings.fulfilled, (state, action) => {
|
|
|
|
|
state.settings = action.payload
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(reloadProfile.fulfilled, (state, action) => {
|
|
|
|
|
state.profile = action.payload
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(reloadTags.fulfilled, (state, action) => {
|
|
|
|
|
state.tags = action.payload
|
|
|
|
|
})
|
2026-05-03 19:14:50 +02:00
|
|
|
builder.addCase(changeSettings.pending, (state, action) => {
|
2024-06-13 21:54:14 +02:00
|
|
|
if (!state.settings) return
|
2026-05-03 19:14:50 +02:00
|
|
|
state.settings = { ...state.settings, ...action.meta.arg }
|
2024-06-13 21:54:14 +02:00
|
|
|
})
|
2026-05-03 19:14:50 +02:00
|
|
|
builder.addCase(changeSettings.fulfilled, () => {
|
|
|
|
|
showNotification({
|
|
|
|
|
message: t`Settings saved.`,
|
|
|
|
|
color: "green",
|
|
|
|
|
})
|
2024-06-13 21:54:14 +02:00
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
})
|
2024-09-10 16:22:23 +02:00
|
|
|
|
2025-05-03 22:17:55 +02:00
|
|
|
export const { setViewMode, setSidebarWidth, setAnnouncementHash, setFontSizePercentage } = userSlice.actions
|