Skip to content

Modern Docker Management, Designed for Everyone

11.7 KB
Raw Blame
<script lang="ts">
	import { mode } from 'mode-watcher';

	import AccentColorPicker from '#lib/components/accent-color/accent-color-picker.svelte';
	import ApplicationThemePicker from '#lib/components/application-theme/application-theme-picker.svelte';
	import FontSizePicker from '#lib/components/font-size-picker.svelte';
	import SelectWithLabel from '#lib/components/form/select-with-label.svelte';
	import LocalePicker from '#lib/components/locale-picker.svelte';
	import SettingsRow from '#lib/components/settings/settings-row.svelte';
	import SettingsSection from '#lib/components/settings/settings-section.svelte';
	import ThemeModeSelector from '#lib/components/theme-mode/theme-mode-selector.svelte';
	import TimeFormatPicker from '#lib/components/time-format-picker.svelte';
	import { Switch } from '#lib/components/ui/switch/index.js';
	import { DEFAULT_LANDING_PAGE, getLandingPageNavItems } from '#lib/config/navigation-config.js';
	import { DockIcon, MonitorSpeakerIcon } from '#lib/icons/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import { getLayoutMode, layoutModeStore, type LayoutMode } from '#lib/stores/layout-mode.store.svelte.js';
	import userStore from '#lib/stores/user-store.svelte.js';
	import type { ProjectEditorLayout, UserPreferences } from '#lib/types/auth.js';
	import type { ApplicationTheme, IconCatalog } from '#lib/types/settings.js';
	import { cn } from '#lib/utils.js';
	import { getMobileNavigation } from '#lib/utils/navigation.js';
	import { applyAccentColor, applyGlassEffects, applyInterfaceAnimations, applyOledMode } from '#lib/utils/theme.svelte.js';
	import { saveUserPreferences } from '#lib/utils/user-preferences.js';
	import { debounced } from '#lib/utils/ws.js';

	const mobileNavigation = getMobileNavigation();
	const currentUser = $derived(userStore.current);
	const preferences = $derived(currentUser?.preferences ?? {});
	const applicationThemeValue = $derived<ApplicationTheme>(preferences.applicationTheme ?? 'default');
	const accentColorValue = $derived(
		preferences.accentColor && preferences.accentColor !== 'default' ? preferences.accentColor : 'theme'
	);
	const iconCatalogValue = $derived(preferences.iconCatalog ?? 'selfhst');
	const oledModeEnabled = $derived(preferences.oledMode ?? false);
	const glassEffectsEnabled = $derived(preferences.glassEffectsEnabled ?? true);
	const animationsEnabled = $derived(preferences.animationsEnabled ?? true);
	const sidebarHoverExpansionEnabled = $derived(preferences.sidebarHoverExpansion ?? true);
	const keyboardShortcutsEnabled = $derived(preferences.keyboardShortcutsEnabled ?? true);
	const mobileNavigationMode = $derived(preferences.mobileNavigationMode ?? 'floating');
	const mobileNavigationShowLabels = $derived(preferences.mobileNavigationShowLabels ?? true);
	const projectEditorLayoutValue = $derived<ProjectEditorLayout>(preferences.defaultProjectEditorLayout ?? 'auto');
	const isDarkMode = $derived(mode.current === 'dark');
	const isDefaultApplicationTheme = $derived(applicationThemeValue === 'default');

	const iconCatalogOptions = $derived([
		{ value: 'selfhst', label: m.icon_catalog_selfhst(), description: m.icon_catalog_selfhst_description() },
		{
			value: 'dashboard-icons',
			label: m.icon_catalog_dashboard_icons(),
			description: m.icon_catalog_dashboard_icons_description()
		}
	]);
	const projectEditorLayoutOptions = $derived([
		{ value: 'auto', label: m.automatic(), description: m.project_editor_layout_auto_description() },
		{ value: 'classic', label: m.classic(), description: m.project_editor_layout_classic_description() },
		{ value: 'tree', label: m.tree(), description: m.project_editor_layout_tree_description() }
	]);
	// Device-local (localStorage), not part of the account preferences: a user may want
	// desktop layout on a tablet and automatic on a phone.
	const layoutModeValue = $derived<LayoutMode>(getLayoutMode());
	const layoutModeOptions = $derived([
		{ value: 'auto', label: m.automatic(), description: m.layout_mode_auto_description() },
		{ value: 'mobile', label: m.layout_mode_mobile(), description: m.layout_mode_mobile_description() },
		{ value: 'desktop', label: m.layout_mode_desktop(), description: m.layout_mode_desktop_description() }
	]);
	const landingPageOptions = $derived(getLandingPageNavItems().map((item) => ({ value: item.url, label: item.title })));
	const landingValue = $derived.by(() => {
		const current = preferences.defaultLandingPage ?? DEFAULT_LANDING_PAGE;
		return landingPageOptions.some((option) => option.value === current) ? current : DEFAULT_LANDING_PAGE;
	});

	let pendingPreferences: Partial<UserPreferences> = {};
	const flushPreferences = debounced(() => {
		const patch = pendingPreferences;
		pendingPreferences = {};
		void saveUserPreferences(patch);
	}, 400);

	function savePreferencesDebounced(patch: Partial<UserPreferences>) {
		pendingPreferences = { ...pendingPreferences, ...patch };
		flushPreferences();
	}

	function selectMobileNavigationMode(value: 'floating' | 'docked') {
		if (value === mobileNavigationMode) return;
		mobileNavigation.visible = true;
		void saveUserPreferences({ mobileNavigationMode: value });
	}
