Skip to content

Commit fa71dbe

Browse files
Do not fetch github when no network connection is available
1 parent 11d23a9 commit fa71dbe

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

src/github/FetchHandler.tsx

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { useAppDispatch, useAppSelector } from '@/store';
2323

2424
import type { Release } from '@octokit/webhooks-types';
25+
import { useNetInfo } from '@react-native-community/netinfo';
2526

2627
const log = rootLogging.extend('FetchHandler');
2728

@@ -89,10 +90,18 @@ const FetchHandler: FC<PropsWithChildren> = ({ children }) => {
8990

9091
const githubApi = useGithub();
9192

93+
const { isConnected } = useNetInfo();
94+
9295
const fetchLatestReleaseHandler = useCallback(
9396
async (force?: boolean): Promise<boolean> => {
9497
if (!githubApi) return false;
9598

99+
if (!isConnected) {
100+
log.info('No internet connection, skipping fetch...');
101+
102+
return false;
103+
}
104+
96105
try {
97106
if ((latestReleaseUpdateOk || force) && enableFetchOpenDTUReleases) {
98107
dispatch(setLatestReleaseTimeout());
@@ -118,13 +127,25 @@ const FetchHandler: FC<PropsWithChildren> = ({ children }) => {
118127

119128
return false;
120129
},
121-
[dispatch, enableFetchOpenDTUReleases, githubApi, latestReleaseUpdateOk],
130+
[
131+
isConnected,
132+
dispatch,
133+
enableFetchOpenDTUReleases,
134+
githubApi,
135+
latestReleaseUpdateOk,
136+
],
122137
);
123138

124139
const fetchAllReleasesHandler = useCallback(
125140
async (force?: boolean): Promise<boolean> => {
126141
if (!githubApi) return false;
127142

143+
if (!isConnected) {
144+
log.info('No internet connection, skipping fetch...');
145+
146+
return false;
147+
}
148+
128149
try {
129150
if ((allReleasesUpdateOk || force) && enableFetchOpenDTUReleases) {
130151
dispatch(setReleasesTimeout());
@@ -150,13 +171,25 @@ const FetchHandler: FC<PropsWithChildren> = ({ children }) => {
150171

151172
return false;
152173
},
153-
[allReleasesUpdateOk, dispatch, enableFetchOpenDTUReleases, githubApi],
174+
[
175+
isConnected,
176+
allReleasesUpdateOk,
177+
dispatch,
178+
enableFetchOpenDTUReleases,
179+
githubApi,
180+
],
154181
);
155182

156183
const fetchLatestAppReleaseHandler = useCallback(
157184
async (force?: boolean): Promise<boolean> => {
158185
if (!githubApi) return false;
159186

187+
if (!isConnected) {
188+
log.info('No internet connection, skipping fetch...');
189+
190+
return false;
191+
}
192+
160193
try {
161194
if ((latestAppReleaseUpdateOk || force) && enableAppUpdates) {
162195
dispatch(setLatestAppReleaseTimeout());
@@ -182,12 +215,24 @@ const FetchHandler: FC<PropsWithChildren> = ({ children }) => {
182215

183216
return false;
184217
},
185-
[dispatch, enableAppUpdates, githubApi, latestAppReleaseUpdateOk],
218+
[
219+
isConnected,
220+
dispatch,
221+
enableAppUpdates,
222+
githubApi,
223+
latestAppReleaseUpdateOk,
224+
],
186225
);
187226

188227
const fetchHandler = useCallback(async () => {
189228
if (!githubApi) return;
190229

230+
if (!isConnected) {
231+
log.info('No internet connection, skipping fetch...');
232+
233+
return false;
234+
}
235+
191236
log.info('Fetching latest information from Github api...');
192237

193238
const latestReleaseSuccess = await fetchLatestReleaseHandler();
@@ -204,6 +249,7 @@ const FetchHandler: FC<PropsWithChildren> = ({ children }) => {
204249
);
205250
}
206251
}, [
252+
isConnected,
207253
fetchAllReleasesHandler,
208254
fetchLatestAppReleaseHandler,
209255
fetchLatestReleaseHandler,

0 commit comments

Comments
 (0)