All files / src/hooks useVaultLoader.ts

47.22% Statements 34/72
18.75% Branches 3/16
26.31% Functions 5/19
51.51% Lines 34/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138            22x 22x 22x   22x 7x 7x 7x   7x 7x   7x     7x 7x   7x 7x     7x     7x   7x         7x     22x 7x   7x     7x   7x             22x 7x     22x         22x       22x       22x                         22x               22x               22x       22x                               22x                              
import { useCallback, useEffect, useState } from 'react'
import { invoke } from '@tauri-apps/api/core'
import { isTauri, mockInvoke } from '../mock-tauri'
import type { VaultEntry, GitCommit, ModifiedFile } from '../types'
 
export function useVaultLoader(vaultPath: string) {
  const [entries, setEntries] = useState<VaultEntry[]>([])
  const [allContent, setAllContent] = useState<Record<string, string>>({})
  const [modifiedFiles, setModifiedFiles] = useState<ModifiedFile[]>([])
 
  useEffect(() => {
    setEntries([])
    setAllContent({})
    setModifiedFiles([])
 
    const loadVault = async () => {
      try {
        let result: VaultEntry[]
        Iif (isTauri()) {
          result = await invoke<VaultEntry[]>('list_vault', { path: vaultPath })
        } else {
          console.info('[mock] Using mock Tauri data for browser testing')
          result = await mockInvoke<VaultEntry[]>('list_vault', { path: vaultPath })
        }
        console.log(`Vault scan complete: ${result.length} entries found`)
        setEntries(result)
 
        let content: Record<string, string>
        Iif (isTauri()) {
          content = {}
        } else {
          content = await mockInvoke<Record<string, string>>('get_all_content', { path: vaultPath })
        }
        setAllContent(content)
      } catch (err) {
        console.warn('Vault scan failed:', err)
      }
    }
    loadVault()
  }, [vaultPath])
 
  const loadModifiedFiles = useCallback(async () => {
    try {
      let files: ModifiedFile[]
      Iif (isTauri()) {
        files = await invoke<ModifiedFile[]>('get_modified_files', { vaultPath })
      } else {
        files = await mockInvoke<ModifiedFile[]>('get_modified_files', {})
      }
      setModifiedFiles(files)
    } catch (err) {
      console.warn('Failed to load modified files:', err)
      setModifiedFiles([])
    }
  }, [vaultPath])
 
  useEffect(() => {
    loadModifiedFiles()
  }, [loadModifiedFiles])
 
  const addEntry = useCallback((entry: VaultEntry, content: string) => {
    setEntries((prev) => [entry, ...prev])
    setAllContent((prev) => ({ ...prev, [entry.path]: content }))
  }, [])
 
  const updateContent = useCallback((path: string, content: string) => {
    setAllContent((prev) => ({ ...prev, [path]: content }))
  }, [])
 
  const updateEntry = useCallback((path: string, patch: Partial<VaultEntry>) => {
    setEntries((prev) => prev.map((e) => e.path === path ? { ...e, ...patch } : e))
  }, [])
 
  const loadGitHistory = useCallback(async (path: string): Promise<GitCommit[]> => {
    try {
      if (isTauri()) {
        return await invoke<GitCommit[]>('get_file_history', { vaultPath, path })
      } else {
        return await mockInvoke<GitCommit[]>('get_file_history', { path })
      }
    } catch (err) {
      console.warn('Failed to load git history:', err)
      return []
    }
  }, [vaultPath])
 
  const loadDiffAtCommit = useCallback(async (path: string, commitHash: string): Promise<string> => {
    if (isTauri()) {
      return invoke<string>('get_file_diff_at_commit', { vaultPath, path, commitHash })
    } else {
      return mockInvoke<string>('get_file_diff_at_commit', { path, commitHash })
    }
  }, [vaultPath])
 
  const loadDiff = useCallback(async (path: string): Promise<string> => {
    if (isTauri()) {
      return invoke<string>('get_file_diff', { vaultPath, path })
    } else {
      return mockInvoke<string>('get_file_diff', { path })
    }
  }, [vaultPath])
 
  const isFileModified = useCallback((path: string): boolean => {
    return modifiedFiles.some((f) => f.path === path)
  }, [modifiedFiles])
 
  const commitAndPush = useCallback(async (message: string): Promise<string> => {
    if (isTauri()) {
      await invoke<string>('git_commit', { vaultPath, message })
      try {
        await invoke<string>('git_push', { vaultPath })
        return 'Committed and pushed'
      } catch {
        return 'Committed (push failed)'
      }
    } else {
      await mockInvoke<string>('git_commit', { message })
      await mockInvoke<string>('git_push', {})
      return 'Committed and pushed'
    }
  }, [vaultPath])
 
  return {
    entries,
    allContent,
    modifiedFiles,
    addEntry,
    updateEntry,
    updateContent,
    loadModifiedFiles,
    loadGitHistory,
    loadDiff,
    loadDiffAtCommit,
    isFileModified,
    commitAndPush,
  }
}