Refactor KeepOnline logic and add useKeepOnline hook

Moved dashboard path check from KeepOnline to a new KeepOnlineWrapper component for cleaner separation of concerns. Introduced a reusable useKeepOnline hook to encapsulate the online status update logic. Updated layout to use KeepOnlineWrapper and simplified KeepOnline. Minor cleanup in broadcast-dialog.tsx.
This commit is contained in:
NotII
2025-08-01 15:27:52 +01:00
parent 5b78e4f86c
commit db1ebcb19d
5 changed files with 83 additions and 16 deletions

View File

@@ -3,9 +3,9 @@ import "./globals.css"
import { ThemeProvider } from "@/components/layout/theme-provider" import { ThemeProvider } from "@/components/layout/theme-provider"
import { Toaster } from "sonner" import { Toaster } from "sonner"
import type React from "react" import type React from "react"
import KeepOnline from "@/components/KeepOnline"
import { NotificationProvider } from "@/lib/notification-context" import { NotificationProvider } from "@/lib/notification-context"
import { Metadata, Viewport } from "next" import { Metadata, Viewport } from "next"
import KeepOnlineWrapper from "@/components/layout/KeepOnlineWrapper"
const inter = Inter({ subsets: ["latin"] }) const inter = Inter({ subsets: ["latin"] })
@@ -77,7 +77,7 @@ export default function RootLayout({
richColors richColors
position="top-right" position="top-right"
/> />
<KeepOnline /> <KeepOnlineWrapper />
{children} {children}
</NotificationProvider> </NotificationProvider>
</ThemeProvider> </ThemeProvider>

View File

@@ -1,21 +1,15 @@
"use client"; "use client";
import { useEffect } from "react"; import { useKeepOnline } from "@/hooks/useKeepOnline";
import { clientFetch } from "@/lib/api";
const KeepOnline = () => { const KeepOnline = () => {
useEffect(() => { useKeepOnline({
if(window.location.pathname.includes("/dashboard")){ interval: 1000 * 60 * 1, // 1 minute
const updateOnlineStatus = () => { enabled: true,
console.log("Updating online status..."); onError: (error) => {
clientFetch('/auth/me'); console.error("Keep online error:", error);
} }
});
const interval = setInterval(updateOnlineStatus, 1000*60*1);
return () => clearInterval(interval);
}
}, []);
return null; return null;
} }

View File

@@ -0,0 +1,17 @@
"use client";
import { usePathname } from "next/navigation";
import KeepOnline from "@/components/KeepOnline";
const KeepOnlineWrapper = () => {
const pathname = usePathname();
// Only render KeepOnline on dashboard pages
if (!pathname?.includes("/dashboard")) {
return null;
}
return <KeepOnline />;
};
export default KeepOnlineWrapper;

View File

@@ -29,7 +29,6 @@ export default function BroadcastDialog({ open, setOpen }: BroadcastDialogProps)
const handleImageSelect = (event: React.ChangeEvent<HTMLInputElement>) => { const handleImageSelect = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0]; const file = event.target.files?.[0];
if (file) { if (file) {
// Magic 10 MB Limit
if (file.size > 10 * 1024 * 1024) { if (file.size > 10 * 1024 * 1024) {
toast.error("Image size must be less than 10MB"); toast.error("Image size must be less than 10MB");
return; return;

57
hooks/useKeepOnline.ts Normal file
View File

@@ -0,0 +1,57 @@
import { useEffect, useRef } from "react";
import { clientFetch } from "@/lib/api";
interface UseKeepOnlineOptions {
interval?: number; // in milliseconds
enabled?: boolean;
onError?: (error: any) => void;
}
export const useKeepOnline = (options: UseKeepOnlineOptions = {}) => {
const {
interval = 1000 * 60 * 1, // 1 minute default
enabled = true,
onError
} = options;
const intervalRef = useRef<NodeJS.Timeout | null>(null);
useEffect(() => {
if (!enabled) {
return;
}
const updateOnlineStatus = async () => {
try {
console.log("Updating online status...");
await clientFetch('/auth/me');
} catch (error) {
console.error("Failed to update online status:", error);
onError?.(error);
}
};
// Initial call
updateOnlineStatus();
// Set up interval
intervalRef.current = setInterval(updateOnlineStatus, interval);
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
};
}, [enabled, interval, onError]);
return {
isActive: enabled && intervalRef.current !== null,
stop: () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
}
};
};