Skip to content

Commit

Permalink
fix: save user theme to settingsStore (#107)
Browse files Browse the repository at this point in the history
Closes #78
  • Loading branch information
GregoMac1 authored Jul 25, 2024
1 parent dbea36b commit 2f8b4cb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
16 changes: 13 additions & 3 deletions src/app.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<link rel="stylesheet" href="/fonts/inter/inter.css">
<link rel="stylesheet" href="/fonts/jetbrains-mono/jetbrains-mono.css">
<link rel="stylesheet" href="/fonts/inter/inter.css" />
<link rel="stylesheet" href="/fonts/jetbrains-mono/jetbrains-mono.css" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>Hollama</title>
%sveltekit.head%
</head>
<script>
// We check and apply the color theme preference on initial load
// to prevent a flash of unstyled content
let theme = JSON.parse(localStorage.getItem('hollama-settings'))?.userTheme;
if (theme) document.documentElement.setAttribute('data-color-theme', theme);
else {
theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
document.documentElement.setAttribute('data-color-theme', theme);
}
</script>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
Expand Down
1 change: 1 addition & 0 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface Settings {
ollamaServer: string | null;
ollamaModel: string | null;
ollamaModels: OllamaModel[];
userTheme?: 'light' | 'dark';
}

export enum StorageKey {
Expand Down
21 changes: 12 additions & 9 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<script lang="ts">
import { onMount } from 'svelte';
import { env } from '$env/dynamic/public';
import { page } from '$app/stores';
import { Brain, MessageSquareText, Settings2, Sun, Moon } from 'lucide-svelte';
import '../app.pcss';
import { settingsStore } from '$lib/store';
import { onMount } from 'svelte';
import { browser } from '$app/environment';
$: pathname = $page.url.pathname;
const SITEMAP = [
Expand All @@ -13,19 +15,20 @@
['/settings', 'Settings']
];
let theme = 'light';
$: theme = $settingsStore?.userTheme;
onMount(() => {
if (!$settingsStore || !browser || theme) return;
$settingsStore.userTheme = window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light';
});
function toggleTheme() {
theme = theme === 'light' ? 'dark' : 'light';
document.documentElement.setAttribute('data-color-theme', theme);
if ($settingsStore) $settingsStore.userTheme = theme;
}
onMount(() => {
// Set initial theme based on user's browser preference
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
theme = prefersDark ? 'dark' : 'light';
document.documentElement.setAttribute('data-color-theme', theme);
});
</script>

<svelte:head>
Expand Down
5 changes: 3 additions & 2 deletions src/routes/settings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
let ollamaModel = $settingsStore?.ollamaModel || '';
let ollamaTagResponse: OllamaTagResponse | null = null;
$: settingsStore.set({
$: settingsStore.update((settings) => ({
...settings,
ollamaServer,
ollamaModels: ollamaTagResponse?.models || [],
ollamaModel
});
}));
async function getModelsList(): Promise<void> {
try {
Expand Down

0 comments on commit 2f8b4cb

Please sign in to comment.