Skip to content

Commit be94ccd

Browse files
committed
feat: add theme switcher component
- Introduced a new `UnraidThemeSwitcher` component to allow users to change themes dynamically. - Updated `nuxt.config.ts` to register the new component. - Created `ThemeSwitcher.ce.vue` with functionality to handle theme changes and update the UI accordingly.
1 parent 48c6ad7 commit be94ccd

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<script lang="ts" setup>
2+
/**
3+
* Add to webgui via DefaultPageLayout.php
4+
* Find the footer and the PHP that builds it. Search for `annotate('Footer');` for the start of the footer.
5+
*
6+
* At the of the footer end replace this PHP
7+
* ```
8+
* echo "</span></div>";
9+
* ```
10+
* with the following PHP
11+
* ```
12+
* echo "</span>";
13+
* echo "<unraid-theme-switcher current='$theme'></unraid-theme-switcher>";
14+
* echo "</div>";
15+
* ```
16+
*/
17+
import { ref } from 'vue';
18+
import { storeToRefs } from 'pinia';
19+
import { WebguiUpdate } from '~/composables/services/webgui';
20+
import { useServerStore } from '~/store/server';
21+
22+
const props = defineProps<{
23+
current: string;
24+
}>();
25+
26+
const { csrf } = storeToRefs(useServerStore());
27+
const devModeEnabled = ref<boolean>(import.meta.env.VITE_ALLOW_CONSOLE_LOGS);
28+
const themes = ref<string[]>(['azure', 'black', 'gray', 'white']);
29+
const submitting = ref<boolean>(false);
30+
31+
const handleThemeChange = (event: Event) => {
32+
const newTheme = (event.target as HTMLSelectElement).value;
33+
34+
if (newTheme === props.current) {
35+
console.debug('[ThemeSwitcher.setTheme] Theme is already set');
36+
return;
37+
}
38+
39+
console.debug('[ThemeSwitcher.setTheme] Submitting form');
40+
submitting.value = true;
41+
42+
try {
43+
WebguiUpdate
44+
.formUrl({
45+
csrf_token: csrf.value,
46+
'#file': 'dynamix/dynamix.cfg',
47+
'#section': 'display',
48+
theme: newTheme,
49+
})
50+
.post()
51+
.res(() => {
52+
console.log('[ThemeSwitcher.setTheme] Theme updated, reloading…');
53+
setTimeout(() => {
54+
window.location.reload();
55+
}, 1000);
56+
});
57+
} catch (error) {
58+
console.error('[ThemeSwitcher.setTheme] Failed to update theme', error);
59+
throw new Error('[ThemeSwitcher.setTheme] Failed to update theme');
60+
}
61+
};
62+
</script>
63+
64+
<template>
65+
<select
66+
v-if="devModeEnabled"
67+
:disabled="submitting"
68+
:value="props.current"
69+
class="text-xs relative float-left mr-2 text-white bg-black"
70+
@change="handleThemeChange"
71+
>
72+
<option
73+
v-for="theme in themes"
74+
:key="theme"
75+
:value="theme"
76+
>
77+
{{ theme }}
78+
</option>
79+
</select>
80+
</template>
81+
82+
<style lang="postcss">
83+
/* Import unraid-ui globals first */
84+
@import '@unraid/ui/styles';
85+
@import '~/assets/main.css';
86+
</style>

web/nuxt.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ export default defineNuxtConfig({
138138
name: 'UnraidLogViewer',
139139
path: '@/components/Logs/LogViewer.ce',
140140
},
141+
{
142+
name: 'UnraidThemeSwitcher',
143+
path: '@/components/ThemeSwitcher.ce',
144+
},
141145
],
142146
},
143147
],

0 commit comments

Comments
 (0)