Skip to content

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.

  1. Invite Carers page: pick “Caring for” (required) → Generate invite.
  2. 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 via qrcode, so the token never leaves the browser).
  3. 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.
  4. 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.
  5. 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).


Inviting a carer or observer today is high-friction for the administrator (frontend/src/components/settings/TeamMembersPanel.tsx, backend/routers/invitations.py):

  1. Select the patient (“Caring for”)
  2. Pick a role
  3. Optionally write a message
  4. Click Generate invite link
  5. Manually copy the link or code
  6. 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.

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:

yes

no

yes

no

Admin taps Share invite

Create invitation via API - background

navigator.canShare available?

Native OS share sheet

WhatsApp, Messages, Mail, AirDrop, ...

Email address entered

and mailto viable?

Open default mail app via mailto:

Copy link to clipboard + toast

Invitation stays in Active invites for re-share

  • 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 without navigator.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)”
#DecisionDefault takenRationale
D1Post-registration approvalKeep the existing admin approval stepSafety; email/share is delivery only, not identity proof
D2Persist invited emailYes — store invitee_email on invitationsLets admin see “Invited sarah@x.com — pending”, re-share, and pre-fill; enables optional match on register
D3Email match on registerSoft — store + display only; do not hard-block a mismatched email at registrationAvoids 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.1 Database — migration V018__invitations_invitee_email.sql

Section titled “5.1 Database — migration V018__invitations_invitee_email.sql”
  • Add nullable column invitee_email TEXT to invitations.
  • Mirror the same ADD COLUMN IF NOT EXISTS in backend/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: add invitee_email: str | None = None (Pydantic EmailStr-style validation optional; keep lenient — empty/None allowed).
  • create_invitation: persist invitee_email in the INSERT ... RETURNING * (parameterised %s, no f-strings).
  • list_invitations: invitee_email flows 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”
  • Invitation interface: add invitee_email?: string | null.
  • invitations.create: accept and send invitee_email in 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:
    1. call api.invitations.create({ invitee_email, patient_id, invite_role, message })
    2. build the register URL (/register?invite=<token>) as today
    3. call shareInvite(...)
    4. surface a small toast/label per ShareOutcome (“Shared”, “Opened your mail app”, “Link copied”)
  • 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_email when present (“Carer for X · sarah@x.com · expires …”) plus a Re-share button that re-runs shareInvite on 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.
FileChange
backend/migrations/V018__invitations_invitee_email.sqlnew — add column
backend/schema.sqladd invitee_email to invitations DDL
backend/routers/invitations.pymodel + insert + response field
openapi/openapi.jsonregenerated
frontend/src/lib/api.types.tsregenerated (no hand-edit)
frontend/src/lib/api.tsInvitation.invitee_email, create body
frontend/src/lib/share.tsnew — share ladder helper
frontend/src/components/settings/TeamMembersPanel.tsxemail field, Share button, re-share, list display
  • Backend (backend/tests/test_invitations.py): extend to assert invitee_email round-trips on create and appears in list; null case still works.
  • Frontend (frontend/src/test/): unit-test shareInvite with mocked navigator.share present/absent and clipboard fallback; assert AbortError maps 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
  • 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 new sw.js CACHE_NAME, no-store on index.html, and API smoke per the deploy-cache rule.

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.