summaryrefslogtreecommitdiff
path: root/src/hooks/useSettings.ts
blob: 09a8f01b8ed127755c31b899584d3326e6f484f0 (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: '',   // empty = same-origin proxy (/api/ via nginx); set to http://localhost:11434 for local dev
  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 };
}