Files
tolaria/src/hooks/useViewMode.ts
lucaronin 6b13120fa6 feat: move vault UI config from localStorage to vault files
Add VaultConfig infrastructure (store, hook, migration) that persists
zoom, view mode, section visibility, tag/status colors, and property
display modes to config/ui.config.md in the vault instead of localStorage.

- New vaultConfigStore module with subscribe/notify pattern
- useVaultConfig hook loads config via Tauri, binds store, runs migration
- One-time silent migration from localStorage on first load
- Config type excluded from note search and unified search
- All hooks/utils updated to read/write through vault config store
- Tests updated to use vault config store instead of localStorage mocks

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 15:26:21 +01:00

42 lines
1.3 KiB
TypeScript

import { useState, useCallback, useEffect } from 'react'
import { getVaultConfig, updateVaultConfigField, subscribeVaultConfig } from '../utils/vaultConfigStore'
export type ViewMode = 'editor-only' | 'editor-list' | 'all'
function isViewMode(v: string | null | undefined): v is ViewMode {
return v === 'editor-only' || v === 'editor-list' || v === 'all'
}
function loadViewMode(): ViewMode {
const stored = getVaultConfig().view_mode
if (isViewMode(stored)) return stored
// Fallback to localStorage during initial load (before vault config is ready)
try {
const ls = localStorage.getItem('laputa-view-mode')
if (isViewMode(ls)) return ls
} catch { /* ignore */ }
return 'all'
}
export function useViewMode() {
const [viewMode, setViewModeState] = useState<ViewMode>(loadViewMode)
// Re-sync when vault config becomes available
useEffect(() => {
return subscribeVaultConfig(() => {
const stored = getVaultConfig().view_mode
if (isViewMode(stored)) setViewModeState(stored)
})
}, [])
const setViewMode = useCallback((mode: ViewMode) => {
setViewModeState(mode)
updateVaultConfigField('view_mode', mode)
}, [])
const sidebarVisible = viewMode === 'all'
const noteListVisible = viewMode === 'all' || viewMode === 'editor-list'
return { viewMode, setViewMode, sidebarVisible, noteListVisible }
}