Frictionless Share Invites — Implementation Plan
Version: 2.1 Last Updated: 2026-07-07 13:50:51 Status: Implemented (final design differs from the original proposal — see §0) Owner: Team onboarding (Carers & Observers)
0. Final shipped design (supersedes §2–§6 below)
Section titled “0. Final shipped design (supersedes §2–§6 below)”The invite is generated in-app, then handed off through the device’s native
share sheet (Web Share API). The per-channel deep-link buttons
(WhatsApp/Messenger/Telegram via window.open) were removed on 2026-07-07:
window.open('whatsapp://…' / 'https://www.messenger.com/' / 'tg://…') does not
launch an installed app — Chrome/Android open a browser tab that lands on the
app’s “download” page, and Messenger had no mobile handling at all. The OS share
sheet is the reliable cross-platform way to open an installed app.
- Invite Carers page: pick “Caring for” (required) → Generate invite.
- Generate creates a single-use invite (
POST /api/invitations, patient only — no role chosen here; the admin sets carer/observer when they approve the applicant) and opens a modal showing the link + QR image (rendered fully client-side viaqrcode, so the token never leaves the browser). - Modal has an optional personal message, then:
- Share invite →
navigator.share({ title, text, files: [qr.png] }). The OS lists whatever apps are installed (WhatsApp, Messenger, Telegram, Instagram, email, …) and attaches the compact QR+link PNG where the platform supports file sharing (navigator.canShare({ files })), otherwise shares the text + link. Cancelling the sheet is a no-op. - Email invite → opens a
mailto:draft with message + link in the body, and copies the QR image to the clipboard to paste into the body (mailto:cannot auto-attach an image — this is the closest possible). - Copy (link + QR block) → manual copy for any surface.
- Share invite →
- Fallback (no share sheet — Firefox desktop / Linux): Share invite copies
the link + QR PNG to the clipboard (
ClipboardItem, text-only fallback) and tells the admin to paste into any app. - Tracking: the Email path records
POST /api/invitations/{id}/shared(delivery_channel = email,shared_at, message). The OS share sheet does not report which app the user picked, so share-sheet sends are not attributed to a specific channel (accepted trade-off). Pending invites still show “sent via …” / “not sent yet” and a re-share button.
Key files: frontend/src/components/settings/InviteShareModal.tsx,
TeamMembersPanel.tsx, frontend/src/lib/qr.ts (shareInvite,
composeInviteImage, copyInviteContent), frontend/src/lib/inviteChannels.ts,
backend/routers/invitations.py (record_invite_share), migrations
V018/V019.
Known browser/platform limits: Web Share is unsupported on Firefox desktop
and Linux Chrome/Edge (clipboard fallback used there). Some share targets that
accept a file may drop the accompanying text — the QR PNG has the URL printed on
it, so the link is still conveyed. mailto: bodies are plain-text (no inline
image).
1. Problem
Section titled “1. Problem”Inviting a carer or observer today is high-friction for the administrator
(frontend/src/components/settings/TeamMembersPanel.tsx,
backend/routers/invitations.py):
- Select the patient (“Caring for”)
- Pick a role
- Optionally write a message
- Click Generate invite link
- Manually copy the link or code
- Leave the app, open a messaging/email app, paste, address, and send
Steps 5–6 are pure friction. The security model is already fully server-side and low-friction (opaque single-use token, 72h TTL, tenant-scoped, post-registration admin approval) — only delivery is clumsy.
2. Goal
Section titled “2. Goal”The admin enters an email address (optional), picks a role, and taps Share invite. The app creates the invitation in the background and hands the link straight to the device’s native sharing capability. One field, one tap. Works on Windows desktop, macOS, Android, and iOS.
3. Chosen approach — progressive share ladder
Section titled “3. Chosen approach — progressive share ladder”A single Share invite action that degrades gracefully so no platform is ever stuck:
- Tier 1 — Web Share API (
navigator.share): mobile Safari/Chrome, desktop Chrome/Edge, Safari macOS. Opens the real native share sheet. - Tier 2 —
mailto:fallback: desktop Firefox and any browser withoutnavigator.share. Pre-fills To/Subject/Body. - Tier 3 — Clipboard copy + toast: guaranteed floor everywhere.
Requirements satisfied by current infra: Web Share needs a secure context —
production is HTTPS via Cloudflare Tunnel, and localhost counts as secure for
dev. A user gesture (the button click) is present.
Non-goal: server-side transactional email (SMTP/SendGrid/SES). Deliberately avoided — no new infra, no domain reputation/SPF/DKIM work, no PHI through a third-party mail vendor, and invites arrive from someone the recipient knows.
4. Decisions (defaults chosen — override if desired)
Section titled “4. Decisions (defaults chosen — override if desired)”| # | Decision | Default taken | Rationale |
|---|---|---|---|
| D1 | Post-registration approval | Keep the existing admin approval step | Safety; email/share is delivery only, not identity proof |
| D2 | Persist invited email | Yes — store invitee_email on invitations | Lets admin see “Invited sarah@x.com — pending”, re-share, and pre-fill; enables optional match on register |
| D3 | Email match on register | Soft — store + display only; do not hard-block a mismatched email at registration | Avoids locking out someone who signs up with a different address; admin approval remains the gate |
If you prefer auto-approval for invited emails (D1) or no persistence (D2), the plan collapses accordingly (skip §5.1 migration and §5.2 storage).
5. Changes by layer
Section titled “5. Changes by layer”5.1 Database — migration V018__invitations_invitee_email.sql
Section titled “5.1 Database — migration V018__invitations_invitee_email.sql”- Add nullable column
invitee_email TEXTtoinvitations. - Mirror the same
ADD COLUMN IF NOT EXISTSinbackend/schema.sql(canonical DDL) so fresh installs match. - No index needed (low cardinality, admin-scoped lists already filter by
tenant_id).
5.2 Backend — backend/routers/invitations.py
Section titled “5.2 Backend — backend/routers/invitations.py”InviteCreate: addinvitee_email: str | None = None(PydanticEmailStr-style validation optional; keep lenient — empty/None allowed).create_invitation: persistinvitee_emailin theINSERT ... RETURNING *(parameterised%s, no f-strings).list_invitations:invitee_emailflows through automatically via the row dict; confirm it is returned.- Keep the endpoint under 50 lines of logic (Router boundary); no SQL added
beyond the existing insert. Still tenant-scoped via
require_tenant_admin. - No new audit obligation beyond current behaviour (invitations are not patient-clinical records), but keep parity with existing create logging.
OpenAPI contract (mandatory per rules): after editing the Pydantic model, run
python backend/scripts/export_openapi.py then
cd frontend && npm run generate:types; commit openapi/openapi.json and
frontend/src/lib/api.types.ts together.
5.3 Frontend API client — frontend/src/lib/api.ts
Section titled “5.3 Frontend API client — frontend/src/lib/api.ts”Invitationinterface: addinvitee_email?: string | null.invitations.create: accept and sendinvitee_emailin the body.
5.4 Frontend share helper — frontend/src/lib/share.ts (new)
Section titled “5.4 Frontend share helper — frontend/src/lib/share.ts (new)”Small, typed, reusable utility (keeps the component lean):
export type ShareOutcome = 'shared' | 'mailto' | 'copied' | 'cancelled'
export interface InviteSharePayload { url: string title: string text: string // warm message + code fallback line email?: string // optional; enables mailto To:}
export async function shareInvite(p: InviteSharePayload): Promise<ShareOutcome>Behaviour: feature-detect navigator.canShare?.({ url }) → navigator.share;
catch AbortError → return 'cancelled' (user closed the sheet, not an error);
fall back to mailto: (encoded subject/body, To only when email present);
final fallback navigator.clipboard.writeText → 'copied'. Body kept well under
~1,800 chars.
5.5 Frontend UI — frontend/src/components/settings/TeamMembersPanel.tsx
Section titled “5.5 Frontend UI — frontend/src/components/settings/TeamMembersPanel.tsx”- Add an email input (optional, type=email) to the invite form, above the role selector. Label makes clear it’s optional (“Their email — optional, we’ll pre-fill it when you share”).
- Replace the primary Generate invite link button with Share invite:
- call
api.invitations.create({ invitee_email, patient_id, invite_role, message }) - build the register URL (
/register?invite=<token>) as today - call
shareInvite(...) - surface a small toast/label per
ShareOutcome(“Shared”, “Opened your mail app”, “Link copied”)
- call
- Keep a secondary Copy link control (and the code fallback) — unchanged, for power users and the Tier-3 path.
- Keep the Active invites list; show
invitee_emailwhen present (“Carer for X · sarah@x.com · expires …”) plus a Re-share button that re-runsshareInviteon the existing token (no new record created). - Preserve existing defaults that reduce friction: auto-select patient when only one exists; default role Carer.
- Respect component length limit (300 lines) — the panel is already large, so the
share logic lives in
lib/share.ts, not inline.
6. Files touched
Section titled “6. Files touched”| File | Change |
|---|---|
backend/migrations/V018__invitations_invitee_email.sql | new — add column |
backend/schema.sql | add invitee_email to invitations DDL |
backend/routers/invitations.py | model + insert + response field |
openapi/openapi.json | regenerated |
frontend/src/lib/api.types.ts | regenerated (no hand-edit) |
frontend/src/lib/api.ts | Invitation.invitee_email, create body |
frontend/src/lib/share.ts | new — share ladder helper |
frontend/src/components/settings/TeamMembersPanel.tsx | email field, Share button, re-share, list display |
7. Testing
Section titled “7. Testing”- Backend (
backend/tests/test_invitations.py): extend to assertinvitee_emailround-trips on create and appears in list; null case still works. - Frontend (
frontend/src/test/): unit-testshareInvitewith mockednavigator.sharepresent/absent and clipboard fallback; assertAbortErrormaps to'cancelled'. - Manual matrix (mobile + desktop per rules):
- iOS Safari / Android Chrome → native sheet, pick WhatsApp + Mail
- Desktop Chrome/Edge → OS share sheet
- Desktop Firefox →
mailto:opens default mail app - Clipboard fallback path → toast shows, link valid
- Registration via shared link still consumes token + requires admin approval
8. Rollout & risk
Section titled “8. Rollout & risk”- No breaking change: new column is nullable; existing invites and the copy-link path keep working. Web Share is purely additive.
- Deliverability/tracking gap: neither share nor mailto confirms delivery — mitigated by the Active invites list + Re-share.
- Ships via
push-to-production.ps1(migration runs on app startup at each tier; full backup automatic). Post-deploy: verify newsw.jsCACHE_NAME,no-storeonindex.html, and API smoke per the deploy-cache rule.
9. Estimate
Section titled “9. Estimate”Small–medium: 1 migration, ~15 backend lines, 1 new frontend helper (~40 lines), UI edits to one panel, contract regen, tests. Roughly a half-day including the manual device matrix.