|
| 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> |
0 commit comments