Add trackEvent() calls for all user actions in the tracking plan: - Vault: vault_opened (with has_git, note_count), vault_switched - Notes: note_created (with has_type, creation_path), note_deleted, note_trashed, note_archived, note_favorited, note_unfavorited - Git: commit_made, sync_triggered - Search: search_used - Editor: raw_mode_toggled, wikilink_inserted - Types & Views: type_created, view_created - Telemetry: telemetry_opted_in, telemetry_opted_out All events gated by PostHog initialization (only fires when opted in). No PII in any event properties — counts, booleans, and enums only. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { useState, useCallback, useEffect } from 'react'
|
|
import { getVaultConfig, updateVaultConfigField, subscribeVaultConfig } from '../utils/vaultConfigStore'
|
|
import { trackEvent } from '../lib/telemetry'
|
|
|
|
interface UseRawModeParams {
|
|
activeTabPath: string | null
|
|
/** Flush pending WYSIWYG edits to disk before entering raw mode. */
|
|
onFlushPending?: () => Promise<boolean>
|
|
/** Called synchronously before raw mode is deactivated, so the caller can
|
|
* flush any debounced raw-editor content into tab state. */
|
|
onBeforeRawEnd?: () => void
|
|
}
|
|
|
|
function loadEditorMode(): boolean {
|
|
return getVaultConfig().editor_mode === 'raw'
|
|
}
|
|
|
|
/**
|
|
* Manages raw editor mode state.
|
|
* The mode preference persists across tab switches and is stored in vault config.
|
|
*/
|
|
export function useRawMode({ activeTabPath, onFlushPending, onBeforeRawEnd }: UseRawModeParams) {
|
|
const [rawEnabled, setRawEnabled] = useState(loadEditorMode)
|
|
|
|
// Re-sync when vault config becomes available (e.g. after initial load)
|
|
useEffect(() => {
|
|
return subscribeVaultConfig(() => {
|
|
const stored = getVaultConfig().editor_mode
|
|
setRawEnabled(stored === 'raw')
|
|
})
|
|
}, [])
|
|
|
|
const rawMode = rawEnabled && activeTabPath !== null
|
|
|
|
const handleToggleRaw = useCallback(async () => {
|
|
trackEvent('raw_mode_toggled')
|
|
if (rawEnabled) {
|
|
onBeforeRawEnd?.()
|
|
setRawEnabled(false)
|
|
updateVaultConfigField('editor_mode', 'preview')
|
|
} else {
|
|
await onFlushPending?.()
|
|
setRawEnabled(true)
|
|
updateVaultConfigField('editor_mode', 'raw')
|
|
}
|
|
}, [rawEnabled, onFlushPending, onBeforeRawEnd])
|
|
|
|
return { rawMode, handleToggleRaw }
|
|
}
|