Skip to content

Improve vercel deployment fetching (auto-fetch) #758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 15 additions & 36 deletions src/components/VercelConnector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Globe } from "lucide-react";
import { IpcClient } from "@/ipc/ipc_client";
import { useSettings } from "@/hooks/useSettings";
import { useLoadApp } from "@/hooks/useLoadApp";
import { useVercelDeployments } from "@/hooks/useVercelDeployments";
import {
Select,
SelectContent,
Expand All @@ -14,7 +15,7 @@ import {
import {} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { App, VercelDeployment } from "@/ipc/ipc_types";
import { App } from "@/ipc/ipc_types";

interface VercelConnectorProps {
appId: number | null;
Expand Down Expand Up @@ -46,41 +47,19 @@ function ConnectedVercelConnector({
app,
refreshApp,
}: ConnectedVercelConnectorProps) {
const [isLoadingDeployments, setIsLoadingDeployments] = useState(false);
const [deploymentsError, setDeploymentsError] = useState<string | null>(null);
const [deployments, setDeployments] = useState<VercelDeployment[]>([]);
const [isDisconnecting, setIsDisconnecting] = useState(false);
const [disconnectError, setDisconnectError] = useState<string | null>(null);
const {
deployments,
isLoading: isLoadingDeployments,
error: deploymentsError,
getDeployments: handleGetDeployments,
disconnectProject,
isDisconnecting,
disconnectError,
} = useVercelDeployments(appId);

const handleDisconnectProject = async () => {
setIsDisconnecting(true);
setDisconnectError(null);
try {
await IpcClient.getInstance().disconnectVercelProject({ appId });
refreshApp();
} catch (err: any) {
setDisconnectError(err.message || "Failed to disconnect project.");
} finally {
setIsDisconnecting(false);
}
};

const handleGetDeployments = async () => {
setIsLoadingDeployments(true);
setDeploymentsError(null);

try {
const result = await IpcClient.getInstance().getVercelDeployments({
appId,
});
setDeployments(result);
} catch (err: any) {
setDeploymentsError(
err.message || "Failed to get deployments from Vercel.",
);
} finally {
setIsLoadingDeployments(false);
}
await disconnectProject();
refreshApp();
};

return (
Expand Down Expand Up @@ -154,7 +133,7 @@ function ConnectedVercelConnector({
Getting Deployments...
</>
) : (
"Get Deployments"
"Refresh Deployments"
)}
</Button>
<Button
Expand Down Expand Up @@ -193,7 +172,7 @@ function ConnectedVercelConnector({
{deployment.readyState}
</span>
<span className="text-sm text-gray-600 dark:text-gray-300">
{new Date(deployment.createdAt * 1000).toLocaleString()}
{new Date(deployment.createdAt).toLocaleString()}
</span>
</div>
<a
Expand Down
50 changes: 50 additions & 0 deletions src/hooks/useVercelDeployments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { IpcClient } from "@/ipc/ipc_client";
import { VercelDeployment } from "@/ipc/ipc_types";

export function useVercelDeployments(appId: number) {
const queryClient = useQueryClient();

const {
data: deployments = [],
isLoading,
error,
refetch,
} = useQuery<VercelDeployment[], Error>({
queryKey: ["vercel-deployments", appId],
queryFn: async () => {
const ipcClient = IpcClient.getInstance();
return ipcClient.getVercelDeployments({ appId });
},
// enabled: false, // Don't auto-fetch, only fetch when explicitly requested
});

const disconnectProjectMutation = useMutation<void, Error, void>({
mutationFn: async () => {
const ipcClient = IpcClient.getInstance();
return ipcClient.disconnectVercelProject({ appId });
},
onSuccess: () => {
// Clear deployments cache when project is disconnected
queryClient.removeQueries({ queryKey: ["vercel-deployments", appId] });
},
});

const getDeployments = async () => {
return refetch();
};

const disconnectProject = async () => {
return disconnectProjectMutation.mutateAsync();
};

return {
deployments,
isLoading,
error: error?.message || null,
getDeployments,
disconnectProject,
isDisconnecting: disconnectProjectMutation.isPending,
disconnectError: disconnectProjectMutation.error?.message || null,
};
}
12 changes: 2 additions & 10 deletions src/ipc/handlers/vercel_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
CreateVercelProjectParams,
IsVercelProjectAvailableParams,
SaveVercelAccessTokenParams,
VercelDeployment,
VercelProject,
} from "../ipc_types";
import { ConnectToExistingVercelProjectParams } from "../ipc_types";
Expand Down Expand Up @@ -400,16 +401,7 @@ async function handleConnectToExistingProject(
async function handleGetVercelDeployments(
event: IpcMainInvokeEvent,
{ appId }: GetVercelDeploymentsParams,
): Promise<
{
uid: string;
url: string;
state: string;
createdAt: number;
target: string;
readyState: string;
}[]
> {
): Promise<VercelDeployment[]> {
try {
const settings = readSettings();
const accessToken = settings.vercelAccessToken?.value;
Expand Down
18 changes: 9 additions & 9 deletions src/ipc/ipc_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,6 @@ export interface GetAppEnvVarsParams {
appId: number;
}

export interface VercelDeployment {
uid: string;
url: string;
state: string;
createdAt: number;
target: string;
readyState: string;
}

export interface ConnectToExistingVercelProjectParams {
projectId: string;
appId: number;
Expand All @@ -300,6 +291,15 @@ export interface GetVercelDeploymentsParams {
appId: number;
}

export interface VercelDeployment {
uid: string;
url: string;
state: string;
createdAt: number;
target: string;
readyState: string;
}

export interface DisconnectVercelProjectParams {
appId: number;
}
Expand Down
Loading