Skip to content

Add loading state and toast notifications to PingTab #671

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
62 changes: 54 additions & 8 deletions client/src/components/PingTab.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,67 @@
import { TabsContent } from "@/components/ui/tabs";
import { Button } from "@/components/ui/button";
import { Bell } from "lucide-react";
import { useState } from "react";
import { useToast } from "@/lib/hooks/useToast";

interface PingTabProps {
onPingClick?: () => Promise<void> | void;
}

export default function PingTab({ onPingClick }: PingTabProps) {
const [isPinging, setIsPinging] = useState(false);
const { toast } = useToast();

const handlePingClick = async () => {
try {
setIsPinging(true);

// Show loading toast
toast({
title: "Ping Initiated",
description: "Sending ping request to the server...",
duration: 5000, // Auto-dismiss after 5 seconds
});

if (onPingClick) {
await onPingClick();
}

// Show success toast when ping completes
toast({
title: "Ping Successful",
description: "Server responded to ping request",
variant: "default",
duration: 5000, // Auto-dismiss after 5 seconds
});
} catch (error) {
// Show error toast if ping fails
toast({
title: "Ping Failed",
description:
error instanceof Error ? error.message : "Failed to ping server",
variant: "destructive",
duration: 5000, // Auto-dismiss after 5 seconds
});
} finally {
setIsPinging(false);
}
};

const PingTab = ({ onPingClick }: { onPingClick: () => void }) => {
return (
<TabsContent value="ping">
<div className="grid grid-cols-2 gap-4">
<div className="col-span-2 flex justify-center items-center">
<div className="col-span-2 flex flex-col justify-center items-center gap-4">
<Button
onClick={onPingClick}
className="font-bold py-6 px-12 rounded-full"
onClick={handlePingClick}
disabled={isPinging}
className="flex items-center gap-2 font-bold py-6 px-12 rounded-full"
>
Ping Server
<Bell className="h-4 w-4" />
{isPinging ? "Pinging..." : "Ping Server"}
</Button>
</div>
</div>
</TabsContent>
);
};

export default PingTab;
}