Skip to content

Commit deaec40

Browse files
author
Denys Dinkevych
authored
Update App.tsx
1 parent b9037ce commit deaec40

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

src/App.tsx

+49-45
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import { Github, Heart } from 'lucide-react';
33
import SearchBar from './components/SearchBar';
44
import UserList from './components/UserList';
55
import { GitHubUser, SearchResults } from './types/github';
66
import { ThemeProvider, useTheme } from './contexts/ThemeContext';
77
import { ThemeSwitcher } from './components/ThemeSwitcher';
8-
import { createTokenAuth } from '@octokit/auth-token';
98
import { Octokit } from '@octokit/rest';
109
import { AuthProvider, useAuth } from './contexts/AuthContext';
1110

@@ -20,12 +19,19 @@ function AppContent() {
2019
const [error, setError] = useState<string | null>(null);
2120
const [loading, setLoading] = useState(false);
2221

22+
// Redirect user to home screen after signing in
23+
useEffect(() => {
24+
if (isAuthenticated) {
25+
window.history.replaceState({}, document.title, '/'); // Remove code from URL
26+
}
27+
}, [isAuthenticated]);
28+
2329
const searchUsers = async (page = 1) => {
2430
if (!keyword.trim()) return;
25-
31+
2632
setLoading(true);
2733
setError(null);
28-
34+
2935
try {
3036
const token = accessToken || import.meta.env.VITE_GITHUB_TOKEN;
3137
if (!token) {
@@ -38,9 +44,9 @@ function AppContent() {
3844
const searchResponse = await octokit.search.users({
3945
q: query,
4046
page,
41-
per_page: 10
47+
per_page: 10,
4248
});
43-
49+
4450
if (searchResponse.data.items.length === 0) {
4551
setUsers([]);
4652
setTotalCount(0);
@@ -53,12 +59,12 @@ function AppContent() {
5359
try {
5460
const [userData, mostUsedLanguage] = await Promise.all([
5561
octokit.users.getByUsername({ username: user.login }),
56-
getMostUsedLanguage(user.login, token)
62+
getMostUsedLanguage(user.login, token),
5763
]);
58-
64+
5965
return {
6066
...userData.data,
61-
most_used_language: mostUsedLanguage
67+
most_used_language: mostUsedLanguage,
6268
};
6369
} catch (err) {
6470
return {
@@ -71,7 +77,7 @@ function AppContent() {
7177
followers: 0,
7278
following: 0,
7379
company: null,
74-
most_used_language: null
80+
most_used_language: null,
7581
};
7682
}
7783
})
@@ -83,8 +89,8 @@ function AppContent() {
8389
} catch (err) {
8490
console.error('Search error:', err);
8591
setError(
86-
err instanceof Error
87-
? `Error: ${err.message}`
92+
err instanceof Error
93+
? `Error: ${err.message}`
8894
: 'An error occurred while searching. Please try again later.'
8995
);
9096
setUsers([]);
@@ -97,26 +103,25 @@ function AppContent() {
97103
const getMostUsedLanguage = async (username: string, token: string) => {
98104
try {
99105
const octokit = new Octokit({ auth: token });
100-
106+
101107
const { data: repos } = await octokit.repos.listForUser({
102108
username,
103109
sort: 'pushed',
104-
per_page: 10
110+
per_page: 10,
105111
});
106-
107-
const languages = repos.map(repo => repo.language).filter(Boolean);
108-
112+
113+
const languages = repos.map((repo) => repo.language).filter(Boolean);
114+
109115
if (languages.length === 0) return null;
110-
116+
111117
const languageCounts = languages.reduce<Record<string, number>>((acc, lang) => {
112118
if (lang) {
113119
acc[lang] = (acc[lang] || 0) + 1;
114120
}
115121
return acc;
116122
}, {});
117-
118-
return Object.entries(languageCounts)
119-
.sort(([, a], [, b]) => b - a)[0][0];
123+
124+
return Object.entries(languageCounts).sort(([, a], [, b]) => b - a)[0][0];
120125
} catch {
121126
return null;
122127
}
@@ -129,17 +134,13 @@ function AppContent() {
129134
const renderAuthButton = () => {
130135
return (
131136
<div className="flex flex-col items-end gap-2">
132-
{authError && (
133-
<div className="text-red-500 text-sm">
134-
{authError}
135-
</div>
136-
)}
137+
{authError && <div className="text-red-500 text-sm">{authError}</div>}
137138
{!isAuthenticated && (
138139
<button
139140
onClick={login}
140141
className={`px-4 py-2 rounded-md ${
141-
theme === 'dark'
142-
? 'bg-white text-black hover:bg-gray-200'
142+
theme === 'dark'
143+
? 'bg-white text-black hover:bg-gray-200'
143144
: 'bg-black text-white hover:bg-gray-800'
144145
}`}
145146
>
@@ -157,12 +158,14 @@ function AppContent() {
157158
<ThemeSwitcher />
158159
{renderAuthButton()}
159160
</div>
160-
161+
161162
<div className="flex flex-col items-center">
162163
<div className="text-center mb-12">
163164
<div className="flex items-center justify-center space-x-3 mb-4">
164165
<Github className={`w-10 h-10 ${theme === 'dark' ? 'text-white' : 'text-black'}`} />
165-
<h1 className={`text-4xl font-bold ${theme === 'dark' ? 'text-white' : 'text-black'} tracking-tight`}>
166+
<h1
167+
className={`text-4xl font-bold ${theme === 'dark' ? 'text-white' : 'text-black'} tracking-tight`}
168+
>
166169
GitHub Bio Search
167170
</h1>
168171
</div>
@@ -172,7 +175,7 @@ function AppContent() {
172175
</div>
173176

174177
<div className="w-full max-w-2xl">
175-
<SearchBar
178+
<SearchBar
176179
keyword={keyword}
177180
location={location}
178181
setKeyword={setKeyword}
@@ -182,7 +185,7 @@ function AppContent() {
182185
</div>
183186

184187
<div className="w-full mt-8">
185-
<UserList
188+
<UserList
186189
users={users}
187190
totalCount={totalCount}
188191
currentPage={currentPage}
@@ -194,26 +197,27 @@ function AppContent() {
194197
</div>
195198
</div>
196199
</div>
197-
198-
<footer className={`py-6 ${theme === 'dark' ? 'bg-black border-gray-800' : 'bg-white border-gray-100'} border-t`}>
200+
201+
<footer
202+
className={`py-6 ${theme === 'dark' ? 'bg-black border-gray-800' : 'bg-white border-gray-100'} border-t`}
203+
>
199204
<div className="container mx-auto px-4 text-center max-w-5xl">
200-
<p className={`text-sm ${theme === 'dark' ? 'text-gray-400' : 'text-gray-600'} flex items-center justify-center gap-2`}>
205+
<p
206+
className={`text-sm ${theme === 'dark' ? 'text-gray-400' : 'text-gray-600'} flex items-center justify-center gap-2`}
207+
>
201208
Built with{' '}
202-
<a
209+
<a
203210
href="https://cursor.sh"
204211
target="_blank"
205212
rel="noopener noreferrer"
206213
className={`hover:text-${theme === 'dark' ? 'white' : 'black'} transition-colors`}
207214
>
208215
Cursor
209-
</a>
210-
{' & '}
211-
<Heart
212-
className="w-4 h-4 text-red-500 inline-block animate-pulse fill-current"
213-
aria-label="love"
214-
/>
215-
{' by '}
216-
<a
216+
</a>{' '}
217+
&{' '}
218+
<Heart className="w-4 h-4 text-red-500 inline-block animate-pulse fill-current" aria-label="love" />{' '}
219+
by{' '}
220+
<a
217221
href="https://linkedin.com/in/sourcingdenis"
218222
target="_blank"
219223
rel="noopener noreferrer"
@@ -238,4 +242,4 @@ function App() {
238242
);
239243
}
240244

241-
export default App;
245+
export default App;

0 commit comments

Comments
 (0)