widget courrier refactor
This commit is contained in:
parent
2df21f5d44
commit
edd230712a
@ -3,11 +3,14 @@
|
|||||||
import { useEffect, useState, useRef } from "react";
|
import { useEffect, useState, useRef } from "react";
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { RefreshCw, MessageSquare } from "lucide-react";
|
import { RefreshCw, MessageSquare, Loader2 } from "lucide-react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
||||||
import { signIn, useSession } from "next-auth/react";
|
import { signIn, useSession } from "next-auth/react";
|
||||||
import { useTriggerNotification } from "@/hooks/use-trigger-notification";
|
import { useTriggerNotification } from "@/hooks/use-trigger-notification";
|
||||||
|
import { useUnifiedRefresh } from "@/hooks/use-unified-refresh";
|
||||||
|
import { REFRESH_INTERVALS } from "@/lib/constants/refresh-intervals";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
|
||||||
interface Message {
|
interface Message {
|
||||||
id: string;
|
id: string;
|
||||||
@ -40,19 +43,23 @@ export function Parole() {
|
|||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [refreshing, setRefreshing] = useState(false);
|
const [refreshing, setRefreshing] = useState(false);
|
||||||
|
const [unreadCount, setUnreadCount] = useState<number>(0);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { data: session, status } = useSession();
|
const { data: session, status } = useSession();
|
||||||
const { triggerNotificationRefresh } = useTriggerNotification();
|
const { triggerNotificationRefresh } = useTriggerNotification();
|
||||||
const lastUnreadCountRef = useRef<number>(-1); // Initialize to -1 to detect first load
|
const lastUnreadCountRef = useRef<number>(-1); // Initialize to -1 to detect first load
|
||||||
const isInitializedRef = useRef<boolean>(false);
|
const isInitializedRef = useRef<boolean>(false);
|
||||||
|
|
||||||
const fetchMessages = async (isRefresh = false) => {
|
const fetchMessages = async (forceRefresh = false) => {
|
||||||
try {
|
// Only show loading spinner on initial load, not on auto-refresh
|
||||||
if (isRefresh) {
|
if (!messages.length) {
|
||||||
setRefreshing(true);
|
setLoading(true);
|
||||||
}
|
}
|
||||||
|
setRefreshing(true);
|
||||||
|
setError(null);
|
||||||
|
|
||||||
const response = await fetch('/api/rocket-chat/messages' + (isRefresh ? '?refresh=true' : ''), {
|
try {
|
||||||
|
const response = await fetch('/api/rocket-chat/messages' + (forceRefresh ? '?refresh=true' : ''), {
|
||||||
cache: 'no-store',
|
cache: 'no-store',
|
||||||
next: { revalidate: 0 },
|
next: { revalidate: 0 },
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
@ -68,6 +75,9 @@ export function Parole() {
|
|||||||
// Utiliser le totalUnreadCount de l'API (plus fiable)
|
// Utiliser le totalUnreadCount de l'API (plus fiable)
|
||||||
const currentUnreadCount = data.totalUnreadCount || 0;
|
const currentUnreadCount = data.totalUnreadCount || 0;
|
||||||
|
|
||||||
|
// Update unread count state for badge display
|
||||||
|
setUnreadCount(currentUnreadCount);
|
||||||
|
|
||||||
// On initialise le count au premier chargement
|
// On initialise le count au premier chargement
|
||||||
if (!isInitializedRef.current) {
|
if (!isInitializedRef.current) {
|
||||||
isInitializedRef.current = true;
|
isInitializedRef.current = true;
|
||||||
@ -110,15 +120,32 @@ export function Parole() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Initial fetch on mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (status === 'authenticated') {
|
if (status === 'authenticated') {
|
||||||
fetchMessages();
|
fetchMessages(false); // Use cache on initial load
|
||||||
// Set up polling every 30 seconds with force refresh to bypass cache
|
|
||||||
const interval = setInterval(() => fetchMessages(true), 30000);
|
|
||||||
return () => clearInterval(interval);
|
|
||||||
}
|
}
|
||||||
}, [status]);
|
}, [status]);
|
||||||
|
|
||||||
|
// Integrate unified refresh for automatic polling
|
||||||
|
const { refresh } = useUnifiedRefresh({
|
||||||
|
resource: 'parole',
|
||||||
|
interval: REFRESH_INTERVALS.PAROLE, // 30 seconds
|
||||||
|
enabled: status === 'authenticated',
|
||||||
|
onRefresh: async () => {
|
||||||
|
await fetchMessages(false); // Use cache for auto-refresh
|
||||||
|
},
|
||||||
|
priority: 'high',
|
||||||
|
});
|
||||||
|
|
||||||
|
// Manual refresh handler (bypasses cache)
|
||||||
|
const handleManualRefresh = async (e?: React.MouseEvent) => {
|
||||||
|
if (e) {
|
||||||
|
e.stopPropagation();
|
||||||
|
}
|
||||||
|
await fetchMessages(true); // Force refresh, bypass cache
|
||||||
|
};
|
||||||
|
|
||||||
if (status === 'loading') {
|
if (status === 'loading') {
|
||||||
return (
|
return (
|
||||||
<Card className="transition-transform duration-500 ease-in-out transform hover:scale-105 bg-white/95 backdrop-blur-sm border-0 shadow-lg h-full">
|
<Card className="transition-transform duration-500 ease-in-out transform hover:scale-105 bg-white/95 backdrop-blur-sm border-0 shadow-lg h-full">
|
||||||
@ -172,41 +199,43 @@ export function Parole() {
|
|||||||
<CardTitle className="text-lg font-semibold text-gray-800 flex items-center gap-2">
|
<CardTitle className="text-lg font-semibold text-gray-800 flex items-center gap-2">
|
||||||
<MessageSquare className="h-5 w-5 text-gray-600" />
|
<MessageSquare className="h-5 w-5 text-gray-600" />
|
||||||
Parole
|
Parole
|
||||||
|
{unreadCount > 0 && (
|
||||||
|
<Badge variant="destructive" className="ml-1 text-xs">
|
||||||
|
{unreadCount}
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
</CardTitle>
|
</CardTitle>
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="icon"
|
size="icon"
|
||||||
onClick={(e) => {
|
onClick={handleManualRefresh}
|
||||||
e.stopPropagation();
|
|
||||||
fetchMessages(true);
|
|
||||||
}}
|
|
||||||
disabled={refreshing}
|
disabled={refreshing}
|
||||||
className={`${refreshing ? 'animate-spin' : ''} text-gray-600 hover:text-gray-900`}
|
className="h-7 w-7 p-0 hover:bg-gray-100/50 rounded-full"
|
||||||
>
|
>
|
||||||
<RefreshCw className="h-4 w-4" />
|
<RefreshCw className={`h-3.5 w-3.5 text-gray-600 ${refreshing ? 'animate-spin' : ''}`} />
|
||||||
</Button>
|
</Button>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent className="p-4">
|
<CardContent className="p-4">
|
||||||
{loading && <p className="text-center text-gray-500">Loading messages...</p>}
|
{loading && messages.length === 0 ? (
|
||||||
{error && (
|
<div className="text-center py-6 flex flex-col items-center">
|
||||||
<div className="text-center">
|
<Loader2 className="h-6 w-6 animate-spin text-gray-400 mb-2" />
|
||||||
<p className="text-red-500">Error: {error}</p>
|
<p className="text-gray-500">Chargement des messages...</p>
|
||||||
|
</div>
|
||||||
|
) : error ? (
|
||||||
|
<div className="text-center py-4">
|
||||||
|
<p className="text-red-500 text-sm mb-2">{error}</p>
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
onClick={(e) => {
|
onClick={handleManualRefresh}
|
||||||
e.stopPropagation();
|
|
||||||
fetchMessages(true);
|
|
||||||
}}
|
|
||||||
className="mt-2"
|
className="mt-2"
|
||||||
>
|
>
|
||||||
Try Again
|
Réessayer
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
)}
|
) : (
|
||||||
{!loading && !error && (
|
|
||||||
<div className="space-y-4 max-h-[400px] overflow-y-auto pr-1 scrollbar-thin scrollbar-thumb-gray-200 scrollbar-track-transparent">
|
<div className="space-y-4 max-h-[400px] overflow-y-auto pr-1 scrollbar-thin scrollbar-thumb-gray-200 scrollbar-track-transparent">
|
||||||
{messages.length === 0 ? (
|
{messages.length === 0 ? (
|
||||||
<p className="text-center text-gray-500">No messages found</p>
|
<p className="text-center text-gray-500 py-6">Aucun message</p>
|
||||||
) : (
|
) : (
|
||||||
messages.map((message) => (
|
messages.map((message) => (
|
||||||
<div key={message.id} className="flex items-start space-x-3 hover:bg-gray-50/50 p-3 rounded-lg transition-colors">
|
<div key={message.id} className="flex items-start space-x-3 hover:bg-gray-50/50 p-3 rounded-lg transition-colors">
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user