-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
362 lines (335 loc) · 11.8 KB
/
Copy pathApp.tsx
File metadata and controls
362 lines (335 loc) · 11.8 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import React, { useState } from "react";
import {
AppState,
ResumeData,
GitHubUser,
GitHubRepo,
EnrichedRepoData,
} from "./types";
import Hero from "./components/Hero";
import ResumeView from "./components/ResumeView";
import ErrorBoundary from "./components/ErrorBoundary";
import {
validateToken,
fetchAllRepos,
enrichRepoData,
scoreAndSortRepos,
} from "./services/githubService";
import { generateResumeFromGithub } from "./services/genaiService";
const App: React.FC = () => {
const [appState, setAppState] = useState<AppState>(AppState.IDLE);
const [resumeData, setResumeData] = useState<ResumeData | null>(null);
const [contextData, setContextData] = useState<{
user: GitHubUser;
repos: GitHubRepo[];
enrichedRepos: EnrichedRepoData[];
linkedinText: string;
geminiApiKey: string;
} | null>(null);
const [errorMessage, setErrorMessage] = useState<string>("");
// Track if generation is in progress to prevent double-submission
const isGeneratingRef = React.useRef<boolean>(false);
const handleStart = async (
githubToken: string,
geminiApiKey: string,
linkedinText: string,
) => {
// Prevent double-submission
if (isGeneratingRef.current) {
return;
}
isGeneratingRef.current = true;
try {
setErrorMessage("");
setAppState(AppState.FETCHING_GITHUB);
// 1. Validate User & Fetch Profile
const user = await validateToken(githubToken);
// 2. Fetch Repos (Public & Private)
const repos = await fetchAllRepos(githubToken, user.login);
if (repos.length === 0) {
throw new Error("No repositories found.");
}
// 3. Enrich top 20 repos with deep analysis (package.json, README, commits, etc.)
// Pass username to track user's actual contributions
const enrichedRepos = await enrichRepoData(
githubToken,
repos,
user.login,
20,
);
// 4. Score and sort repos intelligently (filters out org repos with minimal contribution)
const scoredRepos = scoreAndSortRepos(enrichedRepos, user.login);
setContextData({
user,
repos,
enrichedRepos: scoredRepos,
linkedinText,
geminiApiKey,
});
setAppState(AppState.ANALYZING_AI);
// 5. Generate Resume with Gemini (using scored & enriched data)
const generatedResume = await generateResumeFromGithub(
geminiApiKey,
user,
repos,
scoredRepos,
linkedinText,
);
setResumeData(generatedResume);
setAppState(AppState.READY);
} catch (error: any) {
console.error("Error generating resume:", error);
// Truncate long error messages
const message = error?.message || "An unexpected error occurred.";
setErrorMessage(
message.length > 300 ? message.slice(0, 300) + "..." : message,
);
setAppState(AppState.ERROR);
} finally {
isGeneratingRef.current = false;
}
};
const handleReset = () => {
setResumeData(null);
setContextData(null);
setAppState(AppState.IDLE);
};
const handleUpdateApiKey = (newApiKey: string) => {
if (contextData) {
setContextData({
...contextData,
geminiApiKey: newApiKey,
});
}
};
const handleImportResume = (importedData: any) => {
try {
// Generate UUID helper
const generateId = (): string => {
if (typeof crypto !== "undefined" && crypto.randomUUID) {
return crypto.randomUUID();
}
return `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
};
// Validate and sanitize education items
const validateEducation = (edu: any): any => {
if (!edu || typeof edu !== "object") return null;
return {
id: edu.id || generateId(),
institution:
typeof edu.institution === "string"
? edu.institution.slice(0, 200)
: "Institution",
degree:
typeof edu.degree === "string"
? edu.degree.slice(0, 200)
: "Degree",
location:
typeof edu.location === "string" ? edu.location.slice(0, 100) : "",
period: typeof edu.period === "string" ? edu.period.slice(0, 50) : "",
};
};
// Validate and sanitize certification items
const validateCertification = (cert: any): any => {
if (!cert || typeof cert !== "object") return null;
return {
id: cert.id || generateId(),
name:
typeof cert.name === "string"
? cert.name.slice(0, 200)
: "Certification",
issuer:
typeof cert.issuer === "string"
? cert.issuer.slice(0, 200)
: "Issuer",
date: typeof cert.date === "string" ? cert.date.slice(0, 50) : "",
credentialId:
typeof cert.credentialId === "string"
? cert.credentialId.slice(0, 100)
: undefined,
credentialUrl:
typeof cert.credentialUrl === "string"
? cert.credentialUrl.slice(0, 500)
: undefined,
};
};
// Validate and sanitize experience items
const validateExperience = (exp: any): any => {
if (!exp || typeof exp !== "object") return null;
return {
id: exp.id || generateId(),
company:
typeof exp.company === "string"
? exp.company.slice(0, 200)
: "Company",
title:
typeof exp.title === "string" ? exp.title.slice(0, 200) : "Title",
period: typeof exp.period === "string" ? exp.period.slice(0, 50) : "",
description: Array.isArray(exp.description)
? exp.description
.filter((d: any) => typeof d === "string")
.map((d: string) => d.slice(0, 500))
.slice(0, 10) // Max 10 bullets
: [],
};
};
// Validate and sanitize project items
const validateProject = (proj: any): any => {
if (!proj || typeof proj !== "object") return null;
return {
id: proj.id || generateId(),
name:
typeof proj.name === "string" ? proj.name.slice(0, 200) : "Project",
description: Array.isArray(proj.description)
? proj.description
.filter((d: any) => typeof d === "string")
.map((d: string) => d.slice(0, 500))
.slice(0, 10) // Max 10 bullets
: [],
technologies: Array.isArray(proj.technologies)
? proj.technologies
.filter((t: any) => typeof t === "string")
.map((t: string) => t.slice(0, 100))
.slice(0, 20) // Max 20 technologies
: [],
url: typeof proj.url === "string" ? proj.url.slice(0, 500) : "",
stars:
typeof proj.stars === "number" && proj.stars >= 0 ? proj.stars : 0,
};
};
// Validate and sanitize skills
const validateSkillArray = (arr: any): string[] => {
if (!Array.isArray(arr)) return [];
return arr
.filter(
(item: any) => typeof item === "string" && item.trim().length > 0,
)
.map((item: string) => item.slice(0, 100))
.slice(0, 50); // Max 50 skills per category
};
// Validate and sanitize imported data
const validatedData: ResumeData = {
fullName:
typeof importedData.fullName === "string"
? importedData.fullName.slice(0, 200)
: "Unknown",
title:
typeof importedData.title === "string"
? importedData.title.slice(0, 200)
: "Professional",
email:
typeof importedData.email === "string"
? importedData.email.slice(0, 200)
: "",
phone:
typeof importedData.phone === "string"
? importedData.phone.slice(0, 50)
: "",
githubUrl:
typeof importedData.githubUrl === "string"
? importedData.githubUrl.slice(0, 500)
: "",
website:
typeof importedData.website === "string"
? importedData.website.slice(0, 500)
: "",
location:
typeof importedData.location === "string"
? importedData.location.slice(0, 200)
: "",
linkedinUrl:
typeof importedData.linkedinUrl === "string"
? importedData.linkedinUrl.slice(0, 500)
: "",
education: Array.isArray(importedData.education)
? importedData.education
.map(validateEducation)
.filter((item: any): item is NonNullable<typeof item> => item !== null)
.slice(0, 10) // Max 10 education entries
: [],
certifications: Array.isArray(importedData.certifications)
? importedData.certifications
.map(validateCertification)
.filter((item: any): item is NonNullable<typeof item> => item !== null)
.slice(0, 20) // Max 20 certifications
: [],
skills: {
languages: validateSkillArray(importedData.skills?.languages),
frameworks: validateSkillArray(importedData.skills?.frameworks),
tools: validateSkillArray(importedData.skills?.tools),
},
projects: Array.isArray(importedData.projects)
? importedData.projects
.map(validateProject)
.filter((item: any): item is NonNullable<typeof item> => item !== null)
.slice(0, 20) // Max 20 projects
: [],
experience: Array.isArray(importedData.experience)
? importedData.experience
.map(validateExperience)
.filter((item: any): item is NonNullable<typeof item> => item !== null)
.slice(0, 20) // Max 20 experience entries
: [],
};
// Create mock context data for editing
// Note: geminiApiKey will be empty for imported resumes
// The ResumeView component will handle this by showing a prompt when AI features are used
const mockContext = {
user: {
login: validatedData.fullName.toLowerCase().replace(/\s+/g, "-"),
avatar_url: "",
html_url: validatedData.githubUrl || "",
name: validatedData.fullName,
company: null,
blog: validatedData.website || null,
location: validatedData.location || null,
email: validatedData.email || null,
bio: null,
public_repos: 0,
followers: 0,
following: 0,
created_at: new Date().toISOString(),
},
repos: [],
enrichedRepos: [],
linkedinText: "",
geminiApiKey: "", // Empty for imported resumes - will prompt user when AI features are used
};
setResumeData(validatedData);
setContextData(mockContext);
setAppState(AppState.READY);
} catch (error: any) {
console.error("Error importing resume:", error);
const errorMsg =
error?.message ||
"Failed to import resume. Please check the file format.";
setErrorMessage(
errorMsg.length > 300 ? errorMsg.slice(0, 300) + "..." : errorMsg,
);
setAppState(AppState.ERROR);
}
};
return (
<ErrorBoundary>
<div className="min-h-screen">
{appState !== AppState.READY && (
<Hero
onStart={handleStart}
onImportResume={handleImportResume}
state={appState}
errorMessage={errorMessage}
/>
)}
{appState === AppState.READY && resumeData && contextData && (
<ResumeView
data={resumeData}
context={contextData}
onReset={handleReset}
onUpdateApiKey={handleUpdateApiKey}
/>
)}
</div>
</ErrorBoundary>
);
};
export default App;