-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
362 lines (324 loc) · 12.3 KB
/
Copy pathApp.tsx
File metadata and controls
362 lines (324 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import React from "react";
import { Linking, View, Text, TouchableOpacity, ActivityIndicator, StyleSheet } from "react-native";
import { SafeAreaProvider } from "react-native-safe-area-context";
import {
NavigationContainer,
createNavigationContainerRef,
} from "@react-navigation/native";
import MainNavigator from "./src/navigation/MainNavigator";
import { UserContextProvider, useUser } from "./src/context/UserContext";
import { UsageContextProvider } from "./src/context/UsageContext";
import { PolicyContextProvider, usePolicyContext } from "./src/context/PolicyContext";
import { onAuthStateChanged } from "./src/auth/firebaseAuthService";
import { refreshBootstrap } from "./src/services/bootstrapService";
import { startTimerRealtimeTracking, stopTimerRealtimeTracking } from "./src/services/timerRealtimeService";
import { getOverrideBalanceAPI, useOverrideAPI } from "./src/services/overrideService";
import { grantTemporaryOverrideAccess, grantTemporaryWebsiteOverride } from "./src/services/appBlockerService";
import { resolveCurrentDeviceId } from "./src/services/currentDeviceService";
import { getPolicyPackageKey } from "./src/utils/policyMapper";
import { useFcm } from "./src/hooks/useFcm";
import { AppAlertProvider } from "./src/components/AppAlert";
import TermsAndConditionsScreen, { isTermsAcceptedForAccount } from "./src/screens/TermsAndConditionsScreen";
const navigationRef = createNavigationContainerRef<any>();
type OverrideLinkPayload = {
packageName: string;
appName: string;
};
function AppInner(): React.JSX.Element {
const { user, updateUser, setFirebaseUser, setAccountData, setIsLoading, clearUser } = useUser();
const { policies } = usePolicyContext();
const [bootstrapError, setBootstrapError] = React.useState<string | null>(null);
const [retrying, setRetrying] = React.useState(false);
const pendingOverrideRef = React.useRef<OverrideLinkPayload | null>(null);
const depsRef = React.useRef({ policies, user, updateUser, setAccountData, setFirebaseUser, setIsLoading, clearUser });
depsRef.current = { policies, user, updateUser, setAccountData, setFirebaseUser, setIsLoading, clearUser };
// const parseOverrideLink = React.useRef(
// (url: string): OverrideLinkPayload | null => {
// try {
// const parsed = new URL(url);
// const packageName = parsed.searchParams.get("package") || "";
// const appName = parsed.searchParams.get("appName") || packageName;
// if (parsed.hostname?.toLowerCase() === "override" && packageName) {
// return { packageName, appName };
// }
// } catch (_) { }
// const overrideMatch = url.match(/limitter:\/\/override\?(.*)$/i);
// if (!overrideMatch) return null;
// const params = new URLSearchParams(overrideMatch[1] || "");
// const packageName = params.get("package") || "";
// const appName = params.get("appName") || packageName;
// if (!packageName) return null;
// return { packageName, appName };
// },
// ).current;
const parseOverrideLink = React.useRef(
(url: string): OverrideLinkPayload | null => {
// Small helper: parses "?package=foo&appName=bar" into an object.
// We avoid URLSearchParams/URL because they have weak TypeScript types in React Native.
const parseQuery = (query: string): Record<string, string> => {
const result: Record<string, string> = {};
for (const pair of query.split('&')) {
if (!pair) continue;
const [rawKey, rawValue = ''] = pair.split('=');
try {
result[decodeURIComponent(rawKey)] = decodeURIComponent(rawValue);
} catch {
result[rawKey] = rawValue;
}
}
return result;
};
// Match both forms: "limitter://override?..." and anything ending in "/override?..."
const overrideMatch = url.match(/(?:^|\/\/)override\?(.*)$/i)
|| url.match(/limitter:\/\/override\?(.*)$/i);
if (!overrideMatch) return null;
const params = parseQuery(overrideMatch[1] || '');
const packageName = params.package || '';
const appName = params.appName || packageName;
if (!packageName) return null;
return { packageName, appName };
},
).current;
const openOverrideFlow = React.useRef(async (payload: OverrideLinkPayload) => {
if (!navigationRef.isReady()) {
pendingOverrideRef.current = payload;
return;
}
const { policies, user, updateUser } = depsRef.current;
// CHANGED: was navigating to SubscriptionPlansScreen; now goes straight to the new Buy Overrides page.
const goToPlans = () =>
navigationRef.navigate("BuyOverrides");
const goToConfirm = (targetType: string) =>
navigationRef.navigate("ConfirmOverrideScreen", {
packageName: payload.packageName,
appName: payload.appName,
targetType,
});
let available = 0;
try {
const balance = await getOverrideBalanceAPI();
available = balance?.totalAvailable ?? 0;
} catch {
goToPlans();
return;
}
if (available <= 0) {
goToPlans();
return;
}
const normalizedPkg = String(payload.packageName).trim().toLowerCase();
const matching = (Array.isArray(policies) ? policies : [])
.filter((p: any) => {
const k = getPolicyPackageKey(p);
return k === normalizedPkg || k === `website:${normalizedPkg}`;
})
.sort((a: any, b: any) => (b.created_at || 0) - (a.created_at || 0))[0];
const limitId = matching?.id as string | undefined;
const targetType = (matching?.target_type as string) || "app";
let deviceId: string | null = null;
try {
deviceId = await resolveCurrentDeviceId(user?.uid);
} catch { }
if (!limitId || !deviceId) {
goToConfirm(targetType);
return;
}
try {
await useOverrideAPI(limitId, deviceId);
updateUser({ overrides_left: Math.max(0, available - 1) });
if (targetType === "website") {
await grantTemporaryWebsiteOverride(payload.packageName, 5);
} else {
await grantTemporaryOverrideAccess(payload.packageName, payload.appName, 5);
}
const overriddenKey = targetType === "website"
? `website:${normalizedPkg}`
: normalizedPkg;
navigationRef.navigate("DashboardScreen", {
refreshAt: Date.now(),
justOverriddenPackage: overriddenKey,
});
} catch {
goToConfirm(targetType);
}
}).current;
const loadAccountRef = React.useRef(async () => {
try {
setBootstrapError(null);
const accountData = await refreshBootstrap();
depsRef.current.setAccountData(accountData);
} catch (err: any) {
setBootstrapError(err?.message || "Failed to load account. Check your connection.");
}
});
const fcmRefreshRef = React.useRef(async () => {
try {
const accountData = await refreshBootstrap();
depsRef.current.setAccountData(accountData);
} catch (err: any) {
console.warn(`[App] FCM-triggered refresh failed: ${err?.message || err}`);
}
});
const fcmHandlerRef = React.useRef(async (type: string) => {
if (type === "force_logout") {
const { clearUser } = depsRef.current;
try {
const { signOut: doSignOut } = await import("./src/auth/firebaseAuthService");
await doSignOut();
} catch {}
clearUser();
return;
}
await fcmRefreshRef.current();
});
useFcm(user?.device?.deviceId || null, (type) => fcmHandlerRef.current(type));
React.useEffect(() => {
const unsubscribe = onAuthStateChanged(async (fbUser) => {
const { setFirebaseUser, clearUser, setIsLoading } = depsRef.current;
if (fbUser && !fbUser.emailVerified) {
setFirebaseUser(null);
clearUser();
setIsLoading(false);
return;
}
setFirebaseUser(fbUser);
if (fbUser) {
await loadAccountRef.current();
} else {
clearUser();
setBootstrapError(null);
}
setIsLoading(false);
});
return unsubscribe;
}, []);
React.useEffect(() => {
startTimerRealtimeTracking();
return () => {
stopTimerRealtimeTracking();
};
}, []);
React.useEffect(() => {
const onDeepLink = ({ url }: { url: string }) => {
try {
const payload = parseOverrideLink(url);
if (payload) openOverrideFlow(payload);
} catch { }
};
const sub = Linking.addEventListener("url", onDeepLink);
Linking.getInitialURL().then((initialUrl) => {
if (initialUrl) onDeepLink({ url: initialUrl });
});
return () => sub.remove();
}, []);
// Account-scoped Terms & Conditions gate. Runs only AFTER the user is
// authenticated and an accountId is available (signup/login complete + bootstrap
// returned). Each accountId stores its own acceptance flag in AsyncStorage, so
// a different account on the same device sees the screen again, and the same
// account on a new device also sees it once. Once accepted, in-memory state
// skips the gate for the rest of this session; on next launch the persisted
// flag is read and the gate is bypassed.
const accountId = user?.accountId || '';
const [termsAccountId, setTermsAccountId] = React.useState<string | null>(null);
const [termsAccepted, setTermsAccepted] = React.useState<boolean>(false);
const [termsChecking, setTermsChecking] = React.useState<boolean>(false);
React.useEffect(() => {
if (!accountId) {
setTermsAccountId(null);
setTermsAccepted(false);
setTermsChecking(false);
return;
}
if (termsAccountId === accountId) return;
let cancelled = false;
setTermsChecking(true);
(async () => {
const accepted = await isTermsAcceptedForAccount(accountId);
if (cancelled) return;
setTermsAccountId(accountId);
setTermsAccepted(accepted);
setTermsChecking(false);
})();
return () => {
cancelled = true;
};
}, [accountId, termsAccountId]);
if (bootstrapError) {
return (
<View style={errorStyles.container}>
<Text style={errorStyles.title}>Connection Error</Text>
<Text style={errorStyles.message}>{bootstrapError}</Text>
<TouchableOpacity
style={[errorStyles.retryBtn, retrying && { opacity: 0.6 }]}
disabled={retrying}
onPress={async () => {
setRetrying(true);
await loadAccountRef.current();
setRetrying(false);
}}
>
{retrying ? (
<ActivityIndicator color="#FFFFFF" />
) : (
<Text style={errorStyles.retryText}>Retry</Text>
)}
</TouchableOpacity>
</View>
);
}
if (accountId && (termsChecking || termsAccountId !== accountId)) {
return (
<View style={gateStyles.loading}>
<ActivityIndicator size="large" color="#10B981" />
</View>
);
}
if (accountId && !termsAccepted) {
return (
<TermsAndConditionsScreen
accountId={accountId}
onAccept={() => setTermsAccepted(true)}
/>
);
}
return (
<NavigationContainer
ref={navigationRef}
onReady={() => {
if (pendingOverrideRef.current) {
const payload = pendingOverrideRef.current;
pendingOverrideRef.current = null;
openOverrideFlow(payload);
}
}}
>
<MainNavigator />
</NavigationContainer>
);
}
function App(): React.JSX.Element {
return (
<SafeAreaProvider>
<AppAlertProvider>
<UserContextProvider>
<UsageContextProvider>
<PolicyContextProvider>
<AppInner />
</PolicyContextProvider>
</UsageContextProvider>
</UserContextProvider>
</AppAlertProvider>
</SafeAreaProvider>
);
}
const errorStyles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 32, backgroundColor: '#FFFFFF' },
title: { fontSize: 22, fontWeight: '800', color: '#0F172A', marginBottom: 12 },
message: { fontSize: 15, color: '#64748B', textAlign: 'center', marginBottom: 32, lineHeight: 22 },
retryBtn: { backgroundColor: '#4F46E5', paddingHorizontal: 32, paddingVertical: 14, borderRadius: 12 },
retryText: { color: '#FFFFFF', fontWeight: '700', fontSize: 16 },
});
const gateStyles = StyleSheet.create({
loading: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#FFFFFF' },
});
export default App;