front end feature to showing pending connection request in sidebar.

This commit is contained in:
2026-06-21 13:26:55 +03:30
parent 4e2a9765bc
commit 2f99abbb9e
4 changed files with 68 additions and 1 deletions

View File

@@ -46,6 +46,11 @@ export const organizationApi = {
return response.data;
},
pendingIncomingCount: async (): Promise<{ success: boolean; data: { count: number } }> => {
const response = await apiClient.get('/organizations/connections/pending-count');
return response.data;
},
listInvitations: async (): Promise<{
success: boolean;
data: { items: OrganizationInvitationHistoryItemDto[] };

View File

@@ -0,0 +1,48 @@
'use client';
import { useCallback, useEffect, useState } from 'react';
import { usePathname } from 'next/navigation';
import { canViewTab } from '@/components/shared/permissions';
import { organizationApi } from '@/lib/api/organization';
import { useAuth } from '@/lib/hooks/useAuth';
/** Tell the sidebar badge to refetch after accept/decline on the organizations page. */
export function notifyPendingConnectionsChanged() {
window.dispatchEvent(new Event('pending-connections-changed'));
}
/**
* Sidebar-only: fetches GET /organizations/connections/pending-count.
* Independent from the organizations tab list API.
*/
export function usePendingConnectionsCount(): number {
const pathname = usePathname();
const { currentOrganization } = useAuth();
const [count, setCount] = useState(0);
const fetchCount = useCallback(async () => {
if (!currentOrganization?.id || !canViewTab(currentOrganization, 'TAB_ORGANIZATIONS_READ')) {
setCount(0);
return;
}
try {
const res = await organizationApi.pendingIncomingCount();
setCount(res.data.count);
} catch {
setCount(0);
}
}, [currentOrganization]);
useEffect(() => {
void fetchCount();
}, [fetchCount, pathname]);
useEffect(() => {
const onChanged = () => void fetchCount();
window.addEventListener('pending-connections-changed', onChanged);
return () => window.removeEventListener('pending-connections-changed', onChanged);
}, [fetchCount]);
return count;
}