-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseGithubUser.js
More file actions
51 lines (45 loc) · 1.21 KB
/
Copy pathuseGithubUser.js
File metadata and controls
51 lines (45 loc) · 1.21 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
import { useState } from 'react';
import { GithubService } from '../services/github';
export function useGithubUser() {
const [username, setUsername] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [user, setUser] = useState(null);
const [error, setError] = useState("");
const [hasSearched, setHasSearched] = useState(false);
const searchUser = async (e, forcedUsername = null) => {
if (e && e.preventDefault) e.preventDefault();
setError("");
const targetUsername = forcedUsername || username;
const trimmed = targetUsername ? targetUsername.trim() : "";
if (!trimmed) {
setError("Please enter a GitHub username");
return;
}
setHasSearched(true);
setIsLoading(true);
try {
const data = await GithubService.getUser(trimmed);
if (!data) {
setError("User not found");
setUser(null);
return;
}
setUser(data);
} catch (err) {
setError(err?.message || "An error occurred");
setUser(null);
} finally {
setIsLoading(false);
}
};
return {
username,
setUsername,
user,
setUser,
isLoading,
error,
hasSearched,
searchUser
};
}