Files
accounted/contexts/CompanyContext.tsx
T
Jakob Wennberg 3a2c57a167 feat(billing): paywall conversion pass (deferred first charge, trial touchpoint, sell-view upgrade) (#991)
* feat(billing): paywall conversion pass: deferred first charge, trial touchpoint, sell-view upgrade

- checkout passes subscription_data.trial_end (trial grant expiry, 49h floor)
  so a mid-trial upgrade costs 0 kr today instead of double-billing days the
  company already has free; billing/status counts 'trialing' as paying
- trial countdown pill in the sidebar (CompanyContext.trialEndsAt via
  getCompanyEntitlements); hidden for sandbox, dev bypass, and once any
  non-trial grant is active
- sell view: what-happens-when timeline, free-vs-paid comparison table,
  risk-reversal copy + chevron CTA, post-checkout confirmation state

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(billing): review triage: fail-closed trial lookup, hourly countdown refresh, BFL retention note

- checkout returns 500 (no Stripe session) when the trial-grant lookup errors,
  instead of silently charging immediately after the UI promised 0 kr idag
- sidebar trial countdown recomputes hourly so a long-lived tab stays honest
- sell-view retention copy states BFL 7-year retention explicitly
  (compliance-bot suggestion)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: retrigger CI (pull_request event delivery stuck)

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 22:29:48 +02:00

57 lines
1.7 KiB
TypeScript

'use client'
import { createContext, useContext } from 'react'
import type { Company, CompanyRole, Team } from '@/types'
import type { CapabilityKey } from '@/lib/entitlements/keys'
interface CompanyContextValue {
company: Company | null
role: CompanyRole | null
companies: { company: Company; role: CompanyRole }[]
isTeamMember: boolean
team: Team | null
isSandbox: boolean
/** PAID capability keys the active company currently holds (entitled + enabled). */
capabilities: CapabilityKey[]
/**
* Trial expiry while the trial is the company's only source of paid access;
* null when paying/comped or after the trial lapsed. Drives the countdown
* touchpoint in the sidebar.
*/
trialEndsAt: string | null
}
const CompanyContext = createContext<CompanyContextValue | null>(null)
export function CompanyProvider({
children,
value,
}: {
children: React.ReactNode
value: CompanyContextValue
}) {
return <CompanyContext.Provider value={value}>{children}</CompanyContext.Provider>
}
export function useCompany() {
const ctx = useContext(CompanyContext)
if (!ctx) throw new Error('useCompany must be used within CompanyProvider')
return ctx
}
export function useCompanyOptional() {
return useContext(CompanyContext)
}
/**
* Whether the active company holds a given paid capability. Controls UI
* affordances only: the server gate (lib/entitlements) is the real enforcement.
* Fail-open when rendered outside a CompanyProvider (e.g. standalone dialogs):
* the server still blocks; this only decides whether to show/disable/upsell.
*/
export function useCapability(key: CapabilityKey): boolean {
const ctx = useContext(CompanyContext)
if (!ctx) return true
return ctx.capabilities.includes(key)
}