Merge branch 'pr/fix-git-fetch-toast-missfire(1141d542)'

This commit is contained in:
Alex Gleason committed 2025-12-22 13:01:13 -06:00
commit 5a55c09a67
1 file changed
+34 -47
+34 -47
View File
@@ -9,8 +9,10 @@ import { useEffect, useRef } from 'react';
interface FetchResult {
defaultBranch: string | null;
defaultBranchOid: string | null;
defaultBranchChanged: boolean;
currentBranch: string | null;
currentBranchOid: string | null;
currentBranchChanged: boolean;
fetchedAt: number;
hasChanges: boolean;
remoteUrl: string | null;
@@ -30,12 +32,10 @@ export function useGitFetch(projectId: string | null) {
const { toast } = useToast();
const queryClient = useQueryClient();
// Track previous state to detect changes
const previousStateRef = useRef<{
defaultBranchOid: string | null;
currentBranchOid: string | null;
dataUpdatedAt: number;
} | null>(null);
// Track which branches we've already notified about to avoid duplicate toasts
const notifiedBranchesRef = useRef<Set<string>>(new Set());
// Track if this is the first successful fetch to avoid showing toasts on initial load
const isFirstFetchRef = useRef(true);
const query = useQuery({
queryKey: ['git-fetch', projectId],
@@ -48,8 +48,10 @@ export function useGitFetch(projectId: string | null) {
return {
defaultBranch: null,
defaultBranchOid: null,
defaultBranchChanged: false,
currentBranch: null,
currentBranchOid: null,
currentBranchChanged: false,
fetchedAt: Date.now(),
hasChanges: false,
remoteUrl: null,
@@ -69,8 +71,10 @@ export function useGitFetch(projectId: string | null) {
return {
defaultBranch: null,
defaultBranchOid: null,
defaultBranchChanged: false,
currentBranch: null,
currentBranchOid: null,
currentBranchChanged: false,
fetchedAt: Date.now(),
hasChanges: false,
remoteUrl: null,
@@ -86,8 +90,10 @@ export function useGitFetch(projectId: string | null) {
return {
defaultBranch: null,
defaultBranchOid: null,
defaultBranchChanged: false,
currentBranch: null,
currentBranchOid: null,
currentBranchChanged: false,
fetchedAt: Date.now(),
hasChanges: false,
remoteUrl: null,
@@ -103,8 +109,10 @@ export function useGitFetch(projectId: string | null) {
return {
defaultBranch: null,
defaultBranchOid: null,
defaultBranchChanged: false,
currentBranch: null,
currentBranchOid: null,
currentBranchChanged: false,
fetchedAt: Date.now(),
hasChanges: false,
remoteUrl: null,
@@ -266,21 +274,20 @@ export function useGitFetch(projectId: string | null) {
beforeCurrentOid !== afterCurrentOid &&
currentBranch !== defaultBranch; // Don't double-report if they're the same
const hasChanges = defaultBranchChanged || currentBranchChanged;
console.log('Changes detected:', {
defaultBranchChanged,
currentBranchChanged,
hasChanges,
});
const result = {
defaultBranch,
defaultBranchOid: afterDefaultOid,
defaultBranchChanged,
currentBranch,
currentBranchOid: afterCurrentOid,
currentBranchChanged,
fetchedAt: Date.now(),
hasChanges,
hasChanges: defaultBranchChanged || currentBranchChanged,
remoteUrl,
};
@@ -301,48 +308,29 @@ export function useGitFetch(projectId: string | null) {
// Show toast notifications when changes are detected
useEffect(() => {
if (!query.data || !query.isSuccess || query.dataUpdatedAt === undefined) {
if (!query.data || !query.isSuccess) {
return;
}
const currentState = {
defaultBranchOid: query.data.defaultBranchOid,
currentBranchOid: query.data.currentBranchOid,
dataUpdatedAt: query.dataUpdatedAt,
};
// Initialize on first load or after component remount
if (previousStateRef.current === null) {
previousStateRef.current = currentState;
// Skip toasts on the first successful fetch (initial load)
if (isFirstFetchRef.current) {
isFirstFetchRef.current = false;
return;
}
const prevState = previousStateRef.current;
const { defaultBranch, defaultBranchChanged, currentBranch, currentBranchChanged, fetchedAt } = query.data;
// Skip if this is the same data we've already seen (same dataUpdatedAt timestamp)
// This prevents showing toasts when navigating back to a project with cached data
if (prevState.dataUpdatedAt === currentState.dataUpdatedAt) {
return;
}
// Detect default branch changes
const defaultBranchChanged =
prevState.defaultBranchOid !== null &&
currentState.defaultBranchOid !== null &&
prevState.defaultBranchOid !== currentState.defaultBranchOid;
// Detect current branch changes
const currentBranchChanged =
prevState.currentBranchOid !== null &&
currentState.currentBranchOid !== null &&
prevState.currentBranchOid !== currentState.currentBranchOid &&
query.data.currentBranch !== query.data.defaultBranch;
// Create a unique key for this fetch's branch changes
const defaultBranchKey = defaultBranch ? `${fetchedAt}-${defaultBranch}` : null;
const currentBranchKey = currentBranch ? `${fetchedAt}-${currentBranch}` : null;
// Show toast for default branch changes
if (defaultBranchChanged && query.data.defaultBranch) {
if (defaultBranchChanged && defaultBranch && defaultBranchKey && !notifiedBranchesRef.current.has(defaultBranchKey)) {
notifiedBranchesRef.current.add(defaultBranchKey);
toast({
title: 'Remote changes detected',
description: `The ${query.data.defaultBranch} branch has new commits available.`,
description: `The ${defaultBranch} branch has new commits available.`,
variant: 'default',
});
@@ -351,20 +339,19 @@ export function useGitFetch(projectId: string | null) {
}
// Show toast for current branch changes (if different from default)
if (currentBranchChanged && query.data.currentBranch) {
if (currentBranchChanged && currentBranch && currentBranchKey && !notifiedBranchesRef.current.has(currentBranchKey)) {
notifiedBranchesRef.current.add(currentBranchKey);
toast({
title: 'Remote changes detected',
description: `The ${query.data.currentBranch} branch has ${query.data.currentBranchOid === null ? 'been deleted' : 'new commits available'}.`,
description: `The ${currentBranch} branch has new commits available.`,
variant: 'default',
});
// Invalidate git status to show new ahead/behind counts
queryClient.invalidateQueries({ queryKey: ['git-status', projectId] });
}
// Update previous state
previousStateRef.current = currentState;
}, [query.data, query.isSuccess, query.dataUpdatedAt, toast, queryClient, projectId]);
}, [query.data, query.isSuccess, toast, queryClient, projectId]);
return query;
}