Skip to content

Commit ba09a5b

Browse files
committed
refactor MainNav component: reorganize imports, enhance data fetching logic, and improve error handling
1 parent 3498f2d commit ba09a5b

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

app/[locale]/dashboard/components/main-nav.tsx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use client';
22

3-
import { useMetaKeyPress } from '@/hooks/use-meta-key-press';
4-
import { Session } from 'next-auth';
5-
import { signIn, signOut, useSession } from 'next-auth/react';
3+
import React, { useEffect, useState } from 'react';
64
import Image from 'next/image';
75
import Link from 'next/link';
86
import { usePathname } from 'next/navigation';
7+
import { useMetaKeyPress } from '@/hooks/use-meta-key-press';
8+
import { Session } from 'next-auth';
9+
import { signIn, signOut, useSession } from 'next-auth/react';
910
import {
1011
Avatar,
1112
Button,
@@ -19,13 +20,12 @@ import {
1920
IconButton,
2021
Popover,
2122
Spinner,
22-
Text
23+
Text,
2324
} from 'opub-ui';
24-
import React, { useEffect } from 'react';
2525

26-
import { Icons } from '@/components/icons';
2726
import { useDashboardStore } from '@/config/store';
2827
import { GraphQL } from '@/lib/api';
28+
import { Icons } from '@/components/icons';
2929
import { UserDetailsQryDoc } from '../[entityType]/[entitySlug]/schema';
3030
import { allOrganizationsListingDoc } from '../[entityType]/schema';
3131
import Sidebar from './sidebar';
@@ -62,41 +62,41 @@ export function MainNav({ hideSearch = false }) {
6262
const handleSignIn = async () => {
6363
try {
6464
// First attempt sign in
65-
await signIn('keycloak', {
65+
await signIn('keycloak', {
6666
redirect: true,
67-
callbackUrl: '/dashboard'
67+
callbackUrl: '/dashboard',
6868
});
69-
69+
7070
// The above will redirect automatically, no need for additional code
7171
// If redirect is needed manually, we can use:
7272
// router.push('/dashboard');
73-
7473
} catch (error) {
7574
console.error('Sign in error:', error);
7675
}
7776
};
7877

78+
const [hasFetched, setHasFetched] = useState(false);
79+
7980
useEffect(() => {
8081
const fetchData = async () => {
81-
if (session?.user) {
82+
if (session?.user && !hasFetched) {
8283
try {
83-
// Fetch both queries in parallel
8484
const [userDetailsRes, entityDetailsRes] = await Promise.all([
8585
GraphQL(UserDetailsQryDoc, {}, []),
86-
GraphQL(allOrganizationsListingDoc, {}, [])
86+
GraphQL(allOrganizationsListingDoc, {}, []),
8787
]);
8888

89-
// Update store with results
9089
setUserDetails(userDetailsRes);
9190
setAllEntityDetails(entityDetailsRes);
92-
} catch (queryError) {
93-
console.error('Error fetching data:', queryError);
91+
setHasFetched(true);
92+
} catch (err) {
93+
console.error('Error fetching user/org data:', err);
9494
}
9595
}
9696
};
9797

9898
fetchData();
99-
}, [session]);
99+
}, [session, hasFetched]);
100100

101101
if (isLoggingOut) {
102102
return <LogginOutPage />;
@@ -121,7 +121,6 @@ export function MainNav({ hideSearch = false }) {
121121
},
122122
];
123123

124-
125124
return (
126125
<nav className="p-4 lg:p-6">
127126
<div className="flex items-center justify-between gap-4 ">

0 commit comments

Comments
 (0)