mirror of
https://github.com/unraid/api.git
synced 2026-01-06 00:30:22 -06:00
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added comprehensive test coverage for the purchase, replaceRenew, modal, notifications, theme, trial, unraidApi, unraidApiSettings, updateOs, updateOsActions, updateOsChangelog, activationCode, and callbackActions stores. - Exposed callback error state in the callbackActions store for external access. - Made error state publicly accessible in the replaceRenew store. - **Tests** - Introduced new test files covering state, getters, actions, and side effects for multiple stores including modal, notifications, purchase, replaceRenew, theme, trial, unraidApi, unraidApiSettings, updateOs, updateOsActions, updateOsChangelog, activationCode, and callbackActions. - Enhanced existing test suites with additional mocks, reactive state handling, and expanded test cases for improved coverage and robustness. - **Refactor** - Improved code clarity and readability in modal, notifications, purchase, replaceRenew, trial, theme, updateOsActions, callbackActions, and unraidApi stores through import reorganization and formatting adjustments. - Updated imports to include reactive and computed utilities for enhanced state management in several stores. - Standardized import styles and streamlined store definitions in the unraidApiSettings store. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: mdatelle <mike@datelle.net>
129 lines
4.0 KiB
TypeScript
129 lines
4.0 KiB
TypeScript
import { computed, ref, watch } from 'vue';
|
|
import { createPinia, defineStore, setActivePinia } from 'pinia';
|
|
|
|
import { defaultColors } from '~/themes/default';
|
|
import hexToRgba from 'hex-to-rgba';
|
|
|
|
import type { Theme, ThemeVariables } from '~/themes/types';
|
|
|
|
/**
|
|
* @see https://stackoverflow.com/questions/73476371/using-pinia-with-vue-js-web-components
|
|
* @see https://github.com/vuejs/pinia/discussions/1085
|
|
*/
|
|
setActivePinia(createPinia());
|
|
|
|
// used to swap the UPC text color when using the azure or gray theme
|
|
export const DARK_THEMES = ['black', 'gray'] as const;
|
|
|
|
export const useThemeStore = defineStore('theme', () => {
|
|
// State
|
|
const theme = ref<Theme>({
|
|
name: 'white',
|
|
banner: false,
|
|
bannerGradient: false,
|
|
bgColor: '',
|
|
descriptionShow: false,
|
|
metaColor: '',
|
|
textColor: '',
|
|
});
|
|
|
|
const activeColorVariables = ref<ThemeVariables>(defaultColors.white);
|
|
|
|
// Getters
|
|
const darkMode = computed<boolean>(
|
|
() => DARK_THEMES.includes(theme.value?.name as (typeof DARK_THEMES)[number]) ?? false
|
|
);
|
|
|
|
const bannerGradient = computed(() => {
|
|
if (!theme.value?.banner || !theme.value?.bannerGradient) {
|
|
return undefined;
|
|
}
|
|
const start = theme.value?.bgColor ? 'var(--header-gradient-start)' : 'rgba(0, 0, 0, 0)';
|
|
const end = theme.value?.bgColor ? 'var(--header-gradient-end)' : 'var(--header-background-color)';
|
|
return `background-image: linear-gradient(90deg, ${start} 0, ${end} 30%);`;
|
|
});
|
|
// Actions
|
|
const setTheme = (data: Theme) => {
|
|
theme.value = data;
|
|
};
|
|
|
|
const setCssVars = () => {
|
|
const selectedTheme = theme.value.name;
|
|
const customTheme = { ...defaultColors[selectedTheme] };
|
|
// Set banner gradient if enabled
|
|
if (theme.value.banner && theme.value.bannerGradient) {
|
|
const start = theme.value.bgColor
|
|
? hexToRgba(theme.value.bgColor, 0)
|
|
: customTheme['--header-gradient-start'];
|
|
const end = theme.value.bgColor
|
|
? hexToRgba(theme.value.bgColor, 0.7)
|
|
: customTheme['--header-gradient-end'];
|
|
|
|
// set the banner gradient
|
|
customTheme['--banner-gradient'] = `linear-gradient(90deg, ${start} 0, ${end} 30%)`;
|
|
}
|
|
|
|
// overwrite with hex colors set in webGUI @ /Settings/DisplaySettings
|
|
if (theme.value.textColor) {
|
|
customTheme['--header-text-primary'] = theme.value.textColor;
|
|
}
|
|
if (theme.value.metaColor) {
|
|
customTheme['--header-text-secondary'] = theme.value.metaColor;
|
|
}
|
|
|
|
if (theme.value.bgColor) {
|
|
customTheme['--header-background-color'] = theme.value.bgColor;
|
|
customTheme['--header-gradient-start'] = hexToRgba(theme.value.bgColor, 0);
|
|
customTheme['--header-gradient-end'] = hexToRgba(theme.value.bgColor, 0.7);
|
|
}
|
|
|
|
requestAnimationFrame(() => {
|
|
if (darkMode.value) {
|
|
document.body.classList.add('dark');
|
|
} else {
|
|
document.body.classList.remove('dark');
|
|
}
|
|
|
|
document.body.style.cssText = createCssText(customTheme, document.body);
|
|
activeColorVariables.value = customTheme;
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Creates a string of CSS rules preserving existing rules that are not defined in the theme variables
|
|
* @param themeVariables - The theme variables to apply
|
|
* @param body - The body element to apply the CSS to
|
|
* @returns A string of CSS rules
|
|
*/
|
|
const createCssText = (themeVariables: ThemeVariables, body: HTMLElement) => {
|
|
const existingStyles = body.style.cssText
|
|
.split(';')
|
|
.filter((rule) => rule.trim())
|
|
.filter((rule) => {
|
|
// Keep rules that aren't in our theme variables
|
|
return !Object.keys(themeVariables).some((themeVar) => rule.startsWith(themeVar));
|
|
});
|
|
|
|
const themeStyles = Object.entries(themeVariables).reduce((acc, [key, value]) => {
|
|
if (value) acc.push(`${key}: ${value}`);
|
|
return acc;
|
|
}, [] as string[]);
|
|
|
|
return [...existingStyles, ...themeStyles].join(';');
|
|
};
|
|
|
|
watch(theme, () => {
|
|
setCssVars();
|
|
});
|
|
|
|
return {
|
|
// state
|
|
activeColorVariables,
|
|
bannerGradient,
|
|
darkMode,
|
|
theme,
|
|
// actions
|
|
setTheme,
|
|
};
|
|
});
|