()
| 15 | }; |
| 16 | |
| 17 | export const usePlusSubscription = (): { |
| 18 | isPlus: boolean; |
| 19 | plusProvider?: SubscriptionProvider; |
| 20 | logSubscriptionEvent: (event: LogSubscriptionEvent) => void; |
| 21 | plusHref: string | undefined; |
| 22 | status?: SubscriptionStatus; |
| 23 | } => { |
| 24 | const { user } = useAuthContext(); |
| 25 | const { logEvent } = useLogContext(); |
| 26 | const isPlus = user?.isPlus || false; |
| 27 | const { status, provider: plusProvider } = user?.subscriptionFlags || {}; |
| 28 | |
| 29 | const logSubscriptionEvent = useCallback( |
| 30 | ({ event_name, target_id, extra }: LogSubscriptionEvent) => { |
| 31 | logEvent({ |
| 32 | event_name, |
| 33 | target_type: TargetType.Plus, |
| 34 | target_id, |
| 35 | extra: JSON.stringify(extra) || undefined, |
| 36 | }); |
| 37 | }, |
| 38 | [logEvent], |
| 39 | ); |
| 40 | |
| 41 | const plusHref = useMemo(() => { |
| 42 | if (!isPlus) { |
| 43 | return plusUrl; |
| 44 | } |
| 45 | |
| 46 | // Plus users with Apple StoreKit on iOS get no URL (handled by onClick) |
| 47 | if (isIOSNative() && plusProvider === SubscriptionProvider.AppleStoreKit) { |
| 48 | return undefined; |
| 49 | } |
| 50 | |
| 51 | // External subscription management for Paddle users |
| 52 | if (plusProvider === SubscriptionProvider.Paddle) { |
| 53 | return managePlusUrl; |
| 54 | } |
| 55 | |
| 56 | // Internal subscription management for other cases |
| 57 | return `${webappUrl}account/subscription`; |
| 58 | }, [isPlus, plusProvider]); |
| 59 | |
| 60 | return { |
| 61 | isPlus, |
| 62 | plusProvider, |
| 63 | logSubscriptionEvent, |
| 64 | plusHref, |
| 65 | status, |
| 66 | }; |
| 67 | }; |
no test coverage detected