-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseVCSData.ts
More file actions
151 lines (134 loc) · 4.27 KB
/
Copy pathuseVCSData.ts
File metadata and controls
151 lines (134 loc) · 4.27 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { useCallback, useState, useEffect } from 'react';
import { VCSProviderType } from '@bitcode/vcs-generics';
import { useVCSSelections } from './useVCSSelections';
import {
Account,
Repository,
IssueOrPR,
RepoFile
} from '../types/api';
import { fetchIssuesAndPRs as fetchIssuesAPI, fetchFiles as fetchFilesAPI, fetchAccounts } from '@/networking/api-client';
/**
* VCS data interface for product repository context and Finish delivery
* mechanism selectors.
*/
export interface VCSData {
provider: VCSProviderType | null;
accounts: Account[];
repositories: Repository[];
branches: string[];
commits: any[];
issuesAndPRs: IssueOrPR[];
files: RepoFile[];
defaultBranch: string | null;
isLoadingAccounts: boolean;
isLoadingRepos: boolean;
isLoadingBranches: boolean;
isLoadingCommits: boolean;
isLoadingIssues: boolean;
isLoadingFiles: boolean;
error: string | null;
}
/**
* VCS data hook with issues/PRs and files support for Read measurement,
* AssetPack synthesis evidence, and settle delivery mechanisms.
*/
export const useVCSData = () => {
// Add state management for the data
const [accounts, setAccounts] = useState<Account[]>([]);
const [issuesAndPRs, setIssuesAndPRs] = useState<IssueOrPR[]>([]);
const [files, setFiles] = useState<RepoFile[]>([]);
const [isLoadingAccounts, setIsLoadingAccounts] = useState(true);
const [isLoadingIssues, setIsLoadingIssues] = useState(false);
const [isLoadingFiles, setIsLoadingFiles] = useState(false);
const [error, setError] = useState<string | null>(null);
// State will be managed by the parent component
let currentProvider: VCSProviderType | null = null;
let currentAccount: string | null = null;
let currentRepo: string | null = null;
let currentBranch: string | null = null;
// Fetch accounts on mount
useEffect(() => {
const loadInitialAccounts = async () => {
try {
setIsLoadingAccounts(true);
const data = await fetchAccounts();
setAccounts(data);
} catch (err) {
// Silently handle - empty accounts is valid when not connected
setAccounts([]);
} finally {
setIsLoadingAccounts(false);
}
};
loadInitialAccounts();
}, []);
const setProvider = useCallback((provider: VCSProviderType | null) => {
currentProvider = provider;
}, []);
const loadAccounts = useCallback(async (provider: VCSProviderType) => {
// No-op - handled by useVCSSelections
}, []);
const loadRepositories = useCallback(async (provider: VCSProviderType, owner: string) => {
currentAccount = owner;
}, []);
const loadBranches = useCallback(async (provider: VCSProviderType, owner: string, repo: string) => {
currentRepo = repo;
}, []);
const loadCommits = useCallback(async (provider: VCSProviderType, owner: string, repo: string, branch: string) => {
currentBranch = branch;
}, []);
const loadIssuesAndPRs = useCallback(async (provider: VCSProviderType, owner: string, repo: string) => {
if (!owner || !repo) return;
setIsLoadingIssues(true);
setError(null);
try {
const data = await fetchIssuesAPI(owner, repo);
setIssuesAndPRs(data);
} catch (err) {
// Silently handle - empty issues is valid when not connected
setIssuesAndPRs([]);
} finally {
setIsLoadingIssues(false);
}
}, []);
const loadFiles = useCallback(async (provider: VCSProviderType, owner: string, repo: string, path: string = '') => {
if (!owner || !repo) return;
setIsLoadingFiles(true);
setError(null);
try {
const data = await fetchFilesAPI(owner, repo, path);
setFiles(data);
} catch (err) {
// Silently handle - empty files is valid
setFiles([]);
} finally {
setIsLoadingFiles(false);
}
}, []);
// Return VCS data interface with all fields
return {
provider: currentProvider,
accounts,
repositories: [],
branches: [],
commits: [],
issuesAndPRs,
files,
defaultBranch: null,
isLoadingAccounts,
isLoadingRepos: false,
isLoadingBranches: false,
isLoadingCommits: false,
isLoadingIssues,
isLoadingFiles,
error,
setProvider,
loadAccounts,
loadRepositories,
loadBranches,
loadCommits,
loadIssuesAndPRs,
loadFiles
};
};