|
1 | 1 | <script lang="ts"> |
| 2 | + import { getContext } from 'svelte'; |
| 3 | + import type { AppState, Profile } from '$lib/types/appState'; |
| 4 | + import { updateProfile } from '$lib/services/database/profiles'; |
2 | 5 | import Tooltip from './Tooltip.svelte'; |
3 | 6 |
|
4 | | - // Font size state |
| 7 | + // Get app state from context |
| 8 | + const getApp = getContext<() => AppState>('getApp'); |
| 9 | + const setProfile = getContext<(newProfile: Profile) => void>('setProfile'); |
| 10 | + const app = $derived(getApp()); |
| 11 | +
|
| 12 | + // Font size state - read from appState preferences or default to medium |
5 | 13 | let fontSize = $state<'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'>('medium'); |
| 14 | +
|
| 15 | + // Keep fontSize in sync with appState preferences |
| 16 | + $effect(() => { |
| 17 | + fontSize = app.profile.preferences?.fontSize || 'medium'; |
| 18 | + }); |
6 | 19 | let showModal = $state(false); |
7 | 20 |
|
8 | 21 | const fontSizeLabels = { |
|
13 | 26 | 'extra-large': 'Extra Large' |
14 | 27 | }; |
15 | 28 |
|
| 29 | + // Helper function to update both local state and appState preferences |
| 30 | + const updateFontSize = async (newSize: typeof fontSize) => { |
| 31 | + fontSize = newSize; |
| 32 | +
|
| 33 | + // Update appState preferences immediately for UI responsiveness |
| 34 | + const updatedProfile = { |
| 35 | + ...app.profile, |
| 36 | + preferences: { |
| 37 | + ...app.profile.preferences, |
| 38 | + fontSize: newSize |
| 39 | + } |
| 40 | + }; |
| 41 | + setProfile(updatedProfile); |
| 42 | +
|
| 43 | + // Save to database if user is logged in |
| 44 | + if (app.profile.id) { |
| 45 | + try { |
| 46 | + const result = await updateProfile(app.profile.id, { |
| 47 | + preferences: updatedProfile.preferences |
| 48 | + }); |
| 49 | + if (result.error) { |
| 50 | + console.error('Database error saving font size preference:', result.error); |
| 51 | + } |
| 52 | + } catch (error) { |
| 53 | + console.error('Failed to save font size preference:', error); |
| 54 | + } |
| 55 | + } |
| 56 | + }; |
| 57 | +
|
16 | 58 | const increaseFontSize = () => { |
17 | | - if (fontSize === 'extra-small') fontSize = 'small'; |
18 | | - else if (fontSize === 'small') fontSize = 'medium'; |
19 | | - else if (fontSize === 'medium') fontSize = 'large'; |
20 | | - else if (fontSize === 'large') fontSize = 'extra-large'; |
| 59 | + if (fontSize === 'extra-small') updateFontSize('small'); |
| 60 | + else if (fontSize === 'small') updateFontSize('medium'); |
| 61 | + else if (fontSize === 'medium') updateFontSize('large'); |
| 62 | + else if (fontSize === 'large') updateFontSize('extra-large'); |
21 | 63 | }; |
22 | 64 |
|
23 | 65 | const decreaseFontSize = () => { |
24 | | - if (fontSize === 'extra-large') fontSize = 'large'; |
25 | | - else if (fontSize === 'large') fontSize = 'medium'; |
26 | | - else if (fontSize === 'medium') fontSize = 'small'; |
27 | | - else if (fontSize === 'small') fontSize = 'extra-small'; |
| 66 | + if (fontSize === 'extra-large') updateFontSize('large'); |
| 67 | + else if (fontSize === 'large') updateFontSize('medium'); |
| 68 | + else if (fontSize === 'medium') updateFontSize('small'); |
| 69 | + else if (fontSize === 'small') updateFontSize('extra-small'); |
28 | 70 | }; |
29 | 71 |
|
30 | 72 | const toggleModal = () => { |
|
0 commit comments