mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
|
import { action, observable, makeObservable } from "mobx";
|
||
|
// root store
|
||
|
import { RootStore } from "@/store/root-store";
|
||
|
|
||
|
type TTheme = "dark" | "light";
|
||
|
export interface IThemeStore {
|
||
|
// observables
|
||
|
theme: string | undefined;
|
||
|
isSidebarCollapsed: boolean | undefined;
|
||
|
// actions
|
||
|
toggleSidebar: (collapsed: boolean) => void;
|
||
|
setTheme: (currentTheme: TTheme) => void;
|
||
|
}
|
||
|
|
||
|
export class ThemeStore implements IThemeStore {
|
||
|
// observables
|
||
|
isSidebarCollapsed: boolean | undefined = undefined;
|
||
|
theme: string | undefined = undefined;
|
||
|
|
||
|
constructor(private store: RootStore) {
|
||
|
makeObservable(this, {
|
||
|
// observables
|
||
|
isSidebarCollapsed: observable.ref,
|
||
|
theme: observable.ref,
|
||
|
// action
|
||
|
toggleSidebar: action,
|
||
|
setTheme: action,
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Toggle the sidebar collapsed state
|
||
|
* @param isCollapsed
|
||
|
*/
|
||
|
toggleSidebar = (isCollapsed: boolean) => {
|
||
|
if (isCollapsed === undefined) this.isSidebarCollapsed = !this.isSidebarCollapsed;
|
||
|
else this.isSidebarCollapsed = isCollapsed;
|
||
|
localStorage.setItem("god_mode_sidebar_collapsed", isCollapsed.toString());
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Sets the user theme and applies it to the platform
|
||
|
* @param currentTheme
|
||
|
*/
|
||
|
setTheme = async (currentTheme: TTheme) => {
|
||
|
try {
|
||
|
localStorage.setItem("theme", currentTheme);
|
||
|
this.theme = currentTheme;
|
||
|
} catch (error) {
|
||
|
console.error("setting user theme error", error);
|
||
|
}
|
||
|
};
|
||
|
}
|