Skip to content

Commit 8a67891

Browse files
committed
feat(mavrochat): add model preference persistence using localStorage
1 parent 77977b9 commit 8a67891

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

apps/mavrochat/src/context/ModelContext.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
11
'use client';
22

3-
import { createContext, useContext, useState } from 'react';
3+
import { createContext, useContext, useState, useEffect } from 'react';
44

55
interface ModelContextType {
66
model: string;
77
setModel: (model: string) => void;
88
}
99

1010
const DEFAULT_MODEL = 'gpt-4o';
11+
const MODEL_STORAGE_KEY = 'mavrochat-selected-model';
1112

1213
const ModelContext = createContext<ModelContextType | undefined>(undefined);
1314

1415
export function ModelProvider({ children }: { children: React.ReactNode }) {
1516
const [model, setModel] = useState<string>(DEFAULT_MODEL);
17+
const [mounted, setMounted] = useState(false);
18+
19+
// Load saved model preference on mount
20+
useEffect(() => {
21+
setMounted(true);
22+
try {
23+
const savedModel = localStorage.getItem(MODEL_STORAGE_KEY);
24+
if (savedModel) {
25+
setModel(savedModel);
26+
}
27+
} catch (error) {
28+
console.error('Failed to load saved model:', error);
29+
}
30+
}, []);
31+
32+
// Save model preference when it changes (only after mounted)
33+
useEffect(() => {
34+
if (mounted) {
35+
try {
36+
localStorage.setItem(MODEL_STORAGE_KEY, model);
37+
} catch (error) {
38+
console.error('Failed to save model preference:', error);
39+
}
40+
}
41+
}, [model, mounted]);
1642

1743
return (
1844
<ModelContext.Provider value={{ model, setModel }}>

packages/ui/src/components/navigation/LogoButton.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Link from 'next/link';
33
import Image from 'next/image';
44
import { useTheme } from 'next-themes';
55
import { Button } from '../form/Button';
6+
import { useState, useEffect } from 'react';
67

78
export function LogoButton({
89
className,
@@ -15,8 +16,18 @@ export function LogoButton({
1516
darkLogoSrc: string;
1617
href?: string;
1718
}) {
18-
const { theme } = useTheme();
19-
const logoSrc = theme === 'light' ? lightLogoSrc : darkLogoSrc;
19+
const { theme, systemTheme } = useTheme();
20+
const [mounted, setMounted] = useState(false);
21+
22+
// Avoid hydration mismatch by only rendering theme-specific content after mount
23+
useEffect(() => {
24+
setMounted(true);
25+
}, []);
26+
27+
// Use dark logo as default until we know the actual theme
28+
const currentTheme = theme === 'system' ? systemTheme : theme;
29+
const logoSrc =
30+
mounted && currentTheme === 'light' ? lightLogoSrc : darkLogoSrc;
2031
const isExternal = href.startsWith('http');
2132

2233
return (

0 commit comments

Comments
 (0)