@wasto/cms-components 1.5.11 → 1.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +82 -52
- package/dist/index.d.ts +82 -52
- package/dist/index.js +1149 -901
- package/dist/index.mjs +490 -239
- package/package.json +18 -3
- package/src/components/header/Header.tsx +68 -22
- package/src/components/locations-map/LocationsMapShell.tsx +24 -5
- package/src/components/page/Page.tsx +13 -3
- package/src/components/push/PushTokenRegistrar.tsx +77 -0
- package/src/contexts/HeaderConfigContext.tsx +29 -0
- package/src/contexts/LocalContext.tsx +32 -5
- package/src/contexts/WastoContext.tsx +2 -0
- package/src/helpers/nextCollection.ts +6 -3
- package/src/hooks/useCollectionCalendar.ts +8 -9
- package/src/index.ts +12 -0
- package/src/push/registerExpoPushToken.ts +133 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Platform } from "react-native";
|
|
2
|
+
|
|
3
|
+
type NotificationsModule = typeof import("expo-notifications");
|
|
4
|
+
|
|
5
|
+
type ExpoConstantsLike = {
|
|
6
|
+
expoConfig?: {
|
|
7
|
+
extra?: {
|
|
8
|
+
eas?: {
|
|
9
|
+
projectId?: string;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
} | null;
|
|
13
|
+
easConfig?: {
|
|
14
|
+
projectId?: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
let notificationsModule: NotificationsModule | null | undefined;
|
|
19
|
+
let constantsModule: ExpoConstantsLike | null | undefined;
|
|
20
|
+
let handlerConfigured = false;
|
|
21
|
+
|
|
22
|
+
function loadNotifications(): NotificationsModule | null {
|
|
23
|
+
if (notificationsModule !== undefined) return notificationsModule;
|
|
24
|
+
try {
|
|
25
|
+
// Optional peer — apps without the package still load cms-components.
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
27
|
+
notificationsModule = require("expo-notifications") as NotificationsModule;
|
|
28
|
+
} catch {
|
|
29
|
+
notificationsModule = null;
|
|
30
|
+
}
|
|
31
|
+
return notificationsModule;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function loadConstants(): ExpoConstantsLike | null {
|
|
35
|
+
if (constantsModule !== undefined) return constantsModule;
|
|
36
|
+
try {
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
38
|
+
const mod = require("expo-constants") as
|
|
39
|
+
| ExpoConstantsLike
|
|
40
|
+
| { default: ExpoConstantsLike };
|
|
41
|
+
constantsModule = "default" in mod && mod.default ? mod.default : (mod as ExpoConstantsLike);
|
|
42
|
+
} catch {
|
|
43
|
+
constantsModule = null;
|
|
44
|
+
}
|
|
45
|
+
return constantsModule;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function resolveEasProjectId(): string | null {
|
|
49
|
+
const Constants = loadConstants();
|
|
50
|
+
const fromExtra = Constants?.expoConfig?.extra?.eas?.projectId;
|
|
51
|
+
const fromEasConfig = Constants?.easConfig?.projectId;
|
|
52
|
+
const fromEnv =
|
|
53
|
+
typeof process !== "undefined"
|
|
54
|
+
? String(process.env.EXPO_PUBLIC_EAS_PROJECT_ID ?? "").trim()
|
|
55
|
+
: "";
|
|
56
|
+
const projectId = String(fromExtra || fromEasConfig || fromEnv || "").trim();
|
|
57
|
+
return projectId || null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type RegisterExpoPushTokenOptions = {
|
|
61
|
+
deviceId: string;
|
|
62
|
+
registerPushToken: (deviceId: string, token: string) => Promise<unknown>;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Request OS permission, fetch Expo push token, register it on the API.
|
|
67
|
+
* No-op on web, preview, missing peer, missing EAS projectId, or denied permission.
|
|
68
|
+
*/
|
|
69
|
+
export async function registerExpoPushToken(
|
|
70
|
+
options: RegisterExpoPushTokenOptions,
|
|
71
|
+
): Promise<string | null> {
|
|
72
|
+
if (Platform.OS === "web") return null;
|
|
73
|
+
|
|
74
|
+
const deviceId = options.deviceId?.trim();
|
|
75
|
+
if (!deviceId) return null;
|
|
76
|
+
|
|
77
|
+
const Notifications = loadNotifications();
|
|
78
|
+
if (!Notifications) {
|
|
79
|
+
if (__DEV__) {
|
|
80
|
+
console.warn(
|
|
81
|
+
"[wasto] expo-notifications is not installed; push registration skipped",
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const projectId = resolveEasProjectId();
|
|
88
|
+
if (!projectId) {
|
|
89
|
+
if (__DEV__) {
|
|
90
|
+
console.warn(
|
|
91
|
+
"[wasto] EAS projectId missing (extra.eas.projectId or EXPO_PUBLIC_EAS_PROJECT_ID); push registration skipped",
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!handlerConfigured) {
|
|
98
|
+
Notifications.setNotificationHandler({
|
|
99
|
+
handleNotification: async () => ({
|
|
100
|
+
shouldShowAlert: true,
|
|
101
|
+
shouldPlaySound: true,
|
|
102
|
+
shouldSetBadge: false,
|
|
103
|
+
shouldShowBanner: true,
|
|
104
|
+
shouldShowList: true,
|
|
105
|
+
}),
|
|
106
|
+
});
|
|
107
|
+
handlerConfigured = true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (Platform.OS === "android") {
|
|
111
|
+
await Notifications.setNotificationChannelAsync("default", {
|
|
112
|
+
name: "default",
|
|
113
|
+
importance: Notifications.AndroidImportance.MAX,
|
|
114
|
+
vibrationPattern: [0, 250, 250, 250],
|
|
115
|
+
lightColor: "#FF231F7C",
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const { status: existingStatus } = await Notifications.getPermissionsAsync();
|
|
120
|
+
let finalStatus = existingStatus;
|
|
121
|
+
if (existingStatus !== "granted") {
|
|
122
|
+
const { status } = await Notifications.requestPermissionsAsync();
|
|
123
|
+
finalStatus = status;
|
|
124
|
+
}
|
|
125
|
+
if (finalStatus !== "granted") return null;
|
|
126
|
+
|
|
127
|
+
const push = await Notifications.getExpoPushTokenAsync({ projectId });
|
|
128
|
+
const token = push?.data?.trim();
|
|
129
|
+
if (!token) return null;
|
|
130
|
+
|
|
131
|
+
await options.registerPushToken(deviceId, token);
|
|
132
|
+
return token;
|
|
133
|
+
}
|