- Enhanced tests to ensure consistent messageId generation for legacy inbox rows lacking a messageId. - Updated test descriptions for better clarity regarding the new messageId handling. - Adjusted test expectations to align with the updated behavior of relaying legacy inbox rows with generated messageIds.
70 lines
2 KiB
TypeScript
70 lines
2 KiB
TypeScript
import { computed, watch, onUnmounted } from "vue";
|
|
import { useThemeStore } from "~/stores/theme";
|
|
|
|
export const useBrowserTheme = () => {
|
|
const themeStore = useThemeStore();
|
|
const { $vuetifyTheme } = useNuxtApp();
|
|
const vuetifyTheme = $vuetifyTheme as {
|
|
global: { name: import("vue").Ref<string>; current: import("vue").Ref<any> };
|
|
change: (name: string) => void;
|
|
} | null;
|
|
let mediaQueryHandler: ((event: MediaQueryListEvent) => void) | null = null;
|
|
let mediaQuery: MediaQueryList | null = null;
|
|
|
|
const applyVuetifyTheme = (name: "light" | "dark") => {
|
|
if (!vuetifyTheme) return;
|
|
if (typeof vuetifyTheme.change === "function") {
|
|
vuetifyTheme.change(name);
|
|
} else {
|
|
vuetifyTheme.global.name.value = name;
|
|
}
|
|
};
|
|
|
|
const applyTheme = (name: "light" | "dark") => {
|
|
themeStore.setTheme(name, true);
|
|
applyVuetifyTheme(name);
|
|
};
|
|
|
|
const initTheme = () => {
|
|
if (!process.client) return;
|
|
const initialTheme = themeStore.getInitialTheme();
|
|
themeStore.setTheme(initialTheme, false);
|
|
applyVuetifyTheme(initialTheme);
|
|
|
|
if (process.client && !themeStore.userSelected) {
|
|
mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
mediaQueryHandler = (event: MediaQueryListEvent) => {
|
|
if (!themeStore.userSelected) {
|
|
const newTheme = event.matches ? "dark" : "light";
|
|
themeStore.setTheme(newTheme, false);
|
|
applyVuetifyTheme(newTheme);
|
|
}
|
|
};
|
|
mediaQuery.addEventListener("change", mediaQueryHandler);
|
|
}
|
|
};
|
|
|
|
const toggleTheme = () => {
|
|
applyTheme(themeStore.current === "dark" ? "light" : "dark");
|
|
};
|
|
|
|
onUnmounted(() => {
|
|
if (mediaQuery && mediaQueryHandler) {
|
|
mediaQuery.removeEventListener("change", mediaQueryHandler);
|
|
}
|
|
});
|
|
|
|
watch(
|
|
() => themeStore.current,
|
|
(value) => {
|
|
applyVuetifyTheme(value as "light" | "dark");
|
|
}
|
|
);
|
|
|
|
return {
|
|
currentTheme: computed(() => themeStore.current),
|
|
isDark: computed(() => themeStore.current === "dark"),
|
|
initTheme,
|
|
toggleTheme
|
|
};
|
|
};
|