import { writable } from 'svelte/store'; import { baseUrl } from '$lib/env'; import type { Settings } from '$lib/types'; // Create a default settings object const defaultSettings: Settings = { numScreens: 7, invertedColor: false, timerSeconds: 60, timerRunning: false, minSecPriceUpd: 30, fullRefreshMin: 60, wpTimeout: 600, tzString: 'UTC', dataSource: 0, mempoolInstance: 'mempool.space', mempoolSecure: true, localPoolEndpoint: 'localhost:2019', nostrPubKey: '', nostrRelay: 'wss://relay.damus.io', nostrZapNotify: false, nostrZapPubkey: '', ledFlashOnZap: true, fontName: 'oswald', availableFonts: ['antonio', 'oswald'], customEndpoint: 'ws-staging.btclock.dev', customEndpointDisableSSL: false, ledTestOnPower: true, ledFlashOnUpd: true, ledBrightness: 255, stealFocus: false, mcapBigChar: true, mdnsEnabled: true, otaEnabled: true, useSatsSymbol: true, useBlkCountdown: true, suffixPrice: false, disableLeds: false, mowMode: false, verticalDesc: true, suffixShareDot: false, enableDebugLog: false, hostnamePrefix: 'btclock', hostname: 'btclock', ip: '', txPower: 80, gitReleaseUrl: 'https://git.btclock.dev/api/v1/repos/btclock/btclock_v3/releases/latest', bitaxeEnabled: false, bitaxeHostname: 'bitaxe1', miningPoolStats: false, miningPoolName: 'noderunners', miningPoolUser: '', availablePools: [ 'ocean', 'noderunners', 'satoshi_radio', 'braiins', 'public_pool', 'local_public_pool', 'gobrrr_pool', 'ckpool', 'eu_ckpool' ], httpAuthEnabled: false, httpAuthUser: 'btclock', httpAuthPass: 'satoshi', hasFrontlight: false, // Default frontlight settings flDisable: false, flMaxBrightness: 2684, flAlwaysOn: false, flEffectDelay: 50, flFlashOnUpd: true, flFlashOnZap: true, // Default light sensor settings hasLightLevel: false, luxLightToggle: 128, flOffWhenDark: false, hwRev: '', fsRev: '', gitRev: '', gitTag: '', lastBuildTime: '', screens: [ { id: 0, name: 'Block Height', enabled: true }, { id: 3, name: 'Time', enabled: false }, { id: 4, name: 'Halving countdown', enabled: false }, { id: 6, name: 'Block Fee Rate', enabled: false }, { id: 10, name: 'Sats per dollar', enabled: true }, { id: 20, name: 'Ticker', enabled: true }, { id: 30, name: 'Market Cap', enabled: false } ], actCurrencies: ['USD'], availableCurrencies: ['USD', 'EUR', 'GBP', 'JPY', 'AUD', 'CAD'], poolLogosUrl: 'https://git.btclock.dev/btclock/mining-pool-logos/raw/branch/main', ceEndpoint: 'ws-staging.btclock.dev', ceDisableSSL: false, dnd: { enabled: false, timeBasedEnabled: false, startHour: 23, startMinute: 0, endHour: 7, endMinute: 0 } }; // Create the Svelte store function createSettingsStore() { const { subscribe, set, update } = writable(defaultSettings); return { subscribe, fetch: async () => { try { const response = await fetch(`${baseUrl}/api/settings`); if (!response.ok) { throw new Error(`Error fetching settings: ${response.statusText}`); } const data = await response.json(); data.timePerScreen = data.timerSeconds / 60; data.isLoaded = true; set(data); return data; } catch (error) { console.error('Failed to fetch settings:', error); return defaultSettings; } }, update: async (newSettings: Partial) => { try { const formSettings = { ...newSettings }; delete formSettings['gitRev']; delete formSettings['ip']; delete formSettings['lastBuildTime']; const response = await fetch(`${baseUrl}/api/json/settings`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formSettings) }); if (!response.ok) { throw new Error(`Error updating settings: ${response.statusText}`); } else { console.log(formSettings, response); } // Update the local store with the new settings update((currentSettings) => ({ ...currentSettings, ...newSettings })); return true; } catch (error) { console.error('Failed to update settings:', error); return false; } }, set: (newSettings: Settings) => set(newSettings), reset: async () => { try { const response = await fetch(`${baseUrl}/api/settings`); if (!response.ok) { throw new Error(`Error fetching settings: ${response.statusText}`); } const data = await response.json(); set(data); return data; } catch (error) { console.error('Failed to fetch settings:', error); return defaultSettings; } } }; } export const settings = createSettingsStore(); // Initialize the store by fetching settings when this module is first imported if (typeof window !== 'undefined') { settings.fetch(); }