-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitlog.ts
More file actions
126 lines (109 loc) · 3.12 KB
/
Copy pathgitlog.ts
File metadata and controls
126 lines (109 loc) · 3.12 KB
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
import type { Ref } from 'vue'
import type { GitLog, GitLogFileEntry } from '../../types'
import { isClient } from '@vueuse/core'
import { useFrontmatter } from 'valaxy'
import { computed, ref, toRef } from 'vue'
import { useAddonGitLogConfig } from '../options'
/**
* Module-level cache to avoid re-fetching git-log.json on every route navigation.
*/
let cachedData: GitLogFileEntry | null = null
let fetchPromise: Promise<GitLogFileEntry> | null = null
function getGitLogData(): Promise<GitLogFileEntry> {
if (cachedData)
return Promise.resolve(cachedData)
if (!fetchPromise) {
const base = import.meta.env.BASE_URL || '/'
const url = `${base}git-log.json`
fetchPromise = fetch(url)
.then((res) => {
if (!res.ok)
throw new Error(`Failed to fetch ${url}: ${res.status} ${res.statusText}`)
return res.json()
})
.then((data: GitLogFileEntry) => {
cachedData = data
return data
})
.catch((err) => {
// Allow retry on next call
fetchPromise = null
throw err
})
}
return fetchPromise
}
export function useGitLog(): Ref<GitLog> {
const initGitLog: GitLog = {
contributors: [],
changeLog: [],
path: '',
}
if (!isClient)
return toRef(initGitLog)
const frontmatter = useFrontmatter<{ git_log: GitLog }>()
const gitLogOptions = useAddonGitLogConfig()
const gitLogData = ref<GitLog>(initGitLog)
const strategy = gitLogOptions.value.contributor?.strategy
const isPrebuilt = strategy === 'prebuilt'
const path = frontmatter.value.git_log?.path
if (isPrebuilt && path) {
getGitLogData()
.then((data) => {
if (data[path])
gitLogData.value = data[path]
})
.catch(error => console.error('valaxy-addon-git-log: Error fetching git-log.json:', error))
}
const gitLog = computed<GitLog>(() => {
if (isPrebuilt)
return gitLogData.value
return frontmatter.value.git_log || initGitLog
})
return gitLog
}
/**
* Extended version of useGitLog that also exposes loading and error state.
*/
export function useGitLogState() {
const initGitLog: GitLog = {
contributors: [],
changeLog: [],
path: '',
}
const isLoading = ref(false)
const error = ref<Error | null>(null)
if (!isClient) {
return {
data: toRef(initGitLog),
isLoading,
error,
}
}
const frontmatter = useFrontmatter<{ git_log: GitLog }>()
const gitLogOptions = useAddonGitLogConfig()
const gitLogData = ref<GitLog>(initGitLog)
const strategy = gitLogOptions.value.contributor?.strategy
const isPrebuilt = strategy === 'prebuilt'
const path = frontmatter.value.git_log?.path
if (isPrebuilt && path) {
isLoading.value = true
getGitLogData()
.then((data) => {
if (data[path])
gitLogData.value = data[path]
})
.catch((e) => {
error.value = e
})
.finally(() => {
isLoading.value = false
})
}
const data = computed<GitLog>(() => {
if (isPrebuilt)
return gitLogData.value
return frontmatter.value.git_log || initGitLog
})
return { data, isLoading, error }
}