feat: add netlify one-click deployment

This commit is contained in:
KevIsDev
2025-02-24 17:24:32 +00:00
parent 2a8472ed17
commit 4da13d1edc
9 changed files with 136 additions and 167 deletions

View File

@@ -1,6 +1,5 @@
import React, { useState, useEffect } from 'react';
import { logStore } from '~/lib/stores/logs';
import { classNames } from '~/utils/classNames';
import { motion } from 'framer-motion';
import { toast } from 'react-toastify';
import { GithubConnection } from './GithubConnection';
@@ -74,8 +73,6 @@ export default function ConnectionsTab() {
tokenType: 'classic',
});
const [isLoading, setIsLoading] = useState(true);
const [isConnecting, setIsConnecting] = useState(false);
const [isFetchingStats, setIsFetchingStats] = useState(false);
// Load saved connection on mount
useEffect(() => {
@@ -101,8 +98,6 @@ export default function ConnectionsTab() {
const fetchGitHubStats = async (token: string) => {
try {
setIsFetchingStats(true);
// Fetch repositories - only owned by the authenticated user
const reposResponse = await fetch(
'https://api.github.com/user/repos?sort=updated&per_page=10&affiliation=owner,organization_member,collaborator',
@@ -184,59 +179,9 @@ export default function ConnectionsTab() {
logStore.logError('Failed to fetch GitHub stats', { error });
toast.error('Failed to fetch GitHub statistics');
} finally {
setIsFetchingStats(false);
}
};
const fetchGithubUser = async (token: string) => {
try {
setIsConnecting(true);
const response = await fetch('https://api.github.com/user', {
headers: {
Authorization: `Bearer ${token}`,
},
});
if (!response.ok) {
throw new Error('Invalid token or unauthorized');
}
const data = (await response.json()) as GitHubUserResponse;
const newConnection: GitHubConnection = {
user: data,
token,
tokenType: connection.tokenType,
};
// Save connection
localStorage.setItem('github_connection', JSON.stringify(newConnection));
setConnection(newConnection);
// Fetch additional stats
await fetchGitHubStats(token);
toast.success('Successfully connected to GitHub');
} catch (error) {
logStore.logError('Failed to authenticate with GitHub', { error });
toast.error('Failed to connect to GitHub');
setConnection({ user: null, token: '', tokenType: 'classic' });
} finally {
setIsConnecting(false);
}
};
const handleConnect = async (event: React.FormEvent) => {
event.preventDefault();
await fetchGithubUser(connection.token);
};
const handleDisconnect = () => {
localStorage.removeItem('github_connection');
setConnection({ user: null, token: '', tokenType: 'classic' });
toast.success('Disconnected from GitHub');
};
if (isLoading) {
return <LoadingSpinner />;
}
@@ -259,9 +204,9 @@ export default function ConnectionsTab() {
<div className="grid grid-cols-1 gap-4">
{/* GitHub Connection */}
<GithubConnection/>
<GithubConnection />
{/* Netlify Connection */}
<NetlifyConnection/>
<NetlifyConnection />
</div>
</div>
);

View File

@@ -553,4 +553,3 @@ export function GithubConnection() {
</motion.div>
);
}

View File

@@ -28,7 +28,7 @@ export function NetlifyConnection() {
const sitesResponse = await fetch('https://api.netlify.com/api/v1/sites', {
headers: {
'Authorization': `Bearer ${token}`,
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
@@ -37,8 +37,8 @@ export function NetlifyConnection() {
throw new Error(`Failed to fetch sites: ${sitesResponse.status}`);
}
const sites = await sitesResponse.json() as NetlifySite[];
const sites = (await sitesResponse.json()) as NetlifySite[];
const currentState = netlifyConnection.get();
updateNetlifyConnection({
...currentState,
@@ -63,7 +63,7 @@ export function NetlifyConnection() {
try {
const response = await fetch('https://api.netlify.com/api/v1/user', {
headers: {
'Authorization': `Bearer ${connection.token}`,
Authorization: `Bearer ${connection.token}`,
'Content-Type': 'application/json',
},
});
@@ -72,12 +72,12 @@ export function NetlifyConnection() {
throw new Error('Invalid token or unauthorized');
}
const userData = await response.json() as NetlifyUser;
const userData = (await response.json()) as NetlifyUser;
updateNetlifyConnection({
user: userData,
token: connection.token,
});
await fetchNetlifyStats(connection.token);
toast.success('Successfully connected to Netlify');
} catch (error) {
@@ -105,7 +105,13 @@ export function NetlifyConnection() {
<div className="p-6 space-y-6">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<img className='w-5 h-5' height="24" width="24" crossOrigin='anonymous' src="https://cdn.simpleicons.org/netlify" />
<img
className="w-5 h-5"
height="24"
width="24"
crossOrigin="anonymous"
src="https://cdn.simpleicons.org/netlify"
/>
<h3 className="text-base font-medium text-bolt-elements-textPrimary">Netlify Connection</h3>
</div>
</div>
@@ -113,9 +119,7 @@ export function NetlifyConnection() {
{!connection.user ? (
<div className="space-y-4">
<div>
<label className="block text-sm text-bolt-elements-textSecondary mb-2">
Personal Access Token
</label>
<label className="block text-sm text-bolt-elements-textSecondary mb-2">Personal Access Token</label>
<input
type="password"
value={connection.token}
@@ -190,12 +194,12 @@ export function NetlifyConnection() {
</div>
<div className="flex items-center gap-4 p-4 bg-[#F8F8F8] dark:bg-[#1A1A1A] rounded-lg">
<img
src={connection.user.avatar_url}
referrerPolicy='no-referrer'
crossOrigin="anonymous"
alt={connection.user.full_name}
className="w-12 h-12 rounded-full border-2 border-[#00AD9F]"
<img
src={connection.user.avatar_url}
referrerPolicy="no-referrer"
crossOrigin="anonymous"
alt={connection.user.full_name}
className="w-12 h-12 rounded-full border-2 border-[#00AD9F]"
/>
<div>
<h4 className="text-sm font-medium text-bolt-elements-textPrimary">{connection.user.full_name}</h4>
@@ -231,7 +235,12 @@ export function NetlifyConnection() {
{site.name}
</h5>
<div className="flex items-center gap-2 mt-2 text-xs text-bolt-elements-textSecondary">
<a href={site.url} target="_blank" rel="noopener noreferrer" className="hover:text-[#00AD9F]">
<a
href={site.url}
target="_blank"
rel="noopener noreferrer"
className="hover:text-[#00AD9F]"
>
{site.url}
</a>
{site.published_deploy && (
@@ -270,4 +279,4 @@ export function NetlifyConnection() {
</div>
</motion.div>
);
}
}