blob: 24529450bf8f7f4d92a81d251f681016548f01f2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import { useState } from 'react';
export interface OllamaSettings {
enabled: boolean;
baseUrl: string;
model: string;
}
const DEFAULTS: OllamaSettings = {
enabled: false,
baseUrl: 'http://localhost:11434',
model: 'llama3.2',
};
const KEY = 'bw:settings';
function load(): OllamaSettings {
try {
const raw = localStorage.getItem(KEY);
return raw ? { ...DEFAULTS, ...JSON.parse(raw) } : { ...DEFAULTS };
} catch {
return { ...DEFAULTS };
}
}
// Non-reactive read — used inside callbacks without needing the hook
export function readSettings(): OllamaSettings {
return load();
}
export function useSettings() {
const [settings, setSettingsRaw] = useState<OllamaSettings>(load);
const setSettings = (next: Partial<OllamaSettings>) => {
const merged = { ...settings, ...next };
try { localStorage.setItem(KEY, JSON.stringify(merged)); } catch { /* ignore */ }
setSettingsRaw(merged);
};
return { settings, setSettings };
}
|