@antelopejs/dms-saas 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/README.md +6 -0
  2. package/dist/billing-state/recovery.js +2 -2
  3. package/dist/billing-state/recovery.js.map +1 -1
  4. package/dist/stripe/client.d.ts +3 -0
  5. package/dist/stripe/client.js +16 -1
  6. package/dist/stripe/client.js.map +1 -1
  7. package/dist/stripe/customer-billing.js +1 -1
  8. package/dist/stripe/customer-billing.js.map +1 -1
  9. package/dist/stripe/subscription-cancellation.js +1 -1
  10. package/dist/stripe/subscription-cancellation.js.map +1 -1
  11. package/dist/stripe/upcoming-invoice.js +3 -3
  12. package/dist/stripe/upcoming-invoice.js.map +1 -1
  13. package/dist/types/config.d.ts +2 -0
  14. package/dist/types/config.js.map +1 -1
  15. package/frontend-vue/app/build/PayInvoicePanel.vue +4 -8
  16. package/frontend-vue/app/build/PlanChangeReview.vue +3 -6
  17. package/frontend-vue/app/build/segments/useSegmentCatalog.ts +7 -12
  18. package/frontend-vue/app/build/useWorkspaceName.ts +1 -2
  19. package/frontend-vue/app/build/users/useUserDetail.ts +8 -10
  20. package/frontend-vue/app/build/workspace-detail/ComplimentaryDialog.vue +5 -3
  21. package/frontend-vue/app/build/workspace-detail/CreditDialog.vue +5 -3
  22. package/frontend-vue/app/build/workspace-detail/UpgradeDialog.vue +14 -7
  23. package/frontend-vue/app/build/workspace-detail/useWorkspaceActions.ts +11 -10
  24. package/frontend-vue/app/build/workspace-detail/useWorkspaceOverview.ts +16 -30
  25. package/frontend-vue/app/components/CustomerPortalButton.vue +6 -4
  26. package/frontend-vue/app/components/DataExport.vue +34 -54
  27. package/frontend-vue/app/components/DataExportHistory.vue +2 -7
  28. package/frontend-vue/app/components/IssueCreditNoteModal.vue +5 -4
  29. package/frontend-vue/app/components/MigrationStripeCheck.vue +11 -8
  30. package/frontend-vue/app/components/PastDueAlert.vue +8 -6
  31. package/frontend-vue/app/components/PastDueBanner.vue +4 -4
  32. package/frontend-vue/app/components/PaymentMethodCard.vue +6 -11
  33. package/frontend-vue/app/components/PlanAccessEditor.vue +31 -30
  34. package/frontend-vue/app/components/PlanCard.vue +2 -3
  35. package/frontend-vue/app/components/PlanEditorPreview.vue +27 -36
  36. package/frontend-vue/app/components/PlanUpgradeModal.vue +1 -1
  37. package/frontend-vue/app/components/PlatformNotes.vue +27 -28
  38. package/frontend-vue/app/components/Pricing.vue +16 -23
  39. package/frontend-vue/app/components/RefundButton.vue +61 -46
  40. package/frontend-vue/app/components/RetirePlanModal.vue +13 -7
  41. package/frontend-vue/app/components/SeatQuotaBanner.vue +9 -10
  42. package/frontend-vue/app/components/SegmentConditionsBuilder.vue +0 -1
  43. package/frontend-vue/app/components/SegmentRulesText.vue +1 -3
  44. package/frontend-vue/app/components/TenantBillingForm.vue +14 -19
  45. package/frontend-vue/app/components/TenantPlanCard.vue +18 -21
  46. package/frontend-vue/app/components/TrialEndingBanner.vue +5 -10
  47. package/frontend-vue/app/components/UserPlatformRoleCard.vue +8 -9
  48. package/frontend-vue/app/components/UserSegmentMatches.vue +8 -16
  49. package/frontend-vue/app/components/UserSummaryCard.vue +2 -4
  50. package/frontend-vue/app/components/WorkspaceAdminCreateModal.vue +13 -5
  51. package/frontend-vue/app/components/WorkspaceCreateModal.vue +5 -1
  52. package/frontend-vue/app/components/WorkspaceDangerZone.vue +8 -7
  53. package/frontend-vue/app/components/WorkspaceDetailHeader.vue +14 -4
  54. package/frontend-vue/app/components/WorkspaceSuspended.vue +27 -32
  55. package/frontend-vue/app/components/WorkspaceSwitcherWidget.vue +5 -16
  56. package/frontend-vue/app/composables/useBillingIdentity.ts +9 -5
  57. package/frontend-vue/app/composables/useBillingStatus.ts +8 -7
  58. package/frontend-vue/app/composables/useCurrentWorkspace.ts +6 -8
  59. package/frontend-vue/app/composables/useExportHistory.ts +67 -78
  60. package/frontend-vue/app/composables/useMountedData.ts +27 -0
  61. package/frontend-vue/app/composables/useMyWorkspaces.ts +24 -27
  62. package/frontend-vue/app/composables/useNextInvoice.ts +7 -6
  63. package/frontend-vue/app/composables/useOnDemandRead.ts +49 -0
  64. package/frontend-vue/app/composables/usePendingCheckout.ts +2 -1
  65. package/frontend-vue/app/composables/usePlanCatalog.ts +3 -9
  66. package/frontend-vue/app/composables/usePublicPricing.ts +24 -0
  67. package/frontend-vue/app/composables/useSaasRegistration.ts +2 -0
  68. package/frontend-vue/app/composables/useSeatQuota.ts +2 -6
  69. package/frontend-vue/app/composables/useTenantPlan.ts +26 -9
  70. package/frontend-vue/app/composables/useWorkspaceOverview.ts +2 -6
  71. package/frontend-vue/package.json +1 -1
  72. package/package.json +4 -4
  73. package/frontend-vue/app/composables/useServerConfirm.ts +0 -44
  74. package/frontend-vue/app/composables/useSharedRequest.ts +0 -54
  75. package/frontend-vue/app/composables/useWorkspaceAccessCache.ts +0 -52
