mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00

* chore(web): toggle classList instead of add and remove * fix(web): set theme before the app loads
142 lines
3.2 KiB
TypeScript
142 lines
3.2 KiB
TypeScript
/*
|
|
* Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
*/
|
|
|
|
import { newRidgeState } from "react-ridge-state";
|
|
|
|
export const InitializeGlobalContext = () => {
|
|
const auth_ctx = localStorage.getItem("auth");
|
|
if (auth_ctx)
|
|
AuthContext.set(JSON.parse(auth_ctx));
|
|
|
|
const settings_ctx = localStorage.getItem("settings");
|
|
if (settings_ctx) {
|
|
SettingsContext.set(JSON.parse(settings_ctx));
|
|
} else {
|
|
// Only check for light theme, otherwise dark theme is the default
|
|
SettingsContext.set((state) => ({
|
|
...state,
|
|
darkTheme: !(
|
|
window.matchMedia !== undefined &&
|
|
window.matchMedia("(prefers-color-scheme: light)").matches
|
|
)
|
|
}));
|
|
}
|
|
const filterList_ctx = localStorage.getItem("filterList");
|
|
if (filterList_ctx) {
|
|
FilterListContext.set(JSON.parse(filterList_ctx));
|
|
}
|
|
};
|
|
|
|
interface AuthInfo {
|
|
username: string;
|
|
isLoggedIn: boolean;
|
|
}
|
|
|
|
export const AuthContext = newRidgeState<AuthInfo>(
|
|
{
|
|
username: "",
|
|
isLoggedIn: false
|
|
},
|
|
{
|
|
onSet: (new_state) => {
|
|
try {
|
|
localStorage.setItem("auth", JSON.stringify(new_state));
|
|
} catch (e) {
|
|
console.log("An error occurred while trying to modify the local auth context state.");
|
|
console.log("Error:", e);
|
|
}
|
|
}
|
|
}
|
|
);
|
|
|
|
interface SettingsType {
|
|
debug: boolean;
|
|
checkForUpdates: boolean;
|
|
darkTheme: boolean;
|
|
scrollOnNewLog: boolean;
|
|
indentLogLines: boolean;
|
|
hideWrappedText: boolean;
|
|
}
|
|
|
|
export const SettingsContext = newRidgeState<SettingsType>(
|
|
{
|
|
debug: false,
|
|
checkForUpdates: true,
|
|
darkTheme: true,
|
|
scrollOnNewLog: false,
|
|
indentLogLines: false,
|
|
hideWrappedText: false
|
|
},
|
|
{
|
|
onSet: (new_state) => {
|
|
try {
|
|
document.documentElement.classList.toggle("dark", new_state.darkTheme);
|
|
localStorage.setItem("settings", JSON.stringify(new_state));
|
|
} catch (e) {
|
|
console.log("An error occurred while trying to modify the local settings context state.");
|
|
console.log("Error:", e);
|
|
}
|
|
}
|
|
}
|
|
);
|
|
|
|
export type FilterListState = {
|
|
indexerFilter: string[],
|
|
sortOrder: string;
|
|
status: string;
|
|
};
|
|
|
|
export const FilterListContext = newRidgeState<FilterListState>(
|
|
{
|
|
indexerFilter: [],
|
|
sortOrder: "",
|
|
status: ""
|
|
},
|
|
{
|
|
onSet: (new_state) => {
|
|
try {
|
|
localStorage.setItem("filterList", JSON.stringify(new_state));
|
|
} catch (e) {
|
|
console.log("An error occurred while trying to modify the local filter list context state.");
|
|
console.log("Error:", e);
|
|
}
|
|
}
|
|
}
|
|
);
|
|
|
|
export type IrcNetworkState = {
|
|
id: number;
|
|
name: string;
|
|
status: string;
|
|
};
|
|
|
|
export type IrcBufferType = "NICK" | "CHANNEL" | "SERVER";
|
|
|
|
export type IrcBufferState = {
|
|
id: number;
|
|
name: string;
|
|
type: IrcBufferType;
|
|
messages: string[];
|
|
};
|
|
|
|
export type IrcState = {
|
|
networks: Map<string, IrcNetworkState>;
|
|
buffers: Map<string, IrcBufferState>
|
|
};
|
|
export const IrcContext = newRidgeState<IrcState>(
|
|
{
|
|
networks: new Map(),
|
|
buffers: new Map()
|
|
},
|
|
{
|
|
onSet: (new_state) => {
|
|
try {
|
|
console.log("set irc state", new_state);
|
|
} catch (e) {
|
|
console.log("Error:", e);
|
|
}
|
|
}
|
|
}
|
|
);
|