</script>

<div class="space-y-8">
	<SettingsSection title={m.general_title()}>
		<SettingsRow label={m.language()} description={m.account_language_desc()} layout="switch">
			<LocalePicker inline id="accountLocalePicker" />
		</SettingsRow>
		<SettingsRow label={m.time_format()} description={m.account_time_format_desc()} layout="switch">
			<TimeFormatPicker id="accountTimeFormatPicker" />
		</SettingsRow>
		<SettingsRow label={m.project_editor_layout()} description={m.project_editor_layout_description()} layout="switch">
			<div class="w-52">
				<SelectWithLabel
					id="account-project-editor-layout"
					label={m.project_editor_layout()}
					hideLabel
					triggerSize="sm"
					value={projectEditorLayoutValue}
					options={projectEditorLayoutOptions}
					onValueChange={(value) => void saveUserPreferences({ defaultProjectEditorLayout: value as ProjectEditorLayout })}
				/>
			</div>
		</SettingsRow>
	</SettingsSection>

	<SettingsSection title={m.appearance_title()}>
		<SettingsRow label={m.account_theme()} description={m.appearance_theme_current_user_description()} layout="switch">
			<ThemeModeSelector />
		</SettingsRow>
		<SettingsRow label={m.font_size()} description={m.font_size_description()} layout="switch">
			<FontSizePicker />
		</SettingsRow>
		<SettingsRow label={m.layout_mode()} description={m.layout_mode_description()} layout="switch">
			<div class="w-52">
				<SelectWithLabel
					id="account-layout-mode"
					label={m.layout_mode()}
					hideLabel
					triggerSize="sm"
					value={layoutModeValue}
					options={layoutModeOptions}
					onValueChange={(value) => (layoutModeStore.current = value as LayoutMode)}
				/>
			</div>
		</SettingsRow>
		<SettingsRow label={m.icon_catalog()} description={m.icon_catalog_description()} layout="switch">
			<div class="w-52">
				<SelectWithLabel
					id="account-icon-catalog"
					label={m.icon_catalog()}
					hideLabel
					triggerSize="sm"
					value={iconCatalogValue}
					options={iconCatalogOptions}
					onValueChange={(value) => void saveUserPreferences({ iconCatalog: value as IconCatalog })}
				/>
			</div>
		</SettingsRow>
		<SettingsRow label={m.application_theme()} description={m.application_theme_description()} layout="wide">
			<ApplicationThemePicker
				selectedTheme={applicationThemeValue}
				accentColor={accentColorValue}
				onSelect={(value) => savePreferencesDebounced({ applicationTheme: value })}
			/>
		</SettingsRow>
		<SettingsRow label={m.accent_color()} description={m.accent_color_description()} layout="wide">
			<AccentColorPicker
				previousColor={accentColorValue}
				selectedColor={accentColorValue}
				onSelect={(value) => {
					applyAccentColor(value);
					savePreferencesDebounced({ accentColor: value });
				}}
			/>
		</SettingsRow>
		<SettingsRow label={m.oled_mode()} description={m.oled_mode_description()} layout="switch">
			{#snippet helpText()}
				{#if !isDefaultApplicationTheme}
					<span class="italic">{m.oled_mode_requires_default_theme()}</span>
				{:else if !isDarkMode}
					<span class="italic">{m.oled_mode_requires_dark()}</span>
				{/if}
			{/snippet}
			<Switch
				id="account-oled-mode"
				checked={oledModeEnabled}
				disabled={!isDefaultApplicationTheme}
				onCheckedChange={(checked) => {
					applyOledMode(checked);
					void saveUserPreferences({ oledMode: checked });
				}}
			/>
		</SettingsRow>
		<SettingsRow label={m.glass_effects()} description={m.glass_effects_description()} layout="switch">
			<Switch
				id="account-glass-effects"
				checked={glassEffectsEnabled}
				onCheckedChange={(checked) => {
					applyGlassEffects(checked);
					void saveUserPreferences({ glassEffectsEnabled: checked });
				}}
			/>
		</SettingsRow>
		<SettingsRow label={m.interface_animations()} description={m.interface_animations_description()} layout="switch">
			<Switch
				id="account-animations"
				checked={animationsEnabled}
				onCheckedChange={(checked) => {
					applyInterfaceAnimations(checked);
					void saveUserPreferences({ animationsEnabled: checked });
				}}
			/>
		</SettingsRow>
	</SettingsSection>

	<SettingsSection title={m.navigation_title()}>
		<SettingsRow
			label={m.navigation_default_landing_page_label()}
			description={m.navigation_default_landing_page_description()}
			layout="switch"
		>
			<div class="w-52">
				<SelectWithLabel
					id="account-default-landing-page"
					label={m.navigation_default_landing_page_label()}
					hideLabel
					triggerSize="sm"
					value={landingValue}
					options={landingPageOptions}
					onValueChange={(value) => void saveUserPreferences({ defaultLandingPage: value })}
				/>
			</div>
		</SettingsRow>
		<SettingsRow
			label={m.navigation_sidebar_hover_expansion_label()}
			description={m.navigation_sidebar_hover_expansion_description()}
			layout="switch"
		>
			<Switch
				id="account-sidebar-hover-expansion"
				checked={sidebarHoverExpansionEnabled}
				onCheckedChange={(checked) => void saveUserPreferences({ sidebarHoverExpansion: checked })}
			/>
		</SettingsRow>
		<SettingsRow
			label={m.navigation_keyboard_shortcuts_label()}
			description={m.navigation_keyboard_shortcuts_description()}
			layout="switch"
		>
			<Switch
				id="account-keyboard-shortcuts"
				checked={keyboardShortcutsEnabled}
				onCheckedChange={(checked) => void saveUserPreferences({ keyboardShortcutsEnabled: checked })}
			/>
		</SettingsRow>
	</SettingsSection>

	<SettingsSection title={m.navigation_mobile_appearance_title()}>
		<SettingsRow label={m.navigation_mode_label()} description={m.navigation_mode_description()} layout="switch">
			<div class="inline-flex rounded-lg bg-muted/40 p-0.5">
				{#each [{ value: 'floating' as const, label: m.navigation_mode_floating(), icon: MonitorSpeakerIcon }, { value: 'docked' as const, label: m.navigation_mode_docked(), icon: DockIcon }] as option (option.value)}
					<button
						type="button"
						aria-pressed={mobileNavigationMode === option.value}
						onclick={() => selectMobileNavigationMode(option.value)}
						class={cn(
							'inline-flex items-center gap-1.5 rounded-md px-3 py-1.5 text-xs font-medium transition-colors',
							mobileNavigationMode === option.value
								? 'bg-background text-foreground shadow-sm'
								: 'text-muted-foreground hover:text-foreground'
						)}
					>
						<option.icon class="size-3.5" />
						{option.label}
					</button>
				{/each}
			</div>
		</SettingsRow>
		<SettingsRow label={m.navigation_show_labels_label()} description={m.navigation_show_labels_description()} layout="switch">
			<Switch
				id="account-mobile-nav-labels"
				checked={mobileNavigationShowLabels}
				onCheckedChange={(checked) => void saveUserPreferences({ mobileNavigationShowLabels: checked })}
			/>
		</SettingsRow>
	</SettingsSection>
</div>