Skip to content

Commit

Permalink
Loading status and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
radityaharya authored Apr 30, 2024
1 parent 10fe8da commit be58d42
Show file tree
Hide file tree
Showing 18 changed files with 437 additions and 104 deletions.
22 changes: 2 additions & 20 deletions src/app/auth/p/spotify/page.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
"use client";
import { signIn, useSession } from "next-auth/react";
import { useEffect } from "react";

const LoadingSVG = () => (
<svg
aria-hidden="true"
className="inline h-8 w-8 animate-spin fill-gray-600 text-gray-200 dark:fill-gray-300 dark:text-gray-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
);
import { LoadingSpinner } from "~/components/LoadingSpinner";

const SignInPage = () => {
const { data: session, status } = useSession();
Expand Down Expand Up @@ -54,7 +36,7 @@ const SignInPage = () => {
<div className="flex h-screen w-screen items-center justify-center bg-[#121212]">
<div className="flex flex-col items-center">
<div role="status">
<LoadingSVG />
<LoadingSpinner className="h-8 w-8" />
<span className="sr-only">Loading...</span>
</div>
<p className="mt-4 text-lg text-white">{renderContent()}</p>
Expand Down
22 changes: 2 additions & 20 deletions src/app/auth/providerButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { getProviders, signIn, useSession } from "next-auth/react";
import { redirect } from "next/navigation";
import { useEffect, useState } from "react";
import { LoadingSpinner } from "~/components/LoadingSpinner";
import { Button } from "~/components/ui/button";

type Provider = {
Expand Down Expand Up @@ -60,25 +61,6 @@ const popupCenter = ({
return newWindow;
};

const LoadingSVG = () => (
<svg
aria-hidden="true"
className="inline h-4 w-4 animate-spin fill-gray-600 text-gray-200 dark:fill-gray-300 dark:text-gray-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
);

// million-ignore
export default function ProviderButtons() {
const [providers, setProviders] = useState<Record<string, Provider>>({});
Expand Down Expand Up @@ -144,7 +126,7 @@ export default function ProviderButtons() {
)}
{status === "loading" ? (
<span>
<LoadingSVG />
<LoadingSpinner className="h-2 w-2" />
Authenticating...
</span>
) : (
Expand Down
24 changes: 21 additions & 3 deletions src/app/states/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ const useStore = create<RFState>((set, get) => ({
},
setEdges: (edges) => {
console.debug("setEdges", edges);
edges.forEach((edge) => {
edge.type = "smoothstep";
});
set({
edges: edges,
});
Expand Down Expand Up @@ -278,4 +275,25 @@ const useStore = create<RFState>((set, get) => ({
},
}));

type WorkflowRunState = {
workflowRun: QueueResponse | null;
setWorkflowRun: (workflowRun: QueueResponse) => void;
resetWorkflowRun: () => void;
};

export const workflowRunStore = create<WorkflowRunState>((set, get) => ({
workflowRun: null,
setWorkflowRun: (workflowRun) => {
console.info("workflowRun", workflowRun);
set({
workflowRun: workflowRun,
});
},
resetWorkflowRun: () => {
set({
workflowRun: null,
});
},
}));

export default useStore;
28 changes: 20 additions & 8 deletions src/app/utils/runWorkflow.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { toast } from "sonner";
import useStore from "../states/store";
import useStore, { workflowRunStore } from "../states/store";

export async function runWorkflow(workflow: WorkflowResponse) {
if (!workflow.id) {
throw new Error("Workflow ID is undefined");
}

const dryrun = useStore.getState().flowState.dryrun;
const dryrun = !useStore.getState().flowState.dryrun;
const id = workflow.id;
const promise = fetch(`/api/workflow/${id}/run${dryrun ? "?dryrun=true" : ""}`, {
method: "POST",
})
console.log("dryrun", dryrun);
const promise = fetch(
`/api/workflow/${id}/run${dryrun ? "?dryrun=true" : ""}`,
{
method: "POST",
},
)
.then((res) => {
return res.json();
})
Expand All @@ -30,6 +34,7 @@ export async function runWorkflow(workflow: WorkflowResponse) {
}

const jobId = job.id as string;
const workflowRun = workflowRunStore.getState();

const pollRequest = (id: string) => {
return new Promise((resolve, reject) => {
Expand All @@ -44,9 +49,12 @@ export async function runWorkflow(workflow: WorkflowResponse) {
if (data.error) {
clearInterval(interval);
reject(new Error(data.error));
} else if (data.status === "completed") {
clearInterval(interval);
resolve(data);
} else {
workflowRun.setWorkflowRun(data);
if (data.status === "completed") {
clearInterval(interval);
resolve(data);
}
}
})
.catch((err) => {
Expand All @@ -61,10 +69,14 @@ export async function runWorkflow(workflow: WorkflowResponse) {
toast.promise(pollRequest(jobId), {
loading: "Running workflow...",
success: () => {
setTimeout(() => {
workflowRun.resetWorkflowRun();
}, 5000);
return "Workflow completed successfully";
},
error: (data) => {
console.info("data on err", data);
workflowRun.resetWorkflowRun();
return "Failed running workflow: " + data;
},
});
Expand Down
23 changes: 2 additions & 21 deletions src/app/workflow/Builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ImperativePanelHandle } from "react-resizable-panels";
import { toast } from "sonner";

import { useRouter } from "next/navigation";
import { LoadingWithText } from "~/components/LoadingSpinner";
import { useWorkflowData } from "~/hooks/useWorkflowData";

function Builder({
Expand Down Expand Up @@ -195,28 +196,8 @@ function Builder({

const Loading = memo(() => (
<div className="flex h-full items-center justify-center">
<LoadingSVG />
<div className="ml-2">Loading...</div>
<LoadingWithText size={24} />
</div>
));

const LoadingSVG = () => (
<svg
aria-hidden="true"
className="inline h-4 w-4 animate-spin fill-gray-600 text-gray-200 dark:fill-gray-300 dark:text-gray-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
);

export default Builder;
2 changes: 1 addition & 1 deletion src/app/workflow/Flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export function App() {
);

const edgeOptions = {
animated: false,
animated: true,
};

const setDryRun = useCallback(
Expand Down
1 change: 0 additions & 1 deletion src/app/workflow/settingsDialog/tabs/History.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ const History = () => {
<Button onClick={refreshData}>Refresh</Button>
</div>
<div className="space-y-1">
{error && <div>Error loading history</div>}
<DataTable columns={columns} data={data} isLoading={isLoading} />
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/app/workflows/WorkflowTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ export function WorkflowTable({ workflows }: WorkflowTableProps) {
</Link>
</CardHeader>
<CardContent className="space-y-1">
{error && <div>Error loading history</div>}
<DataTable columns={columns} data={data} isLoading={isLoading} />
</CardContent>
</Card>
Expand Down
85 changes: 85 additions & 0 deletions src/components/LoadingSpinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"use client";

import { useEffect, useState } from "react";
import { cn } from "~/lib/utils";

const loadingMessages = [
"Tuning the instruments...",
"Setting up the stage...",
"Warming up the band...",
"Mixing the sound...",
"Rolling the drum...",
"Strumming the guitar...",
"Hitting the high notes...",
"Feeling the bass...",
"Cueing the lights...",
"Getting ready to rock...",
"Gathering beats",
"Mixing it up...",
"Blending the vibes...",
"Building the ultimate playlist...",
"Getting the party started...",
"Fine-tuning the flow...",
"Crafting your new jam...",
"Connecting the dots...",
"Creating something fresh...",
"Making music magic...",
"Getting ready to rock...",
"Spinning the tunes...",
];

export interface ExtendedSVGProps extends React.SVGProps<SVGSVGElement> {
size?: number;
className?: string;
}

export const LoadingSpinner = ({
size = 24,
className,
...props
}: ExtendedSVGProps) => {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
width={size}
height={size}
{...props}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="3"
strokeLinecap="round"
strokeLinejoin="round"
className={cn("animate-spin", className)}
>
<path d="M21 12a9 9 0 1 1-6.219-8.56" />
</svg>
);
};

export const LoadingWithText = ({
size = 24,
className,
...props
}: ExtendedSVGProps) => {
const randomIndex = Math.floor(Math.random() * loadingMessages.length);
const [loadingText, setLoadingText] = useState(loadingMessages[randomIndex]);

useEffect(() => {
const intervalId = setInterval(() => {
const newRandomIndex = Math.floor(Math.random() * loadingMessages.length);
setLoadingText(loadingMessages[newRandomIndex]);
}, 3000);

return () => clearInterval(intervalId);
}, []);

return (
<div className="flex flex-col items-center gap-2">
<LoadingSpinner size={size} className={className} {...props} />
<span className="text-sm" suppressHydrationWarning>
{loadingText}
</span>
</div>
);
};
Loading

0 comments on commit be58d42

Please sign in to comment.