2024-06-13 21:54:14 +02:00
|
|
|
import { t } from "@lingui/macro"
|
|
|
|
|
import { showNotification } from "@mantine/notifications"
|
2024-09-10 16:22:23 +02:00
|
|
|
import { type PayloadAction, createSlice, isAnyOf } from "@reduxjs/toolkit"
|
|
|
|
|
import type { Settings, UserModel, ViewMode } from "app/types"
|
2024-06-13 21:54:14 +02:00
|
|
|
import {
|
|
|
|
|
changeCustomContextMenu,
|
2024-09-10 16:22:23 +02:00
|
|
|
changeEntriesToKeepOnTopWhenScrolling,
|
2024-06-13 21:54:14 +02:00
|
|
|
changeExternalLinkIconDisplayMode,
|
|
|
|
|
changeLanguage,
|
|
|
|
|
changeMarkAllAsReadConfirmation,
|
|
|
|
|
changeMobileFooter,
|
|
|
|
|
changeReadingMode,
|
|
|
|
|
changeReadingOrder,
|
|
|
|
|
changeScrollMarks,
|
|
|
|
|
changeScrollMode,
|
|
|
|
|
changeScrollSpeed,
|
|
|
|
|
changeSharingSetting,
|
|
|
|
|
changeShowRead,
|
|
|
|
|
changeStarIconDisplayMode,
|
2024-08-25 18:05:34 +02:00
|
|
|
changeUnreadCountFavicon,
|
|
|
|
|
changeUnreadCountTitle,
|
2024-06-13 21:54:14 +02:00
|
|
|
reloadProfile,
|
|
|
|
|
reloadSettings,
|
|
|
|
|
reloadTags,
|
|
|
|
|
} from "./thunks"
|
|
|
|
|
|
|
|
|
|
interface UserState {
|
|
|
|
|
settings?: Settings
|
2024-09-10 16:22:23 +02:00
|
|
|
localSettings: {
|
|
|
|
|
viewMode: ViewMode
|
|
|
|
|
}
|
2024-06-13 21:54:14 +02:00
|
|
|
profile?: UserModel
|
|
|
|
|
tags?: string[]
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 16:22:23 +02:00
|
|
|
const initialState: UserState = {
|
|
|
|
|
localSettings: {
|
|
|
|
|
viewMode: "detailed",
|
|
|
|
|
},
|
|
|
|
|
}
|
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
|
|
|
|
|
},
|
|
|
|
|
},
|
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
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeReadingMode.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.readingMode = action.meta.arg
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeReadingOrder.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.readingOrder = action.meta.arg
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeLanguage.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.language = action.meta.arg
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeScrollSpeed.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.scrollSpeed = action.meta.arg ? 400 : 0
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeShowRead.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.showRead = action.meta.arg
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeScrollMarks.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.scrollMarks = action.meta.arg
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeScrollMode.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.scrollMode = action.meta.arg
|
|
|
|
|
})
|
2024-09-10 16:22:23 +02:00
|
|
|
builder.addCase(changeEntriesToKeepOnTopWhenScrolling.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.entriesToKeepOnTopWhenScrolling = action.meta.arg
|
|
|
|
|
})
|
2024-06-13 21:54:14 +02:00
|
|
|
builder.addCase(changeStarIconDisplayMode.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.starIconDisplayMode = action.meta.arg
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeExternalLinkIconDisplayMode.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.externalLinkIconDisplayMode = action.meta.arg
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeMarkAllAsReadConfirmation.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.markAllAsReadConfirmation = action.meta.arg
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeCustomContextMenu.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.customContextMenu = action.meta.arg
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeMobileFooter.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.mobileFooter = action.meta.arg
|
|
|
|
|
})
|
2024-08-25 18:05:34 +02:00
|
|
|
builder.addCase(changeUnreadCountTitle.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.unreadCountTitle = action.meta.arg
|
|
|
|
|
})
|
|
|
|
|
builder.addCase(changeUnreadCountFavicon.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.unreadCountFavicon = action.meta.arg
|
|
|
|
|
})
|
2024-06-13 21:54:14 +02:00
|
|
|
builder.addCase(changeSharingSetting.pending, (state, action) => {
|
|
|
|
|
if (!state.settings) return
|
|
|
|
|
state.settings.sharingSettings[action.meta.arg.site] = action.meta.arg.value
|
|
|
|
|
})
|
|
|
|
|
builder.addMatcher(
|
|
|
|
|
isAnyOf(
|
|
|
|
|
changeLanguage.fulfilled,
|
|
|
|
|
changeScrollSpeed.fulfilled,
|
|
|
|
|
changeShowRead.fulfilled,
|
|
|
|
|
changeScrollMarks.fulfilled,
|
|
|
|
|
changeScrollMode.fulfilled,
|
2024-09-10 16:22:23 +02:00
|
|
|
changeEntriesToKeepOnTopWhenScrolling.fulfilled,
|
2024-06-13 21:54:14 +02:00
|
|
|
changeStarIconDisplayMode.fulfilled,
|
|
|
|
|
changeExternalLinkIconDisplayMode.fulfilled,
|
|
|
|
|
changeMarkAllAsReadConfirmation.fulfilled,
|
|
|
|
|
changeCustomContextMenu.fulfilled,
|
|
|
|
|
changeMobileFooter.fulfilled,
|
2024-08-25 18:05:34 +02:00
|
|
|
changeUnreadCountTitle.fulfilled,
|
|
|
|
|
changeUnreadCountFavicon.fulfilled,
|
2024-06-13 21:54:14 +02:00
|
|
|
changeSharingSetting.fulfilled
|
|
|
|
|
),
|
|
|
|
|
() => {
|
|
|
|
|
showNotification({
|
|
|
|
|
message: t`Settings saved.`,
|
|
|
|
|
color: "green",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
})
|
2024-09-10 16:22:23 +02:00
|
|
|
|
|
|
|
|
export const { setViewMode } = userSlice.actions
|