@@ -1,5 +1,5 @@
1
1
  <script setup lang="ts">
2
- import { computed, onMounted, watch } from 'vue'
2
+ import { computed, watch } from 'vue'
3
3
  import { userDisplayName, useUserDetail } from '../build/users/useUserDetail'
4
4
 
5
5
  // The identity header of the user page: who the user is and how their
@@ -22,14 +22,12 @@ watch(avatarKey, (key) => key && resolve(key), { immediate: true })
22
22
  const avatarSrc = computed(() =>
23
23
  avatarKey.value ? getUrl(avatarKey.value) : undefined,
24
24
  )
25
-
26
- onMounted(() => void detail.refresh())
27
25
  </script>
28
26
 
29
27
  <template>
30
28
  <div class="flex flex-col gap-4">
31
29
  <DmsSaasLoadFailure
32
- v-if="detail.error.value && !user"
30
+ v-if="detail.status.value === 'error' && !user"
33
31
  :title="$t('saas.users.load_failed')"
34
32
  @retry="detail.refresh()"
35
33
  />
@@ -150,11 +150,18 @@ const whatHappens = computed(() => {
150
150
  return [created, ownerSentence.value].filter(Boolean).join(' ')
151
151
  })
152
152
 
153
+ const plansRead = useOnDemandRead<PlanOption[]>(() => PLANS_ENDPOINT)
154
+ const lookedUpEmail = ref('')
155
+ const ownerRead = useOnDemandRead<OwnerLookup>(
156
+ () => (lookedUpEmail.value ? LOOKUP_ENDPOINT : null),
157
+ { query: () => ({ email: lookedUpEmail.value }) },
158
+ )
159
+
153
160
  async function loadPlans(): Promise<void> {
154
161
  isLoading.value = true
155
162
  loadError.value = null
156
163
  try {
157
- const all = await $authFetch<PlanOption[]>(PLANS_ENDPOINT)
164
+ const all = await plansRead.read()
158
165
  plans.value = all.filter((option) => option.isActive && !option.isDeleted)
159
166
  } catch (error) {
160
167
  loadError.value = resolveApiError(error, `${C}.load_error`)
@@ -164,12 +171,13 @@ async function loadPlans(): Promise<void> {
164
171
  }
165
172
 
166
173
  async function lookUpOwner(email: string): Promise<void> {
174
+ lookedUpEmail.value = email
167
175
  try {
168
- lookup.value = await $authFetch<OwnerLookup>(LOOKUP_ENDPOINT, {
169
- query: { email },
170
- })
176
+ const found = await ownerRead.read()
177
+ // A newer address was typed while this one was looked up.
178
+ if (lookedUpEmail.value === email) lookup.value = found
171
179
  } catch {
172
- lookup.value = null
180
+ if (lookedUpEmail.value === email) lookup.value = null
173
181
  }
174
182
  }
175
183
 
@@ -161,6 +161,8 @@ async function initStripeElement(): Promise<void> {
161
161
  cardError.value = t(`${KEYS}.error.card_setup`)
162
162
  return
163
163
  }
164
+ // Not a `useApiData` read: each call creates a SetupIntent, whose client
165
+ // secret is the card form's alone, never cached nor shared.
164
166
  const setup = await $authFetch<SetupIntent>(SETUP_INTENT_ENDPOINT)
165
167
  stripeHandle.value = useStripePaymentElement({
166
168
  publishableKey: publishableKey.value,
@@ -190,11 +192,13 @@ function ensureCardStep(): Promise<void> {
190
192
 
191
193
  watch(asksForCard, ensureCardStep)
192
194
 
195
+ const optionsRead = useOnDemandRead<CreateOptions>(() => OPTIONS_ENDPOINT)
196
+
193
197
  async function load(): Promise<void> {
194
198
  isLoading.value = true
195
199
  loadError.value = null
196
200
  try {
197
- options.value = await $authFetch<CreateOptions>(OPTIONS_ENDPOINT)
201
+ options.value = await optionsRead.read()
198
202
  selectedPlanId.value ??= options.value.plans[0]?._id ?? null
199
203
  } catch (error) {
200
204
  loadError.value = resolveApiError(error, `${KEYS}.error.load_description`)
@@ -1,5 +1,5 @@
1
1
  <script setup lang="ts">
2
- import { computed, h, onMounted } from 'vue'
2
+ import { computed, h } from 'vue'
3
3
  import { DATA_EXPORT_PATH, HOME_PATH } from '../build/workspace-paths'
4
4
 
5
5
  const DELETE_ENDPOINT = '/api/saas/workspaces/current/delete'
@@ -17,7 +17,7 @@ const { $authFetch } = useAuthFetch()
17
17
  const { confirm } = useConfirm()
18
18
  const { resolveApiError } = useApiErrorMessage()
19
19
  const toast = useToast()
20
- const { data: overview, error, refresh } = useWorkspaceOverview()
20
+ const { data: overview, status, refresh } = useWorkspaceOverview()
21
21
  // The deletion asks for the name as it is now: read again after a rename.
22
22
  useWorkspaceRenamed(() => void refresh())
23
23
 
@@ -142,8 +142,6 @@ async function openDeleteConfirm(): Promise<void> {
142
142
  onConfirm: () => deleteWorkspace(view),
143
143
  })
144
144
  }
145
-
146
- onMounted(refresh)
147
145
  </script>
148
146
 
149
147
  <template>
@@ -165,12 +163,15 @@ onMounted(refresh)
165
163
  </DmsFieldRow>
166
164
  <DmsFieldRow :label="$t(`${KEYS}.delete.title`)">
167
165
  <template #details>
168
- <USkeleton v-if="!overview && !error" class="mt-1 h-10 w-full" />
166
+ <USkeleton
167
+ v-if="!overview && status !== 'error'"
168
+ class="mt-1 h-10 w-full"
169
+ />
169
170
  <DmsSaasLoadFailure
170
- v-else-if="error && !overview"
171
+ v-else-if="status === 'error' && !overview"
171
172
  class="mt-1"
172
173
  :title="$t(`${KEYS}.glance.load_failed`)"
173
- @retry="refresh"
174
+ @retry="refresh()"
174
175
  />
175
176
  <div v-else class="text-muted mt-0.5 flex flex-col gap-1 text-[12.5px]">
176
177
  <p>{{ deleteSummary }}</p>
@@ -1,10 +1,13 @@
1
1
  <script setup lang="ts">
2
- import { computed, ref, watch } from 'vue'
2
+ import { computed, onMounted, ref, watch } from 'vue'
3
3
  import type { DropdownMenuItem } from '@nuxt/ui'
4
4
  import WorkspaceActionDialogs from '../build/workspace-detail/WorkspaceActionDialogs.vue'
5
5
  import { useWorkspaceActions } from '../build/workspace-detail/useWorkspaceActions'
6
6
  import { useWorkspaceOverview } from '../build/workspace-detail/useWorkspaceOverview'
7
- import type { WorkspaceDialog } from '../build/workspace-detail/types'
7
+ import type {
8
+ WorkspaceDialog,
9
+ WorkspaceOverview,
10
+ } from '../build/workspace-detail/types'
8
11
  import { workspaceInitials } from '../build/public/access'
9
12
 
10
13
  /**
@@ -146,13 +149,20 @@ const QUERY_ACTIONS: Record<string, () => void> = {
146
149
  }
147
150
 
148
151
  // The list's row menu opens a dialog here: run it once the workspace is
149
- // loaded, then drop the query so a reload does not open it again.
150
- watch(overview, async (current) => {
152
+ // loaded (the server may have rendered it already), then drop the query so a
153
+ // reload does not open it again.
154
+ async function runRequestedAction(
155
+ current: WorkspaceOverview | null,
156
+ ): Promise<void> {
151
157
  const requested = route.query[ACTION_QUERY]
152
158
  if (!current || typeof requested !== 'string') return
153
159
  const { [ACTION_QUERY]: _action, ...query } = route.query
154
160
  await router.replace({ query })
155
161
  QUERY_ACTIONS[requested]?.()
162
+ }
163
+
164
+ onMounted(() => {
165
+ watch(overview, runRequestedAction, { immediate: true })
156
166
  })
157
167
  </script>
158
168
 
@@ -4,7 +4,7 @@
4
4
  * how the workspace owner reopens it, whom a member should ask, and the
5
5
  * caller's other workspaces. Reachable while the access gate blocks.
6
6
  */
7
- import { computed, onMounted, ref } from 'vue'
7
+ import { computed, onMounted, ref, watch } from 'vue'
8
8
  import {
9
9
  ACCESS_REASON_VIEWS,
10
10
  workspaceInitials,
@@ -39,17 +39,22 @@ interface PortalSession {
39
39
  const { t, locale } = useI18n()
40
40
  const toast = useToast()
41
41
  const { $authFetch } = useAuthFetch()
42
- const { user, clear: clearSession } = useUserSession()
42
+ const { user, clear: clearSession } = useDmsUserSession()
43
43
  const { resolveApiError } = useApiErrorMessage()
44
44
  const { formatMinorUnits } = useMoneyFormat()
45
45
  const { statusView } = useSaasStatus()
46
- const cachedAccess = useWorkspaceAccessCache()
47
- const { data: billingStatusState } = useBillingStatus()
48
- const { data: tenantPlanState } = useTenantPlan()
49
46
 
50
- const access = ref<WorkspaceAccessDetails | null>(null)
51
- const isLoading = ref(true)
52
- const loadError = ref<string | null>(null)
47
+ const accessRead = useApiData<WorkspaceAccessDetails>(ACCESS_ENDPOINT)
48
+ const access = accessRead.data
49
+ const isLoading = computed(() => accessRead.pending.value)
50
+ const loadError = computed(() =>
51
+ accessRead.error.value
52
+ ? resolveApiError(
53
+ accessRead.error.value,
54
+ 'saas.public.suspended.load_error',
55
+ )
56
+ : null,
57
+ )
53
58
  const isOpeningPortal = ref(false)
54
59
 
55
60
  const reason = computed(() => access.value?.reason ?? null)
@@ -152,29 +157,8 @@ const mailtoOwner = computed(() =>
152
157
  : null,
153
158
  )
154
159
 
155
- /**
156
- * Drop the payloads cached while blocked before going back in: an SPA
157
- * navigation keeps them alive, and Billing would keep announcing a
158
- * suspension the owner just settled.
159
- */
160
- async function enterWorkspace(): Promise<void> {
161
- cachedAccess.value = null
162
- billingStatusState.value = null
163
- tenantPlanState.value = null
164
- await navigateDms(HOME_PATH)
165
- }
166
-
167
- async function loadAccess(): Promise<void> {
168
- isLoading.value = true
169
- loadError.value = null
170
- try {
171
- access.value = await $authFetch<WorkspaceAccessDetails>(ACCESS_ENDPOINT)
172
- if (!access.value.blocked) await enterWorkspace()
173
- } catch (error) {
174
- loadError.value = resolveApiError(error, 'saas.public.suspended.load_error')
175
- } finally {
176
- isLoading.value = false
177
- }
160
+ function loadAccess(): Promise<void> {
161
+ return accessRead.refresh()
178
162
  }
179
163
 
180
164
  function payInvoice(): void {
@@ -209,7 +193,18 @@ async function signOut(): Promise<void> {
209
193
  await navigateDms(LOGIN_PATH)
210
194
  }
211
195
 
212
- onMounted(loadAccess)
196
+ // A workspace no longer blocked is entered again, from the browser. The
197
+ // billing data read before the suspension is not shown here, so the pages
198
+ // past this one read it afresh.
199
+ onMounted(() => {
200
+ watch(
201
+ () => access.value?.blocked,
202
+ (blocked) => {
203
+ if (blocked === false) void navigateDms(HOME_PATH)
204
+ },
205
+ { immediate: true },
206
+ )
207
+ })
213
208
  </script>
214
209
 
215
210
  <template>
@@ -1,8 +1,11 @@
1
1
  <script setup lang="ts">
2
- import { computed, onMounted, ref, watch } from 'vue'
2
+ import { computed, ref, watch } from 'vue'
3
3
  import { HOME_PATH } from '../build/workspace-paths'
4
4
  import WorkspaceSwitcherRow from '../build/WorkspaceSwitcherRow.vue'
5
5
 
6
+ // Drawn as two roots: attributes have no single element to land on.
7
+ defineOptions({ inheritAttrs: false })
8
+
6
9
  const props = defineProps<{
7
10
  collapsed?: boolean
8
11
  }>()
@@ -25,8 +28,7 @@ const canCreateWorkspace = computed(
25
28
  ?.admissionMode !== 'invitation-only',
26
29
  )
27
30
 
28
- const { workspaces, isLoaded, refresh } = useMyWorkspaces()
29
- const hasFailed = ref(false)
31
+ const { workspaces, isLoaded, hasFailed, refresh: load } = useMyWorkspaces()
30
32
  const isOpen = ref(false)
31
33
  const isCreateOpen = ref(false)
32
34
  const searchQuery = ref('')
@@ -78,15 +80,6 @@ function initialOf(name: string): string {
78
80
  return (name.trim()[0] ?? '?').toUpperCase()
79
81
  }
80
82
 
81
- async function load(): Promise<void> {
82
- try {
83
- await refresh()
84
- hasFailed.value = false
85
- } catch {
86
- hasFailed.value = true
87
- }
88
- }
89
-
90
83
  function reportSwitchFailure(target: MyWorkspace | string): void {
91
84
  const name = typeof target === 'string' ? target : target.name
92
85
  toast.add({
@@ -136,10 +129,6 @@ async function onWorkspaceCreated(
136
129
  }
137
130
 
138
131
  useWorkspaceRenamed(() => void load())
139
-
140
- onMounted(() => {
141
- if (!isLoaded.value) void load()
142
- })
143
132
  </script>
144
133
 
145
134
  <template>
@@ -124,13 +124,17 @@ function toRequestBody(draft: BillingIdentityDraft) {
124
124
  }
125
125
  }
126
126
 
127
- /** Reads and saves the workspace billing identity. */
127
+ /** The workspace billing identity a form shows, read with the page. */
128
+ export function useBillingInfo(): ApiDataResult<BillingInfoResponse> {
129
+ return useApiData<BillingInfoResponse>(BILLING_INFO_ENDPOINT)
130
+ }
131
+
132
+ /** Saves the workspace billing identity, and reads it for a dialog on open. */
128
133
  export function useBillingIdentity() {
129
134
  const { $authFetch } = useAuthFetch()
130
-
131
- function load(): Promise<BillingInfoResponse> {
132
- return $authFetch<BillingInfoResponse>(BILLING_INFO_ENDPOINT)
133
- }
135
+ const { read: load } = useOnDemandRead<BillingInfoResponse>(
136
+ () => BILLING_INFO_ENDPOINT,
137
+ )
134
138
 
135
139
  function save(draft: BillingIdentityDraft): Promise<BillingInfoResponse> {
136
140
  return $authFetch<BillingInfoResponse>(BILLING_INFO_ENDPOINT, {
@@ -1,5 +1,6 @@
1
+ import type { ApiReadOptions } from './useMountedData'
2
+
1
3
  const BILLING_STATUS_ENDPOINT = '/api/saas/billing/status'
2
- const BILLING_STATUS_STATE_KEY = 'saas-billing-status'
3
4
 
4
5
  export interface PaymentMethodSummary {
5
6
  brand: string
@@ -42,12 +43,12 @@ export interface BillingStatusResponse {
42
43
  }
43
44
 
44
45
  /** Read by most billing blocks and the past-due banner; each call reaches
45
- * Stripe, so they share one request. */
46
- export function useBillingStatus(): SharedRequest<BillingStatusResponse> {
47
- const { $authFetch } = useAuthFetch()
48
- return useSharedRequest(BILLING_STATUS_STATE_KEY, () =>
49
- $authFetch<BillingStatusResponse>(BILLING_STATUS_ENDPOINT),
50
- )
46
+ * Stripe, so they share one request. The layout strips read it with
47
+ * `server: false` (see `useMountedData`). */
48
+ export function useBillingStatus(
49
+ options: ApiReadOptions = {},
50
+ ): ApiDataResult<BillingStatusResponse> {
51
+ return useApiData<BillingStatusResponse>(BILLING_STATUS_ENDPOINT, options)
51
52
  }
52
53
 
53
54
  const CARD_EXPIRY_YEAR_MODULO = 100
@@ -5,24 +5,22 @@ export interface CurrentWorkspace {
5
5
  }
6
6
 
7
7
  const CURRENT_ENDPOINT = '/api/saas/workspaces/current'
8
- const STATE_KEY = 'saas-current-workspace'
9
8
 
10
9
  interface CurrentWorkspaceHandle {
11
- workspace: Ref<CurrentWorkspace | null>
10
+ workspace: Readonly<Ref<CurrentWorkspace | null>>
12
11
  load: () => Promise<void>
13
12
  }
14
13
 
15
14
  /**
16
- * The current workspace's identity, read once and shared by the blocks that
17
- * show it (the plan card names it in its deletion copy).
15
+ * The current workspace's identity, read when a dialog needs it (the plan
16
+ * card names it in its cancellation copy).
18
17
  */
19
18
  export function useCurrentWorkspace(): CurrentWorkspaceHandle {
20
- const { $authFetch } = useAuthFetch()
21
- const workspace = useDmsState<CurrentWorkspace | null>(STATE_KEY, () => null)
19
+ const current = useOnDemandRead<CurrentWorkspace>(() => CURRENT_ENDPOINT)
22
20
 
23
21
  async function load(): Promise<void> {
24
- workspace.value = await $authFetch<CurrentWorkspace>(CURRENT_ENDPOINT)
22
+ await current.read()
25
23
  }
26
24
 
27
- return { workspace, load }
25
+ return { workspace: current.data, load }
28
26
  }
@@ -1,13 +1,15 @@
1
- import { computed, type ComputedRef } from 'vue'
1
+ import { computed, ref, type ComputedRef } from 'vue'
2
2
 
3
3
  const EXPORT_ENDPOINT = '/api/saas/data-export'
4
4
  const HISTORY_ENDPOINT = `${EXPORT_ENDPOINT}/history`
5
5
  const START_ENDPOINT = `${EXPORT_ENDPOINT}/start`
6
- const STATUS_ENDPOINT = `${EXPORT_ENDPOINT}/status`
6
+ /** Where an export's progress is read, by job id. */
7
+ export const EXPORT_STATUS_ENDPOINT = `${EXPORT_ENDPOINT}/status`
7
8
  const DOWNLOAD_ENDPOINT = `${EXPORT_ENDPOINT}/download`
8
9
  const HISTORY_PAGE_SIZE = 10
9
10
  const FIRST_PAGE = 1
10
- const STATE_KEY = 'saas-export-history'
11
+ const HISTORY_KEY = 'saas-export-history'
12
+ const STARTING_STATE_KEY = 'saas-export-starting'
11
13
  const FILENAME_HEADER_PATTERN = /filename="([^"]+)"/i
12
14
  const FALLBACK_EXTENSION = 'zip'
13
15
 
@@ -66,18 +68,6 @@ interface ExportHistoryState {
66
68
  hasMore: boolean
67
69
  }
68
70
 
69
- function initialState(): ExportHistoryState {
70
- return {
71
- entries: [],
72
- isLoading: false,
73
- hasLoaded: false,
74
- hasFailed: false,
75
- isStarting: false,
76
- page: FIRST_PAGE,
77
- hasMore: false,
78
- }
79
- }
80
-
81
71
  function parseFilename(header: string | null): string | null {
82
72
  return header?.match(FILENAME_HEADER_PATTERN)?.[1] ?? null
83
73
  }
@@ -98,101 +88,101 @@ export function isExportExpired(
98
88
  return !!entry.expiresAt && new Date(entry.expiresAt).getTime() <= nowMs
99
89
  }
100
90
 
101
- /** The history reload both blocks of the page share while it runs. */
102
- let pendingRefresh: Promise<void> | null = null
103
-
104
91
  interface ExportHistoryHandle {
105
- state: Ref<ExportHistoryState>
92
+ /** The page of the history this block shows, read with the page. */
93
+ state: ComputedRef<ExportHistoryState>
106
94
  /** The export still being built, if the newest page holds one. */
107
95
  running: ComputedRef<ExportHistoryEntry | null>
108
- load: (targetPage?: number) => Promise<void>
109
- refresh: () => Promise<void>
110
- goToPage: (targetPage: number) => Promise<void>
96
+ /** Reads the newest page again, and the page shown. */
97
+ load: () => Promise<void>
98
+ goToPage: (targetPage: number) => void
111
99
  start: () => Promise<void>
112
- readStatus: (jobId: string) => Promise<ExportJobStatus>
113
100
  download: (jobId: string, naming?: ExportDownloadNaming) => Promise<void>
114
101
  }
115
102
 
116
103
  /**
117
104
  * The caller's exports of the current workspace, shared by the request card
118
105
  * and the history list of the Data export page: one export runs at a time, so
119
- * both read the same running entry.
106
+ * both read the same running entry, from the newest page. The history list
107
+ * pages through the rest; the request card stays on the newest page.
120
108
  */
121
109
  export function useExportHistory(): ExportHistoryHandle {
122
110
  const { $authFetch } = useAuthFetch()
123
- const state = useDmsState<ExportHistoryState>(STATE_KEY, initialState)
111
+ const isStarting = useDmsState<boolean>(STARTING_STATE_KEY, () => false)
112
+ const page = ref(FIRST_PAGE)
113
+
114
+ /**
115
+ * A page past the first that comes back empty falls back to the first
116
+ * rather than showing the "no export yet" state, which would be a lie —
117
+ * exports expiring under the reader is enough to empty the page they were on.
118
+ */
119
+ function readPage(target: () => number) {
120
+ return useApiData<ExportHistoryPage>(
121
+ () => `${HISTORY_KEY}:${target()}`,
122
+ async ({ $authFetch: fetch, signal }) => {
123
+ const read = (pageNumber: number) =>
124
+ fetch<ExportHistoryPage>(HISTORY_ENDPOINT, {
125
+ query: { page: pageNumber, pageSize: HISTORY_PAGE_SIZE },
126
+ signal,
127
+ })
128
+ const result = await read(target())
129
+ return !result.items.length && result.page > FIRST_PAGE
130
+ ? read(FIRST_PAGE)
131
+ : result
132
+ },
133
+ )
134
+ }
135
+
136
+ const newest = readPage(() => FIRST_PAGE)
137
+ const shown = readPage(() => page.value)
124
138
 
125
139
  const running = computed(
126
140
  () =>
127
- state.value.entries.find(
141
+ newest.data.value?.items.find(
128
142
  (entry) => entry.status === EXPORT_STATUS.pending,
129
143
  ) ?? null,
130
144
  )
131
145
 
132
- /**
133
- * A page past the first that comes back empty falls back to the first
134
- * rather than showing the "no export yet" state, which would be a lie —
135
- * exports expiring under the reader is enough to empty the page they were on.
136
- */
137
- async function load(targetPage: number = FIRST_PAGE): Promise<void> {
138
- state.value.isLoading = true
139
- try {
140
- const result = await $authFetch<ExportHistoryPage>(HISTORY_ENDPOINT, {
141
- query: { page: targetPage, pageSize: HISTORY_PAGE_SIZE },
142
- })
143
- if (!result.items.length && result.page > FIRST_PAGE) {
144
- return await load(FIRST_PAGE)
145
- }
146
- Object.assign(state.value, {
147
- entries: result.items,
148
- page: result.page,
149
- hasMore: result.hasMore,
150
- hasFailed: false,
151
- hasLoaded: true,
152
- })
153
- } catch {
154
- state.value.hasFailed = true
155
- } finally {
156
- state.value.isLoading = false
146
+ const state = computed<ExportHistoryState>(() => {
147
+ const current = shown.data.value
148
+ return {
149
+ entries: current?.items ?? [],
150
+ isLoading: shown.pending.value,
151
+ hasLoaded: !!current,
152
+ hasFailed: shown.status.value === 'error',
153
+ isStarting: isStarting.value,
154
+ page: current?.page ?? page.value,
155
+ hasMore: current?.hasMore ?? false,
157
156
  }
158
- }
157
+ })
159
158
 
160
- /**
161
- * Reloads the newest page once for every block mounting together: the
162
- * request card and the history are granted apart, so each one loads, but
163
- * a page showing both reads the history a single time.
164
- */
165
- function refresh(): Promise<void> {
166
- pendingRefresh ??= load().finally(() => {
167
- pendingRefresh = null
168
- })
169
- return pendingRefresh
159
+ async function load(): Promise<void> {
160
+ await Promise.all(
161
+ page.value === FIRST_PAGE
162
+ ? [newest.refresh()]
163
+ : [newest.refresh(), shown.refresh()],
164
+ )
170
165
  }
171
166
 
172
- function goToPage(targetPage: number): Promise<void> {
173
- if (targetPage < FIRST_PAGE || state.value.isLoading)
174
- return Promise.resolve()
175
- return load(targetPage)
167
+ function goToPage(targetPage: number): void {
168
+ if (targetPage < FIRST_PAGE || shown.pending.value) return
169
+ page.value = targetPage
176
170
  }
177
171
 
178
172
  /** Rejects with the server's refusal, for the caller to word. */
179
173
  async function start(): Promise<void> {
180
- if (state.value.isStarting) return
181
- state.value.isStarting = true
174
+ if (isStarting.value) return
175
+ isStarting.value = true
182
176
  try {
183
177
  await $authFetch(START_ENDPOINT, { method: 'POST' })
184
178
  } finally {
185
- state.value.isStarting = false
186
- await load()
179
+ page.value = FIRST_PAGE
180
+ // The new export is listed before the button comes back.
181
+ await refreshPageBlocks()
182
+ isStarting.value = false
187
183
  }
188
184
  }
189
185
 
190
- function readStatus(jobId: string): Promise<ExportJobStatus> {
191
- return $authFetch<ExportJobStatus>(
192
- `${STATUS_ENDPOINT}/${encodeURIComponent(jobId)}`,
193
- )
194
- }
195
-
196
186
  /**
197
187
  * Takes a job id rather than a history row: a link delivered by e-mail may
198
188
  * point at an export older than the window the history lists. The id is
@@ -202,6 +192,7 @@ export function useExportHistory(): ExportHistoryHandle {
202
192
  jobId: string,
203
193
  naming?: ExportDownloadNaming,
204
194
  ): Promise<void> {
195
+ // A file to save, not data to show: fetched as it is, never kept.
205
196
  const response = await $authFetch.raw(
206
197
  `${DOWNLOAD_ENDPOINT}/${encodeURIComponent(jobId)}`,
207
198
  { responseType: 'blob' },
@@ -223,10 +214,8 @@ export function useExportHistory(): ExportHistoryHandle {
223
214
  state,
224
215
  running,
225
216
  load,
226
- refresh,
227
217
  goToPage,
228
218
  start,
229
- readStatus,
230
219
  download,
231
220
  }
232
221
  }
@@ -0,0 +1,27 @@
1
+ import {
2
+ computed,
3
+ onMounted,
4
+ shallowRef,
5
+ type ComputedRef,
6
+ type Ref,
7
+ } from 'vue'
8
+
9
+ /** Where a read runs: `server: false` leaves it to the browser. */
10
+ export interface ApiReadOptions {
11
+ server?: boolean
12
+ }
13
+
14
+ /**
15
+ * Data a layout strip shows, kept null until the strip is mounted. A strip
16
+ * reads Stripe-backed data with `server: false`, so a slow Stripe never holds
17
+ * the server render of the whole page; the server then renders the strip
18
+ * without it, and the browser's first render must match that HTML, even when
19
+ * the page read the same request on the server and handed its data over.
20
+ */
21
+ export function useMountedData<T>(data: Ref<T | null>): ComputedRef<T | null> {
22
+ const isMounted = shallowRef(false)
23
+ onMounted(() => {
24
+ isMounted.value = true
25
+ })
26
+ return computed(() => (isMounted.value ? data.value : null))
27
+ }