Skip to content

Commit 867ea28

Browse files
authored
Auto-sync GitHub after connecting to project (#756)
1 parent 03c200b commit 867ea28

File tree

1 file changed

+63
-26
lines changed

1 file changed

+63
-26
lines changed

src/components/GitHubConnector.tsx

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,33 @@ interface ConnectedGitHubConnectorProps {
4949
appId: number;
5050
app: any;
5151
refreshApp: () => void;
52+
triggerAutoSync?: boolean;
53+
onAutoSyncComplete?: () => void;
5254
}
5355

5456
interface UnconnectedGitHubConnectorProps {
5557
appId: number | null;
5658
folderName: string;
5759
settings: any;
5860
refreshSettings: () => void;
59-
refreshApp: () => void;
61+
handleRepoSetupComplete: () => void;
6062
expanded?: boolean;
6163
}
6264

6365
function ConnectedGitHubConnector({
6466
appId,
6567
app,
6668
refreshApp,
69+
triggerAutoSync,
70+
onAutoSyncComplete,
6771
}: ConnectedGitHubConnectorProps) {
6872
const [isSyncing, setIsSyncing] = useState(false);
6973
const [syncError, setSyncError] = useState<string | null>(null);
7074
const [syncSuccess, setSyncSuccess] = useState<boolean>(false);
7175
const [showForceDialog, setShowForceDialog] = useState(false);
7276
const [isDisconnecting, setIsDisconnecting] = useState(false);
7377
const [disconnectError, setDisconnectError] = useState<string | null>(null);
78+
const autoSyncTriggeredRef = useRef(false);
7479

7580
const handleDisconnectRepo = async () => {
7681
setIsDisconnecting(true);
@@ -85,32 +90,51 @@ function ConnectedGitHubConnector({
8590
}
8691
};
8792

88-
const handleSyncToGithub = async (force: boolean = false) => {
89-
setIsSyncing(true);
90-
setSyncError(null);
91-
setSyncSuccess(false);
92-
setShowForceDialog(false);
93+
const handleSyncToGithub = useCallback(
94+
async (force: boolean = false) => {
95+
setIsSyncing(true);
96+
setSyncError(null);
97+
setSyncSuccess(false);
98+
setShowForceDialog(false);
9399

94-
try {
95-
const result = await IpcClient.getInstance().syncGithubRepo(appId, force);
96-
if (result.success) {
97-
setSyncSuccess(true);
98-
} else {
99-
setSyncError(result.error || "Failed to sync to GitHub.");
100-
// If it's a push rejection error, show the force dialog
101-
if (
102-
result.error?.includes("rejected") ||
103-
result.error?.includes("non-fast-forward")
104-
) {
105-
// Don't show force dialog immediately, let user see the error first
100+
try {
101+
const result = await IpcClient.getInstance().syncGithubRepo(
102+
appId,
103+
force,
104+
);
105+
if (result.success) {
106+
setSyncSuccess(true);
107+
} else {
108+
setSyncError(result.error || "Failed to sync to GitHub.");
109+
// If it's a push rejection error, show the force dialog
110+
if (
111+
result.error?.includes("rejected") ||
112+
result.error?.includes("non-fast-forward")
113+
) {
114+
// Don't show force dialog immediately, let user see the error first
115+
}
106116
}
117+
} catch (err: any) {
118+
setSyncError(err.message || "Failed to sync to GitHub.");
119+
} finally {
120+
setIsSyncing(false);
107121
}
108-
} catch (err: any) {
109-
setSyncError(err.message || "Failed to sync to GitHub.");
110-
} finally {
111-
setIsSyncing(false);
122+
},
123+
[appId],
124+
);
125+
126+
// Auto-sync when triggerAutoSync prop is true
127+
useEffect(() => {
128+
if (triggerAutoSync && !autoSyncTriggeredRef.current) {
129+
autoSyncTriggeredRef.current = true;
130+
handleSyncToGithub(false).finally(() => {
131+
onAutoSyncComplete?.();
132+
});
133+
} else if (!triggerAutoSync) {
134+
// Reset the ref when triggerAutoSync becomes false
135+
autoSyncTriggeredRef.current = false;
112136
}
113-
};
137+
}, [triggerAutoSync]); // Only depend on triggerAutoSync to avoid unnecessary re-runs
114138

115139
return (
116140
<div className="w-full" data-testid="github-connected-repo">
@@ -268,7 +292,7 @@ function UnconnectedGitHubConnector({
268292
folderName,
269293
settings,
270294
refreshSettings,
271-
refreshApp,
295+
handleRepoSetupComplete,
272296
expanded,
273297
}: UnconnectedGitHubConnectorProps) {
274298
// --- Collapsible State ---
@@ -516,9 +540,10 @@ function UnconnectedGitHubConnector({
516540
appId,
517541
);
518542
}
543+
519544
setCreateRepoSuccess(true);
520545
setRepoCheckError(null);
521-
refreshApp();
546+
handleRepoSetupComplete();
522547
} catch (err: any) {
523548
setCreateRepoError(
524549
err.message ||
@@ -882,13 +907,25 @@ export function GitHubConnector({
882907
}: GitHubConnectorProps) {
883908
const { app, refreshApp } = useLoadApp(appId);
884909
const { settings, refreshSettings } = useSettings();
910+
const [pendingAutoSync, setPendingAutoSync] = useState(false);
911+
912+
const handleRepoSetupComplete = useCallback(() => {
913+
setPendingAutoSync(true);
914+
refreshApp();
915+
}, [refreshApp]);
916+
917+
const handleAutoSyncComplete = useCallback(() => {
918+
setPendingAutoSync(false);
919+
}, []);
885920

886921
if (app?.githubOrg && app?.githubRepo && appId) {
887922
return (
888923
<ConnectedGitHubConnector
889924
appId={appId}
890925
app={app}
891926
refreshApp={refreshApp}
927+
triggerAutoSync={pendingAutoSync}
928+
onAutoSyncComplete={handleAutoSyncComplete}
892929
/>
893930
);
894931
} else {
@@ -898,7 +935,7 @@ export function GitHubConnector({
898935
folderName={folderName}
899936
settings={settings}
900937
refreshSettings={refreshSettings}
901-
refreshApp={refreshApp}
938+
handleRepoSetupComplete={handleRepoSetupComplete}
902939
expanded={expanded}
903940
/>
904941
);

0 commit comments

Comments
 (0)