Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 210 additions & 6 deletions mobile/src/screens/gift/GiftShareScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* mobile/src/screens/gift/GiftShareScreen.tsx
*
* Production-ready subscription gift-share screen for SubTrackr mobile.
* Enables users to transfer/pass a subscription to another user via email
* or Stellar public address with shareable link generation and telemetry.
* Enables users to transfer/pass an active subscription to another user via email
* or Stellar public address with shareable link generation, error retry, and telemetry.
*/

import React, { useEffect } from 'react';
Expand Down Expand Up @@ -42,12 +42,16 @@ export const GiftShareScreen: React.FC<GiftShareScreenProps> = ({
setPersonalMessage,
expiresInDays,
setExpiresInDays,
allowSenderRevoke,
setAllowSenderRevoke,
recipientValidation,
error,
giftResult,
isCopied,
handleSelectSubscription,
handleProceed,
handleRetry,
handleEditDetails,
handleBack,
handleCopyLink,
handleShareLink,
Expand All @@ -72,7 +76,9 @@ export const GiftShareScreen: React.FC<GiftShareScreenProps> = ({
<View style={styles.successBadge}>
<Text style={styles.successCheck}>✓</Text>
</View>
<Text style={styles.title}>Gift Share Ready!</Text>
<Text style={styles.title} accessibilityRole="header">
Gift Share Ready!
</Text>
<Text style={styles.subtitle}>
Your subscription to {giftResult.subscriptionName} can now be claimed by{' '}
{giftResult.recipient}.
Expand Down Expand Up @@ -158,6 +164,59 @@ export const GiftShareScreen: React.FC<GiftShareScreenProps> = ({
);
}

// Dedicated Error State with Retry & Edit Support
if (step === 'error') {
return (
<View style={styles.centerContainer} testID="gift-share-error-view">
<View style={styles.errorBadge}>
<Text style={styles.errorBadgeIcon}>✕</Text>
</View>
<Text style={styles.title} accessibilityRole="header">
Gift Share Failed
</Text>
<Text style={styles.subtitle}>
We encountered an issue creating your subscription transfer link.
</Text>

<View style={styles.errorCard} testID="gift-error-card" accessibilityRole="alert">
<Text style={styles.errorCardLabel}>Error Details</Text>
<Text style={styles.errorCardText}>
{error || 'An unexpected failure occurred while creating your gift share ticket.'}
</Text>
</View>

<View style={styles.errorActionRow}>
<TouchableOpacity
style={[styles.primaryButton, styles.retryButton]}
onPress={handleRetry}
accessibilityRole="button"
accessibilityLabel="Retry sending gift"
testID="gift-retry-button">
<Text style={styles.primaryButtonText}>↻ Retry Send</Text>
</TouchableOpacity>

<TouchableOpacity
style={styles.editButton}
onPress={handleEditDetails}
accessibilityRole="button"
accessibilityLabel="Edit recipient details"
testID="gift-edit-button">
<Text style={styles.editButtonText}>Edit Details</Text>
</TouchableOpacity>
</View>

<TouchableOpacity
style={styles.doneButton}
onPress={onBack || handleReset}
accessibilityRole="button"
accessibilityLabel="Cancel gift share"
testID="gift-cancel-button">
<Text style={styles.doneButtonText}>Cancel</Text>
</TouchableOpacity>
</View>
);
}

const isNextDisabled =
(step === 'select' && !selectedSubscription) ||
(step === 'recipient' && !recipientValidation.isValid);
Expand Down Expand Up @@ -223,7 +282,11 @@ export const GiftShareScreen: React.FC<GiftShareScreenProps> = ({

{/* STEP 1: Select Subscription */}
{step === 'select' && (
<View style={styles.stepSection} testID="step-select-subscription">
<View
style={styles.stepSection}
testID="step-select-subscription"
accessibilityRole="radiogroup"
accessibilityLabel="Select subscription to gift">
<Text style={styles.sectionTitle}>1. Select Subscription</Text>
<Text style={styles.sectionSubtitle}>
Choose an active subscription to transfer or share.
Expand Down Expand Up @@ -353,13 +416,18 @@ export const GiftShareScreen: React.FC<GiftShareScreenProps> = ({

{/* Expiry Options */}
<Text style={styles.fieldLabel}>Claim Expiration Window</Text>
<View style={styles.expiryRow}>
<View
style={styles.expiryRow}
accessibilityRole="radiogroup"
accessibilityLabel="Claim expiration window">
{[3, 7, 14, 30].map((days) => (
<TouchableOpacity
key={days}
style={[styles.expiryChip, expiresInDays === days && styles.expiryChipActive]}
onPress={() => setExpiresInDays(days)}
accessibilityRole="button"
accessibilityRole="radio"
accessibilityState={{ selected: expiresInDays === days }}
accessibilityLabel={`${days} days expiration`}
testID={`expiry-chip-${days}`}>
<Text
style={[
Expand All @@ -372,6 +440,26 @@ export const GiftShareScreen: React.FC<GiftShareScreenProps> = ({
))}
</View>

{/* Revocation Safeguard Toggle */}
<TouchableOpacity
style={styles.toggleCard}
onPress={() => setAllowSenderRevoke(!allowSenderRevoke)}
accessibilityRole="switch"
accessibilityState={{ checked: allowSenderRevoke }}
accessibilityLabel="Allow sender revocation"
accessibilityHint="Allows you to cancel this gift if recipient does not claim it"
testID="allow-sender-revoke-toggle">
<View style={styles.toggleInfo}>
<Text style={styles.toggleTitle}>Allow Sender Revocation</Text>
<Text style={styles.toggleDescription}>
Retain ability to cancel this gift if unclaimed before expiration.
</Text>
</View>
<View style={[styles.switchTrack, allowSenderRevoke && styles.switchTrackActive]}>
<View style={[styles.switchThumb, allowSenderRevoke && styles.switchThumbActive]} />
</View>
</TouchableOpacity>

{/* Personal Message */}
<View style={styles.messageLabelRow}>
<Text style={styles.fieldLabel}>Personal Message (Optional)</Text>
Expand Down Expand Up @@ -438,6 +526,12 @@ export const GiftShareScreen: React.FC<GiftShareScreenProps> = ({
<Text style={styles.summaryLabel}>Valid For</Text>
<Text style={styles.summaryValue}>{expiresInDays} Days</Text>
</View>
<View style={styles.summaryRow}>
<Text style={styles.summaryLabel}>Revocation Policy</Text>
<Text style={styles.summaryValue}>
{allowSenderRevoke ? 'Revocable until claimed' : 'Non-revocable'}
</Text>
</View>
{personalMessage.length > 0 && (
<View style={styles.messagePreviewBox}>
<Text style={styles.messagePreviewLabel}>Note Attached:</Text>
Expand Down Expand Up @@ -781,6 +875,53 @@ const styles = StyleSheet.create({
color: '#38BDF8',
fontWeight: '700',
},
toggleCard: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
backgroundColor: '#1E293B',
borderWidth: 1,
borderColor: '#334155',
borderRadius: 8,
padding: 12,
marginVertical: 10,
minHeight: 48,
},
toggleInfo: {
flex: 1,
marginRight: 12,
},
toggleTitle: {
fontSize: 13,
fontWeight: '600',
color: '#F8FAFC',
marginBottom: 2,
},
toggleDescription: {
fontSize: 11,
color: '#94A3B8',
lineHeight: 16,
},
switchTrack: {
width: 44,
height: 24,
borderRadius: 12,
backgroundColor: '#334155',
padding: 2,
justifyContent: 'center',
},
switchTrackActive: {
backgroundColor: '#3B82F6',
},
switchThumb: {
width: 20,
height: 20,
borderRadius: 10,
backgroundColor: '#FFFFFF',
},
switchThumbActive: {
alignSelf: 'flex-end',
},
messageLabelRow: {
flexDirection: 'row',
justifyContent: 'space-between',
Expand Down Expand Up @@ -878,6 +1019,69 @@ const styles = StyleSheet.create({
color: '#10B981',
fontWeight: '800',
},
errorBadge: {
width: 64,
height: 64,
borderRadius: 32,
backgroundColor: '#7F1D1D',
borderWidth: 2,
borderColor: '#EF4444',
justifyContent: 'center',
alignItems: 'center',
marginBottom: 16,
},
errorBadgeIcon: {
fontSize: 28,
color: '#EF4444',
fontWeight: '800',
},
errorCard: {
backgroundColor: '#1E293B',
borderWidth: 1,
borderColor: '#EF4444',
borderRadius: 10,
padding: 16,
width: '100%',
marginVertical: 16,
},
errorCardLabel: {
fontSize: 11,
fontWeight: '700',
color: '#F87171',
textTransform: 'uppercase',
letterSpacing: 1,
marginBottom: 6,
},
errorCardText: {
fontSize: 13,
color: '#FCA5A5',
lineHeight: 20,
},
errorActionRow: {
flexDirection: 'row',
width: '100%',
marginBottom: 12,
},
retryButton: {
flex: 1,
backgroundColor: '#2563EB',
marginRight: 8,
},
editButton: {
flex: 1,
backgroundColor: '#334155',
borderRadius: 10,
paddingVertical: 14,
alignItems: 'center',
justifyContent: 'center',
minHeight: 48,
marginLeft: 8,
},
editButtonText: {
color: '#F8FAFC',
fontSize: 14,
fontWeight: '600',
},
claimCodeCard: {
backgroundColor: '#1E293B',
borderWidth: 1.5,
Expand Down
45 changes: 27 additions & 18 deletions mobile/src/screens/gift/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A production-ready, accessible, and self-contained gift-sharing and transfer sur

## Overview

Enables users to gift or transfer active software subscriptions to friends, colleagues, or team members via email or native Stellar blockchain addresses (`G...`), generating verifiable claim codes and shareable deep links.
Enables users to gift or transfer active software subscriptions to friends, colleagues, or team members via email or native Stellar blockchain addresses (`G...`), generating verifiable claim codes and shareable deep links with error recovery and sender revocation safeguards.

## Key Capabilities

Expand All @@ -14,18 +14,23 @@ Enables users to gift or transfer active software subscriptions to friends, coll
2. **Deterministic Code & Deep-Link Generation**:
- Secure uppercase alphanumeric claim codes (`SUB-XXXX-XXXX`).
- Deep-link claim URL construction (`https://subtrackr.app/gift/claim?code=...`).
3. **Configurable Expiry & Revocation**:
3. **Configurable Expiry & Revocation Safeguards**:
- Customizable claim window (3, 7, 14, or 30 days) with deterministic expiry calculation.
- Revocation safeguards: sender billing continues until the recipient claims the gift, allowing the sender to revoke unclaimed links.
- Sender revocation toggle (`allowSenderRevoke`) persisted in transfer state.
4. **Multi-Delivery Methods**:
- `share_link`: Copy link to clipboard or trigger native OS share sheet.
- `direct_email`: Sends claim notification email to recipient.
- `in_app`: Prepares in-app account transfer ticket.
5. **Observability & Telemetry**:
- Structured metrics hooks: `onView`, `onSelectSubscription`, `onStepChange`, `onGiftShareStart`, `onGiftShareSuccess`, `onGiftShareFailure`, `onCopyLink`, `onShareLink`, `onReset`.
5. **Resilient Error Recovery & Retry**:
- Dedicated `error` state view with structured error messaging.
- One-tap `Retry Send` re-attempts transaction without losing form context.
- `Edit Details` action navigates back to recipient inputs to fix validation or addressing issues.
6. **Observability & Telemetry**:
- Structured metrics hooks: `onView`, `onSelectSubscription`, `onStepChange`, `onGiftShareStart`, `onGiftShareSuccess`, `onGiftShareFailure`, `onRetry`, `onCopyLink`, `onShareLink`, `onReset`.
- Pluggable logger interface (`info`, `warn`, `error`).
6. **Accessibility (WCAG 2.1 AA Compliant)**:
- Proper `accessibilityRole` (`radio`, `tab`, `button`, `alert`, `progressbar`, `header`).
7. **Accessibility (WCAG 2.1 AA Compliant)**:
- Proper `accessibilityRole` (`radio`, `radiogroup`, `switch`, `tab`, `tablist`, `button`, `alert`, `progressbar`, `header`).
- High-contrast visual cues (#0F172A background, #1E293B surface, #3B82F6 primary, #10B981 success, #EF4444 error).
- Minimum touch target dimensions (>= 48x48dp) across buttons, tabs, chips, and radio cards.

Expand All @@ -35,16 +40,19 @@ Enables users to gift or transfer active software subscriptions to friends, coll

```
[select] ──> [recipient] ──> [confirm] ──> [processing] ──> [success]
│ │ │
└─────────────┴───────────────┴─────────────────────────> [error]
│ │ │ │
│ │ │ v
└─────────────┴───────────────┴───────> [error]
│
(Retry / Edit Details)
```

1. `select`: User chooses an eligible subscription from their active list. Ineligible subscriptions (paused/canceled) are disabled with clear status notes.
2. `recipient`: User inputs recipient email or Stellar address (with instant validation), delivery method, expiry window, and optional personal note (up to 250 chars).
3. `confirm`: Summary review card highlighting subscription value, recipient details, and transfer conditions.
2. `recipient`: User inputs recipient email or Stellar address (with instant validation), delivery method, expiry window, sender revocation toggle, and optional personal note (up to 250 chars).
3. `confirm`: Summary review card highlighting subscription value, recipient details, expiration, revocation policy, and transfer conditions.
4. `processing`: Accessible spinner while registering the claim ticket.
5. `success`: Claim code, copyable link with clipboard feedback, native share sheet trigger, and done action.
6. `error`: Alert banner with retry support.
6. `error`: Accessible failure card with error details, one-tap `Retry Send`, and `Edit Details` navigation.

---

Expand All @@ -64,23 +72,24 @@ import { GiftShareScreen } from './screens/gift';
deliveryMethod: formState.deliveryMethod,
personalMessage: formState.personalMessage,
expiresInDays: formState.expiresInDays,
allowSenderRevoke: formState.allowSenderRevoke,
});
}}
onBack={() => navigation.goBack()}
metrics={{
onGiftShareSuccess: (result) => analytics.track('gift_created', result),
onGiftShareFailure: (error) => analytics.track('gift_failed', { error }),
onRetry: () => analytics.track('gift_retry'),
}}
logger={console}
/>
```

---

## Test Coverage

Unit tests in `__tests__/giftShareLogic.test.ts` and `__tests__/GiftShareScreen.test.tsx` cover:
- Stellar public key and email regex validation.
- Recipient auto-detection and error messages.
- Expiration calculations and clamping.
- Unique claim code formatting and URL building.
- Personal message character length limits.
- Screen rendering, step transitions, input updates, clipboard copying, native sharing, error handling, and telemetry firing.
Unit and integration tests cover:
- **`__tests__/giftShareLogic.test.ts`**: Stellar public key and email regex validation, recipient auto-detection, expiry calculations and clamping, claim code formatting, eligibility checks, and URL building.
- **`__tests__/hooks.test.ts`**: Complete state machine lifecycle (`select` -> `recipient` -> `confirm` -> `processing` -> `success` / `error`), validation guards, error recovery and retry, clipboard copying, native share sheet invocation, state resets, and full telemetry firing.
- **`__tests__/GiftShareScreen.test.tsx`**: Component mounting, error views, processing indicators, props forwarding, and accessibility labels.
Loading
Loading