Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SubscriptionUsageCoordinator.tsx

56 lines2.2 KB

1import { useAtomValue } from "@effect/atom-react";
2import { Atom } from "effect/reactivity";
3import * as Linking from "expo-linking";
4import { useEffect } from "react";
5import { Platform } from "react-native";
6import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
7import { environmentCatalog } from "../connection/catalog";
8import { environmentPresentations } from "../state/presentation";
9import { publishSubscriptionUsage } from "./publishSubscriptionUsage";
10import { useSubscriptionUsage } from "./useSubscriptionUsage";
11import { buildSubscriptionUsageSnapshot } from "./subscriptionUsageSnapshot";
12import { createWidgetTheme } from "./widgetTheme";
13​
14// Isolate quota changes from the much busier thread/config presentation stream.
15const snapshotAtom = Atom.make((get) =>
16 buildSubscriptionUsageSnapshot(
17 get(environmentPresentations.presentationsWithProvidersAtom),
18 Linking.createURL("settings/usage", { queryParams: { tab: "limits" } }),
19 Platform.OS === "android" ? Infinity : 6,
20 ),
21).pipe(Atom.withEquality((a, b) => JSON.stringify(a) === JSON.stringify(b)));
22​
23export function SubscriptionUsageCoordinator() {
24 const {
25 themeMode,
26 themeAppearance,
27 themeVariablesByAppearance,
28 isReady: appearanceReady,
29 } = useAppearancePreferences();
30 const catalog = useAtomValue(environmentCatalog.catalogValueAtom);
31 const snapshot = useAtomValue(snapshotAtom);
32 useSubscriptionUsage(catalog.isReady);
33 const androidAppearance = Platform.OS === "android" ? themeAppearance : null;
34 useEffect(() => {
35 if (!catalog.isReady || !appearanceReady) return;
36 void Promise.resolve()
37 .then(() =>
38 publishSubscriptionUsage({
39 ...snapshot,
40 theme: createWidgetTheme(themeMode, themeVariablesByAppearance),
41 }),
42 )
43 .catch((error: unknown) => {
44 console.warn("Could not update subscription usage widget", error);
45 });
46 }, [
47 catalog.isReady,
48 appearanceReady,
49 snapshot,
50 themeMode,
51 themeVariablesByAppearance,
52 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Android stores resolved colors; System appearance changes must republish them.
53 androidAppearance,
54 ]);
55 return null;