|
| 1 | +import { |
| 2 | + createSignal, |
| 3 | + Switch, |
| 4 | + Match, |
| 5 | + For, |
| 6 | + createEffect, |
| 7 | + onCleanup, |
| 8 | + Suspense, |
| 9 | +} from 'solid-js'; |
| 10 | + |
| 11 | +import General from './settings/General'; |
| 12 | +import Appearance from './settings/Appearance'; |
| 13 | +import Plugins from './settings/Plugins'; |
| 14 | +import Advanced from './settings/Advanced'; |
| 15 | +import About from './settings/About'; |
| 16 | +import { Icons } from '@/types/icons'; |
| 17 | +import { t } from '@/i18n'; |
| 18 | + |
| 19 | +interface SidebarItem { |
| 20 | + icon: Icons; |
| 21 | + id: string; |
| 22 | +} |
| 23 | + |
| 24 | +const sidebar: SidebarItem[] = [ |
| 25 | + { icon: 'icons:settings', id: 'general' }, |
| 26 | + { icon: 'yt-icons:color_lens', id: 'appearance' }, |
| 27 | + { icon: 'icons:extension', id: 'plugins' }, |
| 28 | + { icon: 'icons:dashboard', id: 'advanced' }, |
| 29 | + { icon: 'icons:info', id: 'about' }, |
| 30 | +]; |
| 31 | + |
| 32 | +interface SettingsModalProps { |
| 33 | + close: () => void; |
| 34 | +} |
| 35 | + |
| 36 | +export default (props: SettingsModalProps) => { |
| 37 | + const [currentCategory, setCurrentCategory] = createSignal('general'); |
| 38 | + const [isSidebarExpanded, setIsSidebarExpanded] = createSignal(true); |
| 39 | + |
| 40 | + createEffect(() => { |
| 41 | + const handleResize = () => { |
| 42 | + const isMobile = window.innerWidth <= 908; |
| 43 | + if (isMobile) { |
| 44 | + setIsSidebarExpanded(false); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + // initial call |
| 49 | + handleResize(); |
| 50 | + |
| 51 | + window.addEventListener('resize', handleResize); |
| 52 | + onCleanup(() => window.removeEventListener('resize', handleResize)); |
| 53 | + }); |
| 54 | + |
| 55 | + return ( |
| 56 | + <> |
| 57 | + <div |
| 58 | + class="ytmd-sui-modalOverlay" |
| 59 | + onClick={(e) => { |
| 60 | + if (e.target === e.currentTarget) { |
| 61 | + props.close(); |
| 62 | + } |
| 63 | + }} |
| 64 | + > |
| 65 | + <div class="ytmd-sui-modal"> |
| 66 | + <div |
| 67 | + class={`ytmd-sui-sidebar ${ |
| 68 | + !isSidebarExpanded() ? 'collapsed' : '' |
| 69 | + }`} |
| 70 | + > |
| 71 | + <div class="ytmd-sui-sidebarContent"> |
| 72 | + <For each={sidebar}> |
| 73 | + {(item) => ( |
| 74 | + <button |
| 75 | + class={`ytmd-sui-menuItem ${ |
| 76 | + currentCategory() === item.id ? 'active' : '' |
| 77 | + }`} |
| 78 | + onClick={() => setCurrentCategory(item.id)} |
| 79 | + title={ |
| 80 | + !isSidebarExpanded() |
| 81 | + ? t(`plugins.settings-ui.label.${item.id}`) |
| 82 | + : undefined |
| 83 | + } |
| 84 | + > |
| 85 | + <span class="ytmd-sui-icon"> |
| 86 | + <yt-icon icon={item.icon} tabindex="0" /> |
| 87 | + </span> |
| 88 | + <yt-formatted-string |
| 89 | + class="ytmd-sui-menuLabel title style-scope ytmusic-guide-entry-renderer" |
| 90 | + text={{ |
| 91 | + runs: [ |
| 92 | + { text: t(`plugins.settings-ui.label.${item.id}`) }, |
| 93 | + ], |
| 94 | + }} |
| 95 | + /> |
| 96 | + </button> |
| 97 | + )} |
| 98 | + </For> |
| 99 | + </div> |
| 100 | + <button |
| 101 | + class="ytmd-sui-sidebarToggle" |
| 102 | + onClick={() => setIsSidebarExpanded((old) => !old)} |
| 103 | + title={isSidebarExpanded() ? 'Collapse menu' : 'Expand menu'} |
| 104 | + > |
| 105 | + ☰ |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + |
| 109 | + <div class="ytmd-sui-content"> |
| 110 | + <div class="ytmd-sui-header"> |
| 111 | + <h2> |
| 112 | + {t( |
| 113 | + `plugins.settings-ui.title.${ |
| 114 | + sidebar.find((item) => item.id === currentCategory())!.id |
| 115 | + }`, |
| 116 | + )} |
| 117 | + </h2> |
| 118 | + <button class="ytmd-sui-closeButton" onClick={props.close}> |
| 119 | + ✕ |
| 120 | + </button> |
| 121 | + </div> |
| 122 | + |
| 123 | + <Suspense fallback={<div class="ytmd-sui-loading">Loading...</div>}> |
| 124 | + <Switch fallback={<General />}> |
| 125 | + <Match when={currentCategory() === 'general'}> |
| 126 | + <General /> |
| 127 | + </Match> |
| 128 | + <Match when={currentCategory() === 'appearance'}> |
| 129 | + <Appearance /> |
| 130 | + </Match> |
| 131 | + <Match when={currentCategory() === 'plugins'}> |
| 132 | + <Plugins /> |
| 133 | + </Match> |
| 134 | + <Match when={currentCategory() === 'advanced'}> |
| 135 | + <Advanced /> |
| 136 | + </Match> |
| 137 | + <Match when={currentCategory() === 'about'}> |
| 138 | + <About /> |
| 139 | + </Match> |
| 140 | + </Switch> |
| 141 | + </Suspense> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + </> |
| 146 | + ); |
| 147 | +}; |
0 commit comments