krz/brand-bench
A dynamic brand documentation generator with Ollama integration.
clone: git clone https://gitbay.org/krz/brand-bench.git
main: src/hooks/useSettings.ts · raw
1import { useState } from 'react';
2
3export interface OllamaSettings {
4 enabled: boolean;
5 baseUrl: string;
6 model: string;
7}
8
9const DEFAULTS: OllamaSettings = {
10 enabled: false,
11 baseUrl: '', // empty = same-origin proxy (/api/ via nginx); set to http://localhost:11434 for local dev
12 model: 'llama3.2',
13};
14
15const KEY = 'bw:settings';
16
17function load(): OllamaSettings {
18 try {
19 const raw = localStorage.getItem(KEY);
20 return raw ? { ...DEFAULTS, ...JSON.parse(raw) } : { ...DEFAULTS };
21 } catch {
22 return { ...DEFAULTS };
23 }
24}
25
26// Non-reactive read — used inside callbacks without needing the hook
27export function readSettings(): OllamaSettings {
28 return load();
29}
30
31export function useSettings() {
32 const [settings, setSettingsRaw] = useState<OllamaSettings>(load);
33
34 const setSettings = (next: Partial<OllamaSettings>) => {
35 const merged = { ...settings, ...next };
36 try { localStorage.setItem(KEY, JSON.stringify(merged)); } catch { /* ignore */ }
37 setSettingsRaw(merged);
38 };
39
40 return { settings, setSettings };
41}