@flamingo-stack/openframe-frontend-core 0.0.671 → 0.0.672-snapshot.20260928032919
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/chat-protocol/card-marker.d.ts +12 -0
- package/dist/chat-protocol/card-marker.d.ts.map +1 -1
- package/dist/chat-protocol/index.cjs +17 -0
- package/dist/chat-protocol/index.cjs.map +1 -1
- package/dist/chat-protocol/index.d.ts +1 -0
- package/dist/chat-protocol/index.d.ts.map +1 -1
- package/dist/chat-protocol/index.js +15 -1
- package/dist/chat-protocol/index.js.map +1 -1
- package/dist/{chunk-4PVY4JJN.js → chunk-2TQ64TG3.js} +3 -3
- package/dist/{chunk-FAJUQKWQ.cjs → chunk-3EIB5PNM.cjs} +76 -75
- package/dist/chunk-3EIB5PNM.cjs.map +1 -0
- package/dist/{chunk-FUXUJSPP.cjs → chunk-3UDBK2YG.cjs} +1 -1
- package/dist/chunk-3UDBK2YG.cjs.map +1 -0
- package/dist/{chunk-G64BNLDO.js → chunk-3X6WFYSV.js} +2 -2
- package/dist/{chunk-DUZEFACP.js → chunk-4UAIOXYC.js} +3 -3
- package/dist/{chunk-SNNE34OP.js → chunk-5KD5SSP6.js} +5 -5
- package/dist/{chunk-RKUCK6MN.js → chunk-6NGIPAQ6.js} +2 -2
- package/dist/{chunk-OG2BNJ2Q.cjs → chunk-6PPEG2N4.cjs} +389 -375
- package/dist/chunk-6PPEG2N4.cjs.map +1 -0
- package/dist/{chunk-NIYIK32F.js → chunk-6ZX6TFSS.js} +18 -4
- package/dist/{chunk-NIYIK32F.js.map → chunk-6ZX6TFSS.js.map} +1 -1
- package/dist/{chunk-6C6UUOH4.js → chunk-A77HP3N6.js} +8 -7
- package/dist/{chunk-6C6UUOH4.js.map → chunk-A77HP3N6.js.map} +1 -1
- package/dist/{chunk-YNGAJGYB.cjs → chunk-AXMNWMAU.cjs} +340 -6
- package/dist/chunk-AXMNWMAU.cjs.map +1 -0
- package/dist/{chunk-PLG6LP7E.cjs → chunk-BAEHCDPI.cjs} +49 -49
- package/dist/{chunk-PLG6LP7E.cjs.map → chunk-BAEHCDPI.cjs.map} +1 -1
- package/dist/{chunk-R22IA7MP.js → chunk-BBDEIE56.js} +4 -4
- package/dist/{chunk-PQ7YC5Y6.js → chunk-D5HO3RNF.js} +3 -3
- package/dist/{chunk-II66XWU5.cjs → chunk-D6IOMPAH.cjs} +3 -3
- package/dist/{chunk-II66XWU5.cjs.map → chunk-D6IOMPAH.cjs.map} +1 -1
- package/dist/{chunk-DAGKBFZN.cjs → chunk-EQIBWG5U.cjs} +33 -33
- package/dist/{chunk-DAGKBFZN.cjs.map → chunk-EQIBWG5U.cjs.map} +1 -1
- package/dist/{chunk-3PVOF3IB.js → chunk-EVB3EO5S.js} +337 -3
- package/dist/{chunk-3PVOF3IB.js.map → chunk-EVB3EO5S.js.map} +1 -1
- package/dist/{chunk-SJZPKIW6.cjs → chunk-G64BDYU5.cjs} +12 -12
- package/dist/{chunk-SJZPKIW6.cjs.map → chunk-G64BDYU5.cjs.map} +1 -1
- package/dist/{chunk-BP542DGQ.cjs → chunk-GLPGYU6H.cjs} +55 -28
- package/dist/chunk-GLPGYU6H.cjs.map +1 -0
- package/dist/{chunk-TLLTL6LB.js → chunk-JD4U7QMG.js} +1 -1
- package/dist/chunk-JD4U7QMG.js.map +1 -0
- package/dist/{chunk-ZRIVCLPR.js → chunk-JD5TKRCU.js} +1 -1
- package/dist/chunk-JD5TKRCU.js.map +1 -0
- package/dist/{chunk-YJWR7D6O.cjs → chunk-JKIM7U7N.cjs} +3 -3
- package/dist/{chunk-YJWR7D6O.cjs.map → chunk-JKIM7U7N.cjs.map} +1 -1
- package/dist/{chunk-K3O4D7LW.js → chunk-KUASHD7Z.js} +4 -4
- package/dist/{chunk-GF5TSQ6C.cjs → chunk-KXL534EV.cjs} +10 -10
- package/dist/{chunk-GF5TSQ6C.cjs.map → chunk-KXL534EV.cjs.map} +1 -1
- package/dist/{chunk-FHTNCWIP.js → chunk-KYVFU7VQ.js} +5 -5
- package/dist/{chunk-OYMOG544.js → chunk-KZAT6VWE.js} +5 -5
- package/dist/{chunk-D55IENZJ.cjs → chunk-LCMT2GZK.cjs} +1 -1
- package/dist/chunk-LCMT2GZK.cjs.map +1 -0
- package/dist/{chunk-IJIIL6WU.js → chunk-MVU7HF5A.js} +6 -6
- package/dist/{chunk-XGXHHSDU.cjs → chunk-NYDFVUMH.cjs} +22 -22
- package/dist/{chunk-XGXHHSDU.cjs.map → chunk-NYDFVUMH.cjs.map} +1 -1
- package/dist/{chunk-STLBB6PF.js → chunk-O75Y5O3U.js} +35 -8
- package/dist/chunk-O75Y5O3U.js.map +1 -0
- package/dist/{chunk-DAWYS25M.cjs → chunk-PDNGWDJF.cjs} +17 -17
- package/dist/{chunk-DAWYS25M.cjs.map → chunk-PDNGWDJF.cjs.map} +1 -1
- package/dist/{chunk-T3ACDOVD.cjs → chunk-VRZZCL4R.cjs} +7 -7
- package/dist/{chunk-T3ACDOVD.cjs.map → chunk-VRZZCL4R.cjs.map} +1 -1
- package/dist/{chunk-XH4MRTZM.cjs → chunk-WQFT7GOC.cjs} +5 -5
- package/dist/{chunk-XH4MRTZM.cjs.map → chunk-WQFT7GOC.cjs.map} +1 -1
- package/dist/{chunk-YIDNSRPL.cjs → chunk-XGBFQ3YV.cjs} +31 -31
- package/dist/{chunk-YIDNSRPL.cjs.map → chunk-XGBFQ3YV.cjs.map} +1 -1
- package/dist/{chunk-UH3BL2OT.js → chunk-Y7TZHPS2.js} +2 -2
- package/dist/{chunk-EIAVE6SE.cjs → chunk-YKKW67A3.cjs} +10 -10
- package/dist/{chunk-EIAVE6SE.cjs.map → chunk-YKKW67A3.cjs.map} +1 -1
- package/dist/components/case-studies/index.cjs +18 -17
- package/dist/components/case-studies/index.cjs.map +1 -1
- package/dist/components/case-studies/index.js +7 -6
- package/dist/components/case-studies/index.js.map +1 -1
- package/dist/components/case-studies/share-experience-section.d.ts.map +1 -1
- package/dist/components/chat/index.cjs +2 -2
- package/dist/components/chat/index.js +1 -1
- package/dist/components/contact/contact-form.d.ts +6 -1
- package/dist/components/contact/contact-form.d.ts.map +1 -1
- package/dist/components/contact/index.cjs +6 -6
- package/dist/components/contact/index.js +5 -5
- package/dist/components/docs/index.cjs +9 -9
- package/dist/components/docs/index.js +8 -8
- package/dist/components/embeds/index.cjs +6 -6
- package/dist/components/embeds/index.js +5 -5
- package/dist/components/faq/index.cjs +6 -6
- package/dist/components/faq/index.js +5 -5
- package/dist/components/features/index.cjs +5 -5
- package/dist/components/features/index.js +4 -4
- package/dist/components/features/waitlist-form.d.ts +3 -1
- package/dist/components/features/waitlist-form.d.ts.map +1 -1
- package/dist/components/help-center-pages/index.cjs +77 -76
- package/dist/components/help-center-pages/index.cjs.map +1 -1
- package/dist/components/help-center-pages/index.js +17 -16
- package/dist/components/help-center-pages/index.js.map +1 -1
- package/dist/components/help-center-pages/trust-center-sections.d.ts.map +1 -1
- package/dist/components/index.cjs +43 -43
- package/dist/components/index.js +15 -15
- package/dist/components/meeting-scheduler/booking-form.d.ts +4 -1
- package/dist/components/meeting-scheduler/booking-form.d.ts.map +1 -1
- package/dist/components/meeting-scheduler/index.cjs +73 -39
- package/dist/components/meeting-scheduler/index.cjs.map +1 -1
- package/dist/components/meeting-scheduler/index.d.ts.map +1 -1
- package/dist/components/meeting-scheduler/index.js +44 -10
- package/dist/components/meeting-scheduler/index.js.map +1 -1
- package/dist/components/navigation/index.cjs +8 -8
- package/dist/components/navigation/index.js +7 -7
- package/dist/components/onboarding-guides/index.cjs +8 -8
- package/dist/components/onboarding-guides/index.js +7 -7
- package/dist/components/tickets/help-center-create-form.d.ts.map +1 -1
- package/dist/components/tickets/index.cjs +9 -9
- package/dist/components/tickets/index.js +8 -8
- package/dist/components/ui/index.cjs +5 -5
- package/dist/components/ui/index.js +4 -4
- package/dist/contexts/endpoints-runtime-context.d.ts +5 -0
- package/dist/contexts/endpoints-runtime-context.d.ts.map +1 -1
- package/dist/contexts/index.cjs +2 -2
- package/dist/contexts/index.js +1 -1
- package/dist/hooks/index.cjs +5 -3
- package/dist/hooks/index.cjs.map +1 -1
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +4 -2
- package/dist/hooks/use-form-rescue.d.ts +37 -0
- package/dist/hooks/use-form-rescue.d.ts.map +1 -0
- package/dist/index.cjs +51 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +50 -8
- package/dist/index.js.map +1 -1
- package/dist/utils/form-rescue.cjs +117 -0
- package/dist/utils/form-rescue.cjs.map +1 -0
- package/dist/utils/form-rescue.d.ts +98 -0
- package/dist/utils/form-rescue.d.ts.map +1 -0
- package/dist/utils/form-rescue.js +96 -0
- package/dist/utils/form-rescue.js.map +1 -0
- package/dist/utils/index.cjs +113 -0
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +94 -1
- package/dist/utils/index.js.map +1 -1
- package/package.json +7 -1
- package/src/chat-protocol/__tests__/card-marker.test.ts +27 -1
- package/src/chat-protocol/card-marker.ts +24 -0
- package/src/chat-protocol/index.ts +3 -0
- package/src/components/case-studies/share-experience-section.tsx +1 -0
- package/src/components/contact/contact-form.tsx +38 -2
- package/src/components/features/waitlist-form.tsx +23 -2
- package/src/components/help-center-pages/trust-center-sections.tsx +1 -0
- package/src/components/meeting-scheduler/booking-form.tsx +14 -0
- package/src/components/meeting-scheduler/index.tsx +33 -3
- package/src/components/tickets/help-center-create-form.tsx +2 -0
- package/src/contexts/endpoints-runtime-context.tsx +5 -0
- package/src/hooks/__tests__/use-form-rescue.test.tsx +182 -0
- package/src/hooks/index.ts +3 -0
- package/src/hooks/use-form-rescue.ts +315 -0
- package/src/utils/__tests__/form-rescue.test.ts +106 -0
- package/src/utils/form-rescue.ts +190 -0
- package/src/utils/index.ts +5 -0
- package/dist/chunk-BP542DGQ.cjs.map +0 -1
- package/dist/chunk-D55IENZJ.cjs.map +0 -1
- package/dist/chunk-FAJUQKWQ.cjs.map +0 -1
- package/dist/chunk-FUXUJSPP.cjs.map +0 -1
- package/dist/chunk-OG2BNJ2Q.cjs.map +0 -1
- package/dist/chunk-STLBB6PF.js.map +0 -1
- package/dist/chunk-TLLTL6LB.js.map +0 -1
- package/dist/chunk-YNGAJGYB.cjs.map +0 -1
- package/dist/chunk-ZRIVCLPR.js.map +0 -1
- /package/dist/{chunk-4PVY4JJN.js.map → chunk-2TQ64TG3.js.map} +0 -0
- /package/dist/{chunk-G64BNLDO.js.map → chunk-3X6WFYSV.js.map} +0 -0
- /package/dist/{chunk-DUZEFACP.js.map → chunk-4UAIOXYC.js.map} +0 -0
- /package/dist/{chunk-SNNE34OP.js.map → chunk-5KD5SSP6.js.map} +0 -0
- /package/dist/{chunk-RKUCK6MN.js.map → chunk-6NGIPAQ6.js.map} +0 -0
- /package/dist/{chunk-R22IA7MP.js.map → chunk-BBDEIE56.js.map} +0 -0
- /package/dist/{chunk-PQ7YC5Y6.js.map → chunk-D5HO3RNF.js.map} +0 -0
- /package/dist/{chunk-K3O4D7LW.js.map → chunk-KUASHD7Z.js.map} +0 -0
- /package/dist/{chunk-FHTNCWIP.js.map → chunk-KYVFU7VQ.js.map} +0 -0
- /package/dist/{chunk-OYMOG544.js.map → chunk-KZAT6VWE.js.map} +0 -0
- /package/dist/{chunk-IJIIL6WU.js.map → chunk-MVU7HF5A.js.map} +0 -0
- /package/dist/{chunk-UH3BL2OT.js.map → chunk-Y7TZHPS2.js.map} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flamingo-stack/openframe-frontend-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.672-snapshot.20260928032919",
|
|
4
4
|
"description": "Shared design system and components for all Flamingo platforms",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -229,6 +229,12 @@
|
|
|
229
229
|
"require": "./dist/utils/humanity-signals.cjs",
|
|
230
230
|
"default": "./dist/utils/humanity-signals.js"
|
|
231
231
|
},
|
|
232
|
+
"./utils/form-rescue": {
|
|
233
|
+
"types": "./dist/utils/form-rescue.d.ts",
|
|
234
|
+
"import": "./dist/utils/form-rescue.js",
|
|
235
|
+
"require": "./dist/utils/form-rescue.cjs",
|
|
236
|
+
"default": "./dist/utils/form-rescue.js"
|
|
237
|
+
},
|
|
232
238
|
"./utils/platform-identity": {
|
|
233
239
|
"types": "./dist/utils/platform-identity.d.ts",
|
|
234
240
|
"import": "./dist/utils/platform-identity.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import { describe, expect, it } from 'vitest';
|
|
19
|
-
import { CARD_REFERENCE, createCardMarkerScanner } from '../card-marker';
|
|
19
|
+
import { CARD_REFERENCE, createCardMarkerScanner, extractCardReferences } from '../card-marker';
|
|
20
20
|
|
|
21
21
|
/** The id half as the backend validates it: anything but `]` and newlines. */
|
|
22
22
|
const PUNCTUATED_IDS = [
|
|
@@ -61,3 +61,29 @@ describe('card marker grammar', () => {
|
|
|
61
61
|
expect(CARD_REFERENCE.exec('[card://faq:a] plus a note')).toBeNull();
|
|
62
62
|
});
|
|
63
63
|
});
|
|
64
|
+
|
|
65
|
+
describe('extractCardReferences', () => {
|
|
66
|
+
it('returns each reference once, in order of first appearance', () => {
|
|
67
|
+
const text =
|
|
68
|
+
'See [card://hubspot_ticket_self:48720777129] and [card://faq:a], ' +
|
|
69
|
+
'then [card://hubspot_ticket_self:48720777129] again ([card://podcast:9b2f).';
|
|
70
|
+
|
|
71
|
+
expect(extractCardReferences(text)).toEqual([
|
|
72
|
+
{ type: 'hubspot_ticket_self', id: '48720777129' },
|
|
73
|
+
{ type: 'faq', id: 'a' },
|
|
74
|
+
{ type: 'podcast', id: '9b2f' },
|
|
75
|
+
]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('finds nothing in text without markers, or with a malformed one', () => {
|
|
79
|
+
expect(extractCardReferences('no cards here')).toEqual([]);
|
|
80
|
+
expect(extractCardReferences('[card://faq:]')).toEqual([]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('is served from the server-safe `chat-protocol` entry point', async () => {
|
|
84
|
+
const chatProtocol = await import('../index');
|
|
85
|
+
expect(chatProtocol.extractCardReferences).toBe(extractCardReferences);
|
|
86
|
+
expect(chatProtocol.createCardMarkerScanner).toBe(createCardMarkerScanner);
|
|
87
|
+
expect(chatProtocol.CARD_REFERENCE).toBe(CARD_REFERENCE);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -38,3 +38,27 @@ export const CARD_REFERENCE = new RegExp(`^\\[card://(${CARD_TYPE}):(${CARD_ID})
|
|
|
38
38
|
export function createCardMarkerScanner(): RegExp {
|
|
39
39
|
return new RegExp(`\\[card://(${CARD_TYPE}):(${CARD_ID})[\\])]`, 'g');
|
|
40
40
|
}
|
|
41
|
+
|
|
42
|
+
/** One `[card://<type>:<id>]` reference, split into its two halves. */
|
|
43
|
+
export interface CardReference {
|
|
44
|
+
type: string;
|
|
45
|
+
id: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Every card reference EMBEDDED in a text, in order of first appearance,
|
|
50
|
+
* each once. The server reads the references an answer cites through this
|
|
51
|
+
* (the hub's MCP `ask_guide` returns them as `cards[]`), so a host and the
|
|
52
|
+
* body renderer can never disagree about which markers a text carries.
|
|
53
|
+
*/
|
|
54
|
+
export function extractCardReferences(text: string): CardReference[] {
|
|
55
|
+
const seen = new Set<string>();
|
|
56
|
+
const out: CardReference[] = [];
|
|
57
|
+
for (const match of text.matchAll(createCardMarkerScanner())) {
|
|
58
|
+
const key = `${match[1]}:${match[2]}`;
|
|
59
|
+
if (seen.has(key)) continue;
|
|
60
|
+
seen.add(key);
|
|
61
|
+
out.push({ type: match[1], id: match[2] });
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
@@ -23,3 +23,6 @@ export * from './ip-normalize';
|
|
|
23
23
|
// forwarding above must be parsed IDENTICALLY on both sides of the seam.
|
|
24
24
|
export * from './env-flag';
|
|
25
25
|
export { isRecord, unwrapEnvelope } from './wire-narrow';
|
|
26
|
+
// The `[card://<type>:<id>]` grammar — the renderer's and the server's one
|
|
27
|
+
// definition, so a reference the hub emits is one the client expands.
|
|
28
|
+
export * from './card-marker';
|
|
@@ -93,6 +93,7 @@ const DEFAULT_CONTACT_FORM_PROPS = {
|
|
|
93
93
|
buttonVariant: 'outline' as ContactFormProps['buttonVariant'],
|
|
94
94
|
buttonClassName: 'w-full',
|
|
95
95
|
successToastMessage: "Thank you! We'll reach out to schedule your case study.",
|
|
96
|
+
rescueFormId: 'case_study_pitch',
|
|
96
97
|
} satisfies Partial<ContactFormProps>;
|
|
97
98
|
|
|
98
99
|
export function ShareExperienceSection({
|
|
@@ -22,9 +22,10 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
25
|
-
import { useState, type ReactNode } from 'react';
|
|
25
|
+
import { useEffect, useMemo, useState, type ReactNode } from 'react';
|
|
26
26
|
import { useForm, Controller } from 'react-hook-form';
|
|
27
27
|
import { useContactSubmission } from '../../hooks/use-contact-submission';
|
|
28
|
+
import { useFormRescue } from '../../hooks/use-form-rescue';
|
|
28
29
|
import { useHumanitySignals } from '../../hooks/use-humanity-signals';
|
|
29
30
|
import {
|
|
30
31
|
ContactSchema,
|
|
@@ -33,6 +34,7 @@ import {
|
|
|
33
34
|
referralSourceOptions,
|
|
34
35
|
defaultHelpCategoryOptions,
|
|
35
36
|
} from '../../schemas/contact-schema';
|
|
37
|
+
import type { FormRescueFormId } from '../../utils/form-rescue';
|
|
36
38
|
import { HUBSPOT_DO_NOT_COLLECT_FORM_PROPS } from '../../utils/hubspot-collected-forms';
|
|
37
39
|
import { ChatAttachmentAddButton, ChatAttachmentChipStrip } from '../chat/chat-attachment-bar';
|
|
38
40
|
import { useChatAttachments } from '../chat/hooks/use-chat-attachments';
|
|
@@ -130,6 +132,10 @@ export interface ContactFormProps {
|
|
|
130
132
|
submitSuccessLabel?: string;
|
|
131
133
|
successRedirectUrl?: string;
|
|
132
134
|
successToastMessage?: string;
|
|
135
|
+
/** Which public form this is, for form rescue (a half-filled form is saved so
|
|
136
|
+
* the team can follow up). Defaults to `'contact'`; `null` turns it off for a
|
|
137
|
+
* form that is not a lead (e.g. a signed-in ticket). */
|
|
138
|
+
rescueFormId?: FormRescueFormId | null;
|
|
133
139
|
}
|
|
134
140
|
|
|
135
141
|
export function ContactForm({
|
|
@@ -155,6 +161,7 @@ export function ContactForm({
|
|
|
155
161
|
submitSuccessLabel = 'Message Sent!',
|
|
156
162
|
successRedirectUrl = '/blog#community',
|
|
157
163
|
successToastMessage = 'Redirecting you to join our community...',
|
|
164
|
+
rescueFormId = 'contact',
|
|
158
165
|
}: ContactFormProps = {}) {
|
|
159
166
|
// Attachments staging — same hook the chat composer + ticket
|
|
160
167
|
// detail-drawer composer use. Files upload to Supabase as soon as
|
|
@@ -191,6 +198,9 @@ export function ContactForm({
|
|
|
191
198
|
control,
|
|
192
199
|
formState: { errors },
|
|
193
200
|
reset,
|
|
201
|
+
watch,
|
|
202
|
+
getValues,
|
|
203
|
+
setValue,
|
|
194
204
|
} = useForm<ContactFormData>({
|
|
195
205
|
resolver: zodResolver(ContactSchema),
|
|
196
206
|
defaultValues: {
|
|
@@ -202,11 +212,36 @@ export function ContactForm({
|
|
|
202
212
|
},
|
|
203
213
|
});
|
|
204
214
|
|
|
215
|
+
// Form rescue: only the fields the visitor can see are tracked, so a hidden
|
|
216
|
+
// prefilled value (a signed-in email, a fixed category) is never sent.
|
|
217
|
+
const rescueFieldNames = useMemo(
|
|
218
|
+
() =>
|
|
219
|
+
(['name', 'email', 'companySize', 'referralSource', 'helpCategory', 'message'] as const).filter(
|
|
220
|
+
field => !hideFields.includes(field),
|
|
221
|
+
),
|
|
222
|
+
[hideFields],
|
|
223
|
+
);
|
|
224
|
+
const rescue = useFormRescue({
|
|
225
|
+
formId: rescueFormId,
|
|
226
|
+
fieldNames: rescueFieldNames,
|
|
227
|
+
getSignals,
|
|
228
|
+
onRestore: values => {
|
|
229
|
+
for (const field of rescueFieldNames) {
|
|
230
|
+
const value = values[field];
|
|
231
|
+
if (value && !getValues(field)) setValue(field, value);
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
useEffect(() => {
|
|
236
|
+
const subscription = watch((values, { name }) => rescue.track(values, name));
|
|
237
|
+
return () => subscription.unsubscribe();
|
|
238
|
+
}, [watch, rescue]);
|
|
239
|
+
|
|
205
240
|
const handleFormSubmit = async (data: ContactFormData) => {
|
|
206
241
|
if (isSubmitting) return;
|
|
207
242
|
if (attachmentsEnabled && attachments.hasInflightUploads) return;
|
|
208
243
|
try {
|
|
209
|
-
const payload = { ...data, ...(rdtCid && { rdt_cid: rdtCid }), ...getSignals() };
|
|
244
|
+
const payload = { ...data, ...(rdtCid && { rdt_cid: rdtCid }), ...getSignals(), ...rescue.submitFields() };
|
|
210
245
|
const readyAttachments = attachmentsEnabled ? attachments.readyAttachments : [];
|
|
211
246
|
if (onCustomSubmit) {
|
|
212
247
|
setCustomSubmitting(true);
|
|
@@ -218,6 +253,7 @@ export function ContactForm({
|
|
|
218
253
|
} else {
|
|
219
254
|
await builtInSubmission.submit(payload);
|
|
220
255
|
}
|
|
256
|
+
rescue.complete();
|
|
221
257
|
onSubmitSuccess?.();
|
|
222
258
|
reset();
|
|
223
259
|
resetSignals();
|
|
@@ -5,6 +5,7 @@ import type { CountryCode } from 'libphonenumber-js';
|
|
|
5
5
|
import { useEffect, useState } from 'react';
|
|
6
6
|
import type { KeyboardEvent } from 'react';
|
|
7
7
|
import { useIsHydrated } from '../../hooks/ui/use-is-hydrated';
|
|
8
|
+
import { useFormRescue } from '../../hooks/use-form-rescue';
|
|
8
9
|
import { useHumanitySignals } from '../../hooks/use-humanity-signals';
|
|
9
10
|
import { useToast } from '../../hooks/use-toast';
|
|
10
11
|
import { cn } from '../../utils/cn';
|
|
@@ -30,7 +31,14 @@ export interface WaitlistFormProps {
|
|
|
30
31
|
* POST body. Must throw on failure (toast is handled by the form). `signals`
|
|
31
32
|
* is optional for backward compatibility with older callers.
|
|
32
33
|
*/
|
|
33
|
-
|
|
34
|
+
/** `rescue` carries the form-rescue keys (`form_attempt_id`, …) to merge into the
|
|
35
|
+
* POST body, so the host closes the half-filled draft this submit completes. */
|
|
36
|
+
onRegister: (
|
|
37
|
+
email: string,
|
|
38
|
+
phone?: string,
|
|
39
|
+
signals?: HumanitySignals,
|
|
40
|
+
rescue?: Record<string, string>,
|
|
41
|
+
) => Promise<void>;
|
|
34
42
|
/** Whether a registration request is currently in flight */
|
|
35
43
|
isSubmitting?: boolean;
|
|
36
44
|
/** Whether registration completed successfully */
|
|
@@ -101,6 +109,18 @@ export function WaitlistForm({
|
|
|
101
109
|
const isClient = useIsHydrated();
|
|
102
110
|
const [isPhoneInvalid, setIsPhoneInvalid] = useState(false);
|
|
103
111
|
const [showConsentError, setShowConsentError] = useState(false);
|
|
112
|
+
const rescue = useFormRescue({
|
|
113
|
+
formId: 'waitlist',
|
|
114
|
+
// The phone is reported as filled only; its value never leaves the form early.
|
|
115
|
+
fieldNames: ['email', 'phone'],
|
|
116
|
+
getSignals,
|
|
117
|
+
onRestore: values => {
|
|
118
|
+
if (values.email) setEmail(current => current || values.email);
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
rescue.track({ email, phone }, null);
|
|
123
|
+
}, [email, phone, rescue]);
|
|
104
124
|
|
|
105
125
|
const isMailDomainGeneric = hasGenericEmailDomain(email);
|
|
106
126
|
|
|
@@ -160,7 +180,8 @@ export function WaitlistForm({
|
|
|
160
180
|
const finalPhone = phone ? formatPhoneE164(phone, countryCode) : undefined;
|
|
161
181
|
|
|
162
182
|
try {
|
|
163
|
-
await onRegister(email, finalPhone, getSignals());
|
|
183
|
+
await onRegister(email, finalPhone, getSignals(), rescue.submitFields());
|
|
184
|
+
rescue.complete();
|
|
164
185
|
resetSignals();
|
|
165
186
|
} catch {
|
|
166
187
|
// caller's onRegister should handle its own error toasts if needed
|
|
@@ -370,6 +370,9 @@ export interface BookingFormProps {
|
|
|
370
370
|
/** From useHumanitySignals — parent owns the instance so it can resetSignals(). */
|
|
371
371
|
honeypotInputProps: { ref: Ref<HTMLInputElement>; name: string };
|
|
372
372
|
getSignals: () => Record<string, string | number>;
|
|
373
|
+
/** Called on every edit with the answers flattened (declared questions beside
|
|
374
|
+
* the built-ins) and the edited field's name — the parent's form rescue. */
|
|
375
|
+
onValuesChange?: (values: Record<string, unknown>, changedField: string | null) => void;
|
|
373
376
|
}
|
|
374
377
|
|
|
375
378
|
/**
|
|
@@ -402,6 +405,7 @@ export function BookingForm({
|
|
|
402
405
|
onSubmit,
|
|
403
406
|
honeypotInputProps,
|
|
404
407
|
getSignals,
|
|
408
|
+
onValuesChange,
|
|
405
409
|
}: BookingFormProps) {
|
|
406
410
|
const { formFields, legalConsent } = availability;
|
|
407
411
|
// Every declared question resolved to a control (`resolveFormFieldControl`) —
|
|
@@ -431,6 +435,7 @@ export function BookingForm({
|
|
|
431
435
|
handleSubmit,
|
|
432
436
|
setValue,
|
|
433
437
|
getValues,
|
|
438
|
+
watch,
|
|
434
439
|
formState: { errors },
|
|
435
440
|
} = useForm<BookingFormValues>({
|
|
436
441
|
resolver: zodResolver(schema),
|
|
@@ -479,6 +484,15 @@ export function BookingForm({
|
|
|
479
484
|
);
|
|
480
485
|
}, [consentDefaults, priorConsents, getValues, setValue]);
|
|
481
486
|
|
|
487
|
+
useEffect(() => {
|
|
488
|
+
if (!onValuesChange) return undefined;
|
|
489
|
+
const subscription = watch((values, { name }) => {
|
|
490
|
+
const answers = (values.formFields ?? {}) as Record<string, unknown>;
|
|
491
|
+
onValuesChange({ ...values, ...answers }, name ? name.replace(/^formFields\./, '') : null);
|
|
492
|
+
});
|
|
493
|
+
return () => subscription.unsubscribe();
|
|
494
|
+
}, [watch, onValuesChange]);
|
|
495
|
+
|
|
482
496
|
const submitValid = handleSubmit(async data => {
|
|
483
497
|
if (consentMissing) return; // the error is already on screen — see `submit`
|
|
484
498
|
if (deferSlot) {
|
|
@@ -42,11 +42,14 @@
|
|
|
42
42
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
43
43
|
import type { ComponentType, ReactNode } from 'react';
|
|
44
44
|
import { useIsHydrated } from '../../hooks/ui/use-is-hydrated';
|
|
45
|
+
import { useFormRescue } from '../../hooks/use-form-rescue';
|
|
45
46
|
import { useHumanitySignals } from '../../hooks/use-humanity-signals';
|
|
46
47
|
import { BOOKING_IN_FLIGHT_MESSAGE, useMeetingBooking } from '../../hooks/use-meeting-booking';
|
|
47
48
|
import { useToast } from '../../hooks/use-toast';
|
|
48
49
|
import {
|
|
49
50
|
blocksNativeBooking,
|
|
51
|
+
BUILT_IN_BOOKING_FIELDS,
|
|
52
|
+
normalizeFormFields,
|
|
50
53
|
type BookingConfirmation,
|
|
51
54
|
type MeetingAvailability,
|
|
52
55
|
type MeetingBookingErrorCode,
|
|
@@ -492,6 +495,30 @@ export function HubSpotMeetingScheduler({
|
|
|
492
495
|
const { honeypotInputProps, getSignals, resetSignals } = useHumanitySignals();
|
|
493
496
|
const { toast } = useToast();
|
|
494
497
|
|
|
498
|
+
// Form rescue lives HERE, not in the form: in details-first the form unmounts
|
|
499
|
+
// before the POST, and the attempt must survive it. The form reports edits
|
|
500
|
+
// (`onValuesChange`); a restored draft remounts it with the answers seeded.
|
|
501
|
+
const rescueFieldNames = useMemo(
|
|
502
|
+
() => [
|
|
503
|
+
...BUILT_IN_BOOKING_FIELDS.map(field => field.name),
|
|
504
|
+
...normalizeFormFields(availability?.formFields ?? []).map(field => field.name),
|
|
505
|
+
],
|
|
506
|
+
[availability?.formFields],
|
|
507
|
+
);
|
|
508
|
+
const [restored, setRestored] = useState<{ version: number; values: Record<string, unknown> } | null>(null);
|
|
509
|
+
const rescue = useFormRescue({
|
|
510
|
+
formId: 'meeting_booking',
|
|
511
|
+
fieldNames: rescueFieldNames,
|
|
512
|
+
getSignals,
|
|
513
|
+
onRestore: values => {
|
|
514
|
+
const { email, firstName, lastName, ...answers } = values;
|
|
515
|
+
setRestored(prev => ({
|
|
516
|
+
version: (prev?.version ?? 0) + 1,
|
|
517
|
+
values: { email: email ?? '', firstName: firstName ?? '', lastName: lastName ?? '', formFields: answers },
|
|
518
|
+
}));
|
|
519
|
+
},
|
|
520
|
+
});
|
|
521
|
+
|
|
495
522
|
// Reset the machine when the host switches links. Adjusted while rendering —
|
|
496
523
|
// React's documented pattern for a prop-driven reset — so the swapped-in link
|
|
497
524
|
// never paints a frame of the previous link's chosen day, slot or
|
|
@@ -634,7 +661,7 @@ export function HubSpotMeetingScheduler({
|
|
|
634
661
|
// visible SLOT_TAKEN alert on its way to being ignored.
|
|
635
662
|
const priorError = bookingError;
|
|
636
663
|
setBookingError(null);
|
|
637
|
-
const result = await book(payload);
|
|
664
|
+
const result = await book({ ...payload, ...rescue.submitFields() });
|
|
638
665
|
|
|
639
666
|
// Not an error: the hook's in-flight guard. No toast, no step change.
|
|
640
667
|
if (!result.ok && result.message === BOOKING_IN_FLIGHT_MESSAGE) {
|
|
@@ -642,6 +669,7 @@ export function HubSpotMeetingScheduler({
|
|
|
642
669
|
return;
|
|
643
670
|
}
|
|
644
671
|
if (result.ok && result.confirmation) {
|
|
672
|
+
rescue.complete();
|
|
645
673
|
setConfirmation(result.confirmation);
|
|
646
674
|
setStep('confirmed');
|
|
647
675
|
onBooked?.(result.confirmation);
|
|
@@ -681,7 +709,7 @@ export function HubSpotMeetingScheduler({
|
|
|
681
709
|
});
|
|
682
710
|
}
|
|
683
711
|
},
|
|
684
|
-
[book, bookingError, detailsFirst, onBooked, refetchAvailability, resetSignals, toast],
|
|
712
|
+
[book, bookingError, detailsFirst, onBooked, refetchAvailability, rescue, resetSignals, toast],
|
|
685
713
|
);
|
|
686
714
|
|
|
687
715
|
const escapeHatch = fallbackUrl ? (
|
|
@@ -853,6 +881,7 @@ export function HubSpotMeetingScheduler({
|
|
|
853
881
|
</p>
|
|
854
882
|
)}
|
|
855
883
|
<DetailsForm
|
|
884
|
+
key={restored?.version ?? 0}
|
|
856
885
|
{...detailsFormProps}
|
|
857
886
|
availability={availability}
|
|
858
887
|
meetingId={meetingId}
|
|
@@ -862,11 +891,12 @@ export function HubSpotMeetingScheduler({
|
|
|
862
891
|
deferSlot={detailsFirst}
|
|
863
892
|
submitLabel={preset.submitLabel}
|
|
864
893
|
footerNote={preset.footerNote}
|
|
865
|
-
initialValues={detailsFirst ? stash?.payload : undefined}
|
|
894
|
+
initialValues={(detailsFirst ? stash?.payload : undefined) ?? restored?.values}
|
|
866
895
|
isSubmitting={isSubmitting}
|
|
867
896
|
onSubmit={detailsFirst ? stashDetails : handleSubmit}
|
|
868
897
|
honeypotInputProps={honeypotInputProps}
|
|
869
898
|
getSignals={getSignals}
|
|
899
|
+
onValuesChange={rescue.track}
|
|
870
900
|
/>
|
|
871
901
|
</div>
|
|
872
902
|
) : (
|
|
@@ -205,6 +205,8 @@ export function HelpCenterCreateForm({
|
|
|
205
205
|
|
|
206
206
|
return (
|
|
207
207
|
<ContactForm
|
|
208
|
+
// A signed-in support ticket, not a lead: nothing to rescue.
|
|
209
|
+
rescueFormId={null}
|
|
208
210
|
title="Open a new ticket"
|
|
209
211
|
footerText="The support team typically responds within one business day."
|
|
210
212
|
hideFields={['name', 'email', 'companySize', 'referralSource', 'helpCategory']}
|
|
@@ -34,6 +34,11 @@ export interface EndpointsRuntime {
|
|
|
34
34
|
};
|
|
35
35
|
/** POST contact-form submission. */
|
|
36
36
|
contactUrl: string;
|
|
37
|
+
/** Base of the host's form-rescue draft endpoints: `POST <base>` saves a
|
|
38
|
+
* half-filled public form (upsert by `attempt_id`), `GET <base>/resume/<token>` reads one
|
|
39
|
+
* back for a resume link (`useFormRescue`). OPTIONAL: a host without them
|
|
40
|
+
* omits it and forms keep only this device's local draft. */
|
|
41
|
+
formDraftsUrl?: string;
|
|
37
42
|
/** GET base of the host's private-storage view proxy
|
|
38
43
|
* (`<base>/<bucket>/<object>`). `ClaudeEmbed` derives artifact mirror
|
|
39
44
|
* urls under it (`<base>/design-briefs/<uuid>.html`). OPTIONAL: a host
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { act, renderHook, waitFor } from '@testing-library/react';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import { EndpointsRuntimeContext, type EndpointsRuntime } from '../../contexts/endpoints-runtime-context';
|
|
6
|
+
import { FORM_RESCUE_DEBOUNCE_MS } from '../../utils/form-rescue';
|
|
7
|
+
import { useFormRescue } from '../use-form-rescue';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The hook's contract is a network and privacy contract, so it is asserted on
|
|
11
|
+
* the fetch spy: one debounced save per burst of typing, only allowlisted
|
|
12
|
+
* values in the body, no save before anything is filled, no analytics event
|
|
13
|
+
* carrying a value, and a submit that closes the attempt it saved.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const runtime: EndpointsRuntime = {
|
|
17
|
+
announcementsUrl: '/api/announcements',
|
|
18
|
+
accessCode: { validateUrl: '/v', consumeUrl: '/c' },
|
|
19
|
+
contactUrl: '/api/contact',
|
|
20
|
+
formDraftsUrl: '/api/contact/drafts',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function wrapper({ children }: { children: ReactNode }) {
|
|
24
|
+
return <EndpointsRuntimeContext.Provider value={runtime}>{children}</EndpointsRuntimeContext.Provider>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const FIELDS = ['name', 'email', 'message'] as const;
|
|
28
|
+
|
|
29
|
+
let fetchSpy: ReturnType<typeof vi.fn>;
|
|
30
|
+
let captures: Array<{ event: string; props: Record<string, unknown> }>;
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
vi.useFakeTimers();
|
|
34
|
+
window.localStorage.clear();
|
|
35
|
+
fetchSpy = vi.fn(() => Promise.resolve(new Response(JSON.stringify({ status: 'started' }), { status: 202 })));
|
|
36
|
+
vi.stubGlobal('fetch', fetchSpy);
|
|
37
|
+
captures = [];
|
|
38
|
+
(window as unknown as { posthog: unknown }).posthog = {
|
|
39
|
+
capture: (event: string, props: Record<string, unknown>) => captures.push({ event, props }),
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
vi.useRealTimers();
|
|
45
|
+
vi.unstubAllGlobals();
|
|
46
|
+
delete (window as unknown as { posthog?: unknown }).posthog;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
function sentBodies() {
|
|
50
|
+
return fetchSpy.mock.calls
|
|
51
|
+
.filter(([, init]) => (init as RequestInit | undefined)?.method === 'POST')
|
|
52
|
+
.map(([url, init]) => ({ url: String(url), body: JSON.parse(String((init as RequestInit).body)) }));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
describe('useFormRescue', () => {
|
|
56
|
+
it('debounces one save per burst with allowlisted values only', () => {
|
|
57
|
+
const { result } = renderHook(() => useFormRescue({ formId: 'contact', fieldNames: FIELDS }), { wrapper });
|
|
58
|
+
|
|
59
|
+
act(() => {
|
|
60
|
+
result.current.track({ name: 'Al', email: '', message: '' }, 'name');
|
|
61
|
+
result.current.track({ name: 'Alex', email: 'alex@northwind-it.com', message: 'secret text' }, 'message');
|
|
62
|
+
});
|
|
63
|
+
expect(sentBodies()).toHaveLength(0);
|
|
64
|
+
|
|
65
|
+
act(() => {
|
|
66
|
+
vi.advanceTimersByTime(FORM_RESCUE_DEBOUNCE_MS);
|
|
67
|
+
});
|
|
68
|
+
const sent = sentBodies();
|
|
69
|
+
expect(sent).toHaveLength(1);
|
|
70
|
+
expect(sent[0].url).toBe('/api/contact/drafts');
|
|
71
|
+
expect(sent[0].body.attempt_id).toMatch(/^[0-9a-f-]{36}$/);
|
|
72
|
+
expect(sent[0].body).toMatchObject({
|
|
73
|
+
form_id: 'contact',
|
|
74
|
+
values: { name: 'Alex', email: 'alex@northwind-it.com' },
|
|
75
|
+
fields_filled: ['name', 'email', 'message'],
|
|
76
|
+
last_field: 'message',
|
|
77
|
+
completion_pct: 100,
|
|
78
|
+
});
|
|
79
|
+
expect(JSON.stringify(sent[0].body)).not.toContain('secret text');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('sends nothing until a field is filled', () => {
|
|
83
|
+
const { result } = renderHook(() => useFormRescue({ formId: 'contact', fieldNames: FIELDS }), { wrapper });
|
|
84
|
+
act(() => {
|
|
85
|
+
result.current.track({ name: '', email: '', message: '' }, 'name');
|
|
86
|
+
vi.advanceTimersByTime(FORM_RESCUE_DEBOUNCE_MS * 2);
|
|
87
|
+
});
|
|
88
|
+
expect(sentBodies()).toHaveLength(0);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('never puts a field value into an analytics event', () => {
|
|
92
|
+
const { result } = renderHook(() => useFormRescue({ formId: 'contact', fieldNames: FIELDS }), { wrapper });
|
|
93
|
+
act(() => {
|
|
94
|
+
result.current.track({ name: 'Alex', email: 'alex@northwind-it.com', message: '' }, 'email');
|
|
95
|
+
vi.advanceTimersByTime(FORM_RESCUE_DEBOUNCE_MS);
|
|
96
|
+
});
|
|
97
|
+
expect(captures.map(c => c.event)).toEqual(['form_started', 'form_lead_captured', 'form_progress']);
|
|
98
|
+
const serialized = JSON.stringify(captures);
|
|
99
|
+
expect(serialized).not.toContain('alex@northwind-it.com');
|
|
100
|
+
expect(serialized).not.toContain('Alex"');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('carries the saved attempt into the submit and starts a fresh one after', () => {
|
|
104
|
+
const { result } = renderHook(() => useFormRescue({ formId: 'contact', fieldNames: FIELDS }), { wrapper });
|
|
105
|
+
act(() => {
|
|
106
|
+
result.current.track({ name: 'Alex', email: '', message: '' }, 'name');
|
|
107
|
+
vi.advanceTimersByTime(FORM_RESCUE_DEBOUNCE_MS);
|
|
108
|
+
});
|
|
109
|
+
const savedId = sentBodies()[0].body.attempt_id;
|
|
110
|
+
expect(result.current.submitFields()).toEqual({ form_attempt_id: savedId });
|
|
111
|
+
|
|
112
|
+
act(() => result.current.complete());
|
|
113
|
+
expect(window.localStorage.getItem('form-rescue:v1:contact')).toBeNull();
|
|
114
|
+
expect(result.current.submitFields().form_attempt_id).not.toBe(savedId);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('restores this device’s draft and keeps its attempt', () => {
|
|
118
|
+
window.localStorage.setItem(
|
|
119
|
+
'form-rescue:v1:contact',
|
|
120
|
+
JSON.stringify({
|
|
121
|
+
attemptId: '6f1c2a7e-3b9d-4e21-9a4f-0c8d5e7b1a22',
|
|
122
|
+
values: { email: 'alex@northwind-it.com', password: 'never' },
|
|
123
|
+
savedAt: Date.now(),
|
|
124
|
+
}),
|
|
125
|
+
);
|
|
126
|
+
const onRestore = vi.fn();
|
|
127
|
+
const { result } = renderHook(() => useFormRescue({ formId: 'contact', fieldNames: FIELDS, onRestore }), {
|
|
128
|
+
wrapper,
|
|
129
|
+
});
|
|
130
|
+
expect(onRestore).toHaveBeenCalledWith({ email: 'alex@northwind-it.com' });
|
|
131
|
+
expect(result.current.submitFields()).toEqual({ form_attempt_id: '6f1c2a7e-3b9d-4e21-9a4f-0c8d5e7b1a22' });
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('prefills from a resume link and sends its token with the submit', async () => {
|
|
135
|
+
vi.useRealTimers();
|
|
136
|
+
// vitest.setup replaces `window.location` with a plain object; set its search directly.
|
|
137
|
+
const location = window.location as unknown as { search: string; pathname: string };
|
|
138
|
+
location.pathname = '/contact';
|
|
139
|
+
location.search = '?resume=k3Xq9vT2mB7wYp1sLr8dQa';
|
|
140
|
+
fetchSpy.mockImplementation(() =>
|
|
141
|
+
Promise.resolve(
|
|
142
|
+
new Response(JSON.stringify({ form_id: 'contact', values: { email: 'alex@northwind-it.com' } }), {
|
|
143
|
+
status: 200,
|
|
144
|
+
}),
|
|
145
|
+
),
|
|
146
|
+
);
|
|
147
|
+
const onRestore = vi.fn();
|
|
148
|
+
const { result } = renderHook(() => useFormRescue({ formId: 'contact', fieldNames: FIELDS, onRestore }), {
|
|
149
|
+
wrapper,
|
|
150
|
+
});
|
|
151
|
+
await waitFor(() => expect(onRestore).toHaveBeenCalledWith({ email: 'alex@northwind-it.com' }));
|
|
152
|
+
expect(String(fetchSpy.mock.calls[0][0])).toBe('/api/contact/drafts/resume/k3Xq9vT2mB7wYp1sLr8dQa');
|
|
153
|
+
expect(result.current.submitFields()).toMatchObject({ form_resume_token: 'k3Xq9vT2mB7wYp1sLr8dQa' });
|
|
154
|
+
expect(captures.map(c => c.event)).toContain('form_resumed');
|
|
155
|
+
location.pathname = '/';
|
|
156
|
+
location.search = '';
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('does nothing when rescue is off', () => {
|
|
160
|
+
const { result } = renderHook(() => useFormRescue({ formId: null, fieldNames: FIELDS }), { wrapper });
|
|
161
|
+
act(() => {
|
|
162
|
+
result.current.track({ name: 'Alex', email: 'alex@northwind-it.com', message: '' }, 'name');
|
|
163
|
+
vi.advanceTimersByTime(FORM_RESCUE_DEBOUNCE_MS);
|
|
164
|
+
});
|
|
165
|
+
expect(fetchSpy).not.toHaveBeenCalled();
|
|
166
|
+
expect(result.current.submitFields()).toEqual({});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('saves once more with keepalive when the tab is hidden', () => {
|
|
170
|
+
const { result } = renderHook(() => useFormRescue({ formId: 'contact', fieldNames: FIELDS }), { wrapper });
|
|
171
|
+
act(() => {
|
|
172
|
+
result.current.track({ name: 'Alex', email: '', message: '' }, 'name');
|
|
173
|
+
});
|
|
174
|
+
act(() => {
|
|
175
|
+
window.dispatchEvent(new Event('pagehide'));
|
|
176
|
+
});
|
|
177
|
+
const puts = fetchSpy.mock.calls.filter(([, init]) => (init as RequestInit)?.method === 'POST');
|
|
178
|
+
expect(puts).toHaveLength(1);
|
|
179
|
+
expect((puts[0][1] as RequestInit).keepalive).toBe(true);
|
|
180
|
+
expect(captures.map(c => c.event)).toContain('form_abandoned');
|
|
181
|
+
});
|
|
182
|
+
});
|
package/src/hooks/index.ts
CHANGED
|
@@ -41,3 +41,6 @@ export * from './use-scroll-to-hash';
|
|
|
41
41
|
// Invisible bot-protection client primitive (honeypot ref + submit-timing).
|
|
42
42
|
// Pairs with the server-safe decision fn in `utils/humanity-signals`.
|
|
43
43
|
export * from './use-humanity-signals';
|
|
44
|
+
// Saves a half-filled public form so a visitor who leaves can be followed up with.
|
|
45
|
+
// Pairs with the server-safe rules in `utils/form-rescue`.
|
|
46
|
+
export * from './use-form-rescue';
|