@opengovsg/confetti 0.0.10 → 0.0.12
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/build/index.d.ts +2 -1
- package/build/index.js +45 -11
- package/package.json +3 -1
package/build/index.d.ts
CHANGED
|
@@ -43,13 +43,14 @@ interface ConfettiProviderProps {
|
|
|
43
43
|
apiBaseUrl?: string;
|
|
44
44
|
isSurveyVisible?: boolean | ((_: {
|
|
45
45
|
lastRespondedAt: Date | null;
|
|
46
|
+
lastDismissedAt: Date | null;
|
|
46
47
|
}) => boolean);
|
|
47
48
|
}
|
|
48
49
|
declare const ConfettiProvider: ({
|
|
49
50
|
children,
|
|
50
51
|
surveyId,
|
|
51
52
|
publishableKey,
|
|
52
|
-
respondent,
|
|
53
|
+
respondent: respondentInput,
|
|
53
54
|
metadata,
|
|
54
55
|
isSurveyVisible: isSurveyVisibleInput,
|
|
55
56
|
apiBaseUrl
|
package/build/index.js
CHANGED
|
@@ -177,7 +177,12 @@ const publicPutResponseInputSchema = z.object({
|
|
|
177
177
|
])),
|
|
178
178
|
metadata: z.record(z.string().check(z.trim(), z.maxLength(1e3)), z.string().check(z.trim(), z.maxLength(1e3)))
|
|
179
179
|
});
|
|
180
|
-
const publicGetRespondentOutputSchema = z.object({
|
|
180
|
+
const publicGetRespondentOutputSchema = z.object({
|
|
181
|
+
lastRespondedAt: z.nullable(z.union([z.iso.datetime(), z.date()])),
|
|
182
|
+
lastViewedAt: z.nullable(z.union([z.iso.datetime(), z.date()])),
|
|
183
|
+
lastDismissedAt: z.nullable(z.union([z.iso.datetime(), z.date()]))
|
|
184
|
+
});
|
|
185
|
+
const publicPostRespondentEventInputSchema = z.object({ type: z.enum(["view", "dismiss"]) });
|
|
181
186
|
|
|
182
187
|
//#endregion
|
|
183
188
|
//#region src/core/logic/api.ts
|
|
@@ -194,15 +199,18 @@ const client = ky.create({
|
|
|
194
199
|
//#endregion
|
|
195
200
|
//#region src/core/ConfettiProvider.tsx
|
|
196
201
|
const ConfettiContext = createContext(null);
|
|
197
|
-
const ConfettiProvider = ({ children, surveyId, publishableKey, respondent, metadata = {}, isSurveyVisible: isSurveyVisibleInput, apiBaseUrl }) => {
|
|
202
|
+
const ConfettiProvider = ({ children, surveyId, publishableKey, respondent: respondentInput, metadata = {}, isSurveyVisible: isSurveyVisibleInput, apiBaseUrl }) => {
|
|
203
|
+
const hasTrackedViewRef = useRef(false);
|
|
198
204
|
const [survey, setSurvey] = useState();
|
|
199
205
|
const [lastRespondedAt, setLastRespondedAt] = useState();
|
|
206
|
+
const [lastDismissedAt, setLastDismissedAt] = useState();
|
|
200
207
|
const responseId = useMemo(() => crypto.randomUUID(), []);
|
|
201
208
|
const [internalRespondentId, setInternalRespondentId] = useLocalStorage(`cfti:${surveyId}:respondent`, "");
|
|
202
209
|
useEffect(() => {
|
|
203
210
|
if (internalRespondentId) return;
|
|
204
211
|
setInternalRespondentId(crypto.randomUUID());
|
|
205
212
|
}, [internalRespondentId, setInternalRespondentId]);
|
|
213
|
+
const respondent = useMemo(() => respondentInput || internalRespondentId, [respondentInput, internalRespondentId]);
|
|
206
214
|
const client$1 = useMemo(() => {
|
|
207
215
|
const properties = { headers: { "x-cfti-pk": publishableKey } };
|
|
208
216
|
if (apiBaseUrl) properties.prefixUrl = apiBaseUrl;
|
|
@@ -214,14 +222,16 @@ const ConfettiProvider = ({ children, surveyId, publishableKey, respondent, meta
|
|
|
214
222
|
if (!parseResult.success) return;
|
|
215
223
|
setSurvey(parseResult.data);
|
|
216
224
|
});
|
|
217
|
-
if (!respondent
|
|
218
|
-
client$1.get(`v1/cfti/${surveyId}/respondent/${respondent
|
|
225
|
+
if (!respondent) return;
|
|
226
|
+
client$1.get(`v1/cfti/${surveyId}/respondent/${respondent}`).json().then((data) => {
|
|
219
227
|
const parseResult = publicGetRespondentOutputSchema.safeParse(data);
|
|
220
228
|
if (!parseResult.success) {
|
|
221
229
|
setLastRespondedAt(null);
|
|
230
|
+
setLastDismissedAt(null);
|
|
222
231
|
return;
|
|
223
232
|
}
|
|
224
233
|
setLastRespondedAt(parseResult.data.lastRespondedAt ? new Date(parseResult.data.lastRespondedAt) : null);
|
|
234
|
+
setLastDismissedAt(parseResult.data.lastDismissedAt ? new Date(parseResult.data.lastDismissedAt) : null);
|
|
225
235
|
});
|
|
226
236
|
}, [
|
|
227
237
|
apiBaseUrl,
|
|
@@ -232,13 +242,12 @@ const ConfettiProvider = ({ children, surveyId, publishableKey, respondent, meta
|
|
|
232
242
|
]);
|
|
233
243
|
const submit = useCallback(async ({ answers }) => {
|
|
234
244
|
await client$1.put(`v1/cfti/${surveyId}/response/${responseId}`, { json: {
|
|
235
|
-
respondent
|
|
245
|
+
respondent,
|
|
236
246
|
data: answers,
|
|
237
247
|
metadata
|
|
238
248
|
} });
|
|
239
249
|
}, [
|
|
240
250
|
client$1,
|
|
241
|
-
internalRespondentId,
|
|
242
251
|
metadata,
|
|
243
252
|
respondent,
|
|
244
253
|
responseId,
|
|
@@ -246,13 +255,38 @@ const ConfettiProvider = ({ children, surveyId, publishableKey, respondent, meta
|
|
|
246
255
|
]);
|
|
247
256
|
const dismiss = useCallback(() => {
|
|
248
257
|
setSurvey(void 0);
|
|
249
|
-
|
|
250
|
-
|
|
258
|
+
client$1.post(`v1/cfti/${surveyId}/respondent/${respondent}/event`, { json: { type: "dismiss" } }).catch(() => {});
|
|
259
|
+
}, [
|
|
260
|
+
client$1,
|
|
261
|
+
respondent,
|
|
262
|
+
surveyId
|
|
263
|
+
]);
|
|
264
|
+
const isSurveyVisible = useMemo(() => {
|
|
251
265
|
if (isSurveyVisibleInput === void 0) return true;
|
|
252
266
|
if (typeof isSurveyVisibleInput === "boolean") return isSurveyVisibleInput;
|
|
253
|
-
if (lastRespondedAt === void 0) return false;
|
|
254
|
-
return isSurveyVisibleInput({
|
|
255
|
-
|
|
267
|
+
if (lastRespondedAt === void 0 || lastDismissedAt === void 0) return false;
|
|
268
|
+
return isSurveyVisibleInput({
|
|
269
|
+
lastRespondedAt,
|
|
270
|
+
lastDismissedAt
|
|
271
|
+
});
|
|
272
|
+
}, [
|
|
273
|
+
isSurveyVisibleInput,
|
|
274
|
+
lastRespondedAt,
|
|
275
|
+
lastDismissedAt
|
|
276
|
+
]);
|
|
277
|
+
useEffect(() => {
|
|
278
|
+
if (hasTrackedViewRef.current) return;
|
|
279
|
+
if (isSurveyVisible) {
|
|
280
|
+
hasTrackedViewRef.current = true;
|
|
281
|
+
client$1.post(`v1/cfti/${surveyId}/respondent/${respondent}/event`, { json: { type: "view" } }).catch(() => {});
|
|
282
|
+
}
|
|
283
|
+
}, [
|
|
284
|
+
client$1,
|
|
285
|
+
isSurveyVisible,
|
|
286
|
+
respondent,
|
|
287
|
+
surveyId
|
|
288
|
+
]);
|
|
289
|
+
if (!isSurveyVisible || !survey) return null;
|
|
256
290
|
return /* @__PURE__ */ jsx(ConfettiContext.Provider, {
|
|
257
291
|
value: {
|
|
258
292
|
submit,
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengovsg/confetti",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"description": "Widgets for Confetti",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"repository": "https://github.com/hfpg2026/confetti",
|
|
9
9
|
"homepage": "https://github.com/hfpg2026/confetti",
|
|
10
|
+
"main": "./build/index.js",
|
|
11
|
+
"types": "./build/index.d.ts",
|
|
10
12
|
"exports": {
|
|
11
13
|
".": {
|
|
12
14
|
"import": "./build/index.js",
|