Updates
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Desktop UI preferences (theme, density, chart options).
|
||||
* Stored under Pear.config.storage or ~/.config/peardata/ui-settings.json.
|
||||
*/
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import os from 'os'
|
||||
|
||||
const FILE_NAME = 'ui-settings.json'
|
||||
const VERSION = 1
|
||||
|
||||
/** @typedef {{
|
||||
* theme: 'dark'|'light',
|
||||
* accent: string,
|
||||
* density: 'comfortable'|'compact'|'spacious',
|
||||
* sidebarCollapsed: boolean,
|
||||
* notifyDesktop: boolean,
|
||||
* comparePeers: boolean,
|
||||
* chartPoints: number,
|
||||
* defaultExplore: string,
|
||||
* reduceMotion: boolean,
|
||||
* }} UiSettings */
|
||||
|
||||
/** @returns {UiSettings} */
|
||||
export function defaultSettings() {
|
||||
return {
|
||||
theme: 'dark',
|
||||
accent: 'teal',
|
||||
density: 'comfortable',
|
||||
sidebarCollapsed: false,
|
||||
notifyDesktop: true,
|
||||
comparePeers: false,
|
||||
chartPoints: 90,
|
||||
defaultExplore: 'system.io',
|
||||
reduceMotion: false,
|
||||
}
|
||||
}
|
||||
|
||||
function homeDir() {
|
||||
if (typeof globalThis !== 'undefined' && globalThis.Pear?.config?.storage) {
|
||||
return globalThis.Pear.config.storage
|
||||
}
|
||||
if (process.env.PEARDATA_HOME) return process.env.PEARDATA_HOME
|
||||
if (process.env.PEARDATA_STORAGE) return process.env.PEARDATA_STORAGE
|
||||
const base =
|
||||
process.env.XDG_CONFIG_HOME ||
|
||||
(process.platform === 'darwin'
|
||||
? path.join(os.homedir(), 'Library', 'Application Support')
|
||||
: path.join(os.homedir(), '.config'))
|
||||
return path.join(base, 'peardata')
|
||||
}
|
||||
|
||||
export function settingsPath() {
|
||||
return path.join(homeDir(), FILE_NAME)
|
||||
}
|
||||
|
||||
export function loadSettings() {
|
||||
const defaults = defaultSettings()
|
||||
try {
|
||||
const raw = JSON.parse(fs.readFileSync(settingsPath(), 'utf8'))
|
||||
return { ...defaults, ...(raw?.settings || raw || {}) }
|
||||
} catch {
|
||||
return defaults
|
||||
}
|
||||
}
|
||||
|
||||
/** @param {Partial<UiSettings>} patch */
|
||||
export function saveSettings(patch) {
|
||||
const next = { ...loadSettings(), ...patch }
|
||||
const dir = homeDir()
|
||||
try {
|
||||
fs.mkdirSync(dir, { recursive: true })
|
||||
fs.writeFileSync(
|
||||
settingsPath(),
|
||||
JSON.stringify({ version: VERSION, settings: next, updatedAt: new Date().toISOString() }, null, 2)
|
||||
)
|
||||
} catch {
|
||||
// ignore persistence failures in restricted runtimes
|
||||
}
|
||||
return next
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply settings to document body/html datasets (PearDock-style).
|
||||
* @param {UiSettings} s
|
||||
*/
|
||||
export function applySettingsToDom(s) {
|
||||
const html = document.documentElement
|
||||
const body = document.body
|
||||
html.dataset.theme = s.theme
|
||||
body.dataset.theme = s.theme
|
||||
body.dataset.accent = s.accent || 'teal'
|
||||
body.dataset.density = s.density || 'comfortable'
|
||||
body.dataset.reduceMotion = s.reduceMotion ? '1' : '0'
|
||||
body.classList.toggle('sidebar-collapsed', Boolean(s.sidebarCollapsed))
|
||||
}
|
||||
Reference in New Issue
Block a user