perf: Optimize profile data fetching by preventing re-showing loading spinner and unnecessary re-fetches on subsequent session changes.

This commit is contained in:
pradeepkumar
2026-03-04 21:40:16 +05:30
parent be863d4ec2
commit 27d3a24605
2 changed files with 331 additions and 158 deletions

View File

@@ -54,12 +54,17 @@ export function ProfileSettingsForm({
const [successMessage, setSuccessMessage] = useState<string | null>(null);
// Store original data for cancel/reset functionality
const [originalData, setOriginalData] = useState<typeof formData | null>(null);
// Track if initial profile fetch is done to avoid re-showing loading spinner
const hasFetchedRef = useRef(false);
// Fetch profile data on mount based on user role
useEffect(() => {
const fetchProfile = async () => {
try {
setIsLoading(true);
// Only show loading spinner on initial fetch, not on session-triggered re-fetches
if (!hasFetchedRef.current) {
setIsLoading(true);
}
const role = (session?.user as any)?.role;
if (role === 'AGENT') {
@@ -122,10 +127,13 @@ export function ProfileSettingsForm({
}
} finally {
setIsLoading(false);
hasFetchedRef.current = true;
}
};
if (session) {
// Skip re-fetching on session changes after initial load (e.g. after updateSession in handleSave)
if (hasFetchedRef.current) return;
fetchProfile();
}
}, [session]);