@carlosstenzel/opencode-git-sidebar 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,151 @@
1
+ import { createLatestGuard, errorMessage } from "./ui-helpers.js"
2
+
3
+ type Status = {
4
+ repo: string
5
+ branch: string
6
+ dirty: boolean
7
+ changes: number
8
+ ahead: number
9
+ behind: number
10
+ upstream: string
11
+ }
12
+
13
+ type Pull = {
14
+ number: number
15
+ title: string
16
+ url: string
17
+ branch: string
18
+ checks: "success" | "failure" | "none"
19
+ }
20
+
21
+ type Issue = {
22
+ number: number
23
+ title: string
24
+ url: string
25
+ author: string
26
+ labels: string[]
27
+ comments: number
28
+ date: string
29
+ }
30
+
31
+ type Branch = {
32
+ name: string
33
+ current: boolean
34
+ upstream: string
35
+ ahead: number
36
+ behind: number
37
+ hash: string
38
+ subject: string
39
+ date: string
40
+ }
41
+
42
+ type Commit = {
43
+ hash: string
44
+ short: string
45
+ subject: string
46
+ author: string
47
+ date: string
48
+ parents: string[]
49
+ refs: string[]
50
+ files: string[]
51
+ }
52
+
53
+ type Change = {
54
+ status: string
55
+ path: string
56
+ from?: string
57
+ additions?: number
58
+ deletions?: number
59
+ binary?: boolean
60
+ }
61
+
62
+ export type LoadDeps = {
63
+ directory: () => string | undefined
64
+ guard: ReturnType<typeof createLatestGuard>
65
+ rpc: {
66
+ status: (directory: string) => Promise<Status>
67
+ pulls: (directory: string) => Promise<Pull[]>
68
+ issues: (directory: string) => Promise<Issue[]>
69
+ branches: (directory: string, limit?: number) => Promise<Branch[]>
70
+ commits: (directory: string, limit?: number) => Promise<Commit[]>
71
+ changes: (directory: string) => Promise<Change[]>
72
+ }
73
+ setStatus: (s: Status | undefined) => void
74
+ setPulls: (p: Pull[]) => void
75
+ setIssues: (i: Issue[]) => void
76
+ setBranches: (b: Branch[]) => void
77
+ setCommits: (c: Commit[]) => void
78
+ setChanges: (c: Change[]) => void
79
+ setSelected: (s: Set<string>) => void
80
+ selected: () => Set<string>
81
+ setBusy: (b: boolean) => void
82
+ setLoaded: (l: boolean) => void
83
+ setError: (e: string | undefined) => void
84
+ lastDirRef: { current: string }
85
+ }
86
+
87
+ export async function load(
88
+ dir: string | undefined,
89
+ deps: LoadDeps,
90
+ ): Promise<void> {
91
+ const token = deps.guard.next()
92
+ const targetDir = dir ?? deps.directory()
93
+
94
+ if (!targetDir) {
95
+ deps.guard.invalidate()
96
+ deps.setStatus(undefined)
97
+ deps.setPulls([])
98
+ deps.setIssues([])
99
+ deps.setBranches([])
100
+ deps.setCommits([])
101
+ deps.setChanges([])
102
+ deps.setSelected(new Set<string>())
103
+ deps.setError(undefined)
104
+ deps.setBusy(false)
105
+ deps.setLoaded(true)
106
+ deps.lastDirRef.current = ""
107
+ return
108
+ }
109
+
110
+ if (targetDir !== deps.lastDirRef.current) {
111
+ deps.setStatus(undefined)
112
+ deps.setPulls([])
113
+ deps.setIssues([])
114
+ deps.setBranches([])
115
+ deps.setCommits([])
116
+ deps.setChanges([])
117
+ deps.setSelected(new Set<string>())
118
+ deps.setError(undefined)
119
+ deps.setLoaded(false)
120
+ deps.lastDirRef.current = targetDir
121
+ }
122
+
123
+ deps.setBusy(true)
124
+ try {
125
+ const [nextStatus, nextPulls, nextIssues, nextBranches, nextCommits, nextChanges] = (await Promise.all([
126
+ deps.rpc.status(targetDir),
127
+ deps.rpc.pulls(targetDir),
128
+ deps.rpc.issues(targetDir),
129
+ deps.rpc.branches(targetDir, 60),
130
+ deps.rpc.commits(targetDir, 20),
131
+ deps.rpc.changes(targetDir),
132
+ ])) as [Status, Pull[], Issue[], Branch[], Commit[], Change[]]
133
+ if (!deps.guard.isCurrent(token)) return
134
+ deps.setStatus(nextStatus)
135
+ deps.setPulls(nextPulls)
136
+ deps.setIssues(nextIssues)
137
+ deps.setBranches(nextBranches)
138
+ deps.setCommits(nextCommits)
139
+ deps.setChanges(nextChanges)
140
+ const valid = new Set(nextChanges.map((change) => change.path))
141
+ deps.setSelected(new Set([...deps.selected()].filter((path) => valid.has(path))))
142
+ deps.setLoaded(true)
143
+ deps.setError(undefined)
144
+ } catch (cause) {
145
+ if (!deps.guard.isCurrent(token)) return
146
+ deps.setError(errorMessage(cause))
147
+ deps.setLoaded(true)
148
+ } finally {
149
+ if (deps.guard.isCurrent(token)) deps.setBusy(false)
150
+ }
151
+ }