-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub-callback.tsx
185 lines (163 loc) · 5.81 KB
/
github-callback.tsx
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
'use client';
import { useEffect, useRef, useState } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { useAuthContext } from '@/providers/AuthProvider';
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Github, Check, AlertCircle, Loader } from 'lucide-react';
// This component handles the GitHub App installation callback
export default function GitHubCallback() {
const router = useRouter();
const searchParams = useSearchParams();
const { token } = useAuthContext();
const [status, setStatus] = useState<'loading' | 'success' | 'error'>(
'loading'
);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const hasCalledBackend = useRef(false); // Add guard flag here
useEffect(() => {
// Extract installation ID from search params
const githubCode = searchParams.get('code');
const installationId = searchParams.get('installation_id');
const setupAction = searchParams.get('setup_action');
if (!token || hasCalledBackend.current) return; // Prevent multiple calls
console.log('GitHub Callback:', {
githubCode,
installationId,
setupAction,
});
// If there's no installation ID, this might be a cancellation
if (!installationId || !githubCode) {
// Check if it was canceled
if (searchParams.get('canceled') === 'true') {
setStatus('error');
setErrorMessage('GitHub App installation was canceled.');
return;
}
// Or if it's a delete operation
if (setupAction === 'delete') {
setStatus('success');
// We could call a different endpoint to remove the installationId
return;
}
setStatus('error');
setErrorMessage('No installation ID was provided.');
return;
}
hasCalledBackend.current = true;
// Call the backend directly to store the installation ID
const storeInstallation = async () => {
try {
// Use environment variable or hardcoded value for backend URL
const backendUrl =
process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8080';
const response = await fetch(`${backendUrl}/github/storeInstallation`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ installationId, githubCode }),
});
if (!response.ok) {
throw new Error(`Failed to store installation: ${response.status}`);
}
const data = await response.json();
if (data.success) {
setStatus('success');
} else {
throw new Error('Backend reported failure');
}
} catch (error) {
console.error('Error storing GitHub installation:', error);
setStatus('error');
setErrorMessage(
error.message || 'Failed to store GitHub installation.'
);
}
};
storeInstallation();
}, []);
// Function to handle redirect back to projects
const handleContinue = () => {
router.push('/'); // Change this to your desired redirect path
};
return (
<div className="container flex items-center justify-center min-h-screen py-8">
<Card className="w-full max-w-md">
<CardHeader>
<div className="flex items-center space-x-2">
<Github className="h-6 w-6" />
<CardTitle>GitHub Integration</CardTitle>
</div>
<CardDescription>
Completing your GitHub App installation
</CardDescription>
</CardHeader>
<CardContent className="flex flex-col items-center justify-center py-6">
{status === 'loading' && (
<>
<Loader className="h-12 w-12 text-primary animate-spin mb-4" />
<p className="text-center text-muted-foreground">
Finalizing your GitHub App installation...
</p>
</>
)}
{status === 'success' && (
<>
<div className="bg-green-100 dark:bg-green-900/20 rounded-full p-3 mb-4">
<Check className="h-10 w-10 text-green-600 dark:text-green-400" />
</div>
<h3 className="text-xl font-semibold mb-2">
Installation Complete!
</h3>
<p className="text-center text-muted-foreground mb-4">
Your GitHub App has been successfully installed. You can now
publish your projects to GitHub.
</p>
</>
)}
{status === 'error' && (
<>
<div className="bg-red-100 dark:bg-red-900/20 rounded-full p-3 mb-4">
<AlertCircle className="h-10 w-10 text-red-600 dark:text-red-400" />
</div>
<h3 className="text-xl font-semibold mb-2">
Installation Failed
</h3>
<p className="text-center text-muted-foreground mb-2">
We encountered an error while setting up your GitHub
integration.
</p>
{errorMessage && (
<p className="text-center text-sm text-red-500 dark:text-red-400">
{errorMessage}
</p>
)}
</>
)}
</CardContent>
<CardFooter>
<Button
className="w-full"
onClick={handleContinue}
disabled={status === 'loading'}
>
{status === 'success'
? 'Return to Projects'
: status === 'error'
? 'Try Again Later'
: 'Please Wait...'}
</Button>
</CardFooter>
</Card>
</div>
);
}