-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(releases): improvements to archive release detail (#8597)
- Loading branch information
Showing
10 changed files
with
403 additions
and
59 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
packages/sanity/src/core/hooks/__mocks__/useProjectSubscriptions.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import {type Mock, type Mocked} from 'vitest' | ||
|
||
import {useProjectSubscriptions} from '../useProjectSubscriptions' | ||
|
||
export const useProjectSubscriptionsMockReturn: Mocked<ReturnType<typeof useProjectSubscriptions>> = | ||
{ | ||
error: null, | ||
isLoading: false, | ||
projectSubscriptions: { | ||
id: 'sub_123', | ||
projectId: 'proj_456', | ||
productType: 'premium', | ||
productId: 'prod_789', | ||
customerId: 'cust_101', | ||
planId: 'plan_202', | ||
previousSubscriptionId: null, | ||
status: 'active', | ||
startedAt: '2024-02-01T00:00:00Z', | ||
startedBy: 'user_303', | ||
endedAt: null, | ||
endedBy: null, | ||
trialUntil: '2024-02-15T00:00:00Z', | ||
plan: { | ||
id: 'plan_202', | ||
planTypeId: 'type_404', | ||
variantId: null, | ||
productType: 'premium', | ||
variantOfPlanId: null, | ||
name: 'Premium Plan', | ||
variantName: null, | ||
price: 49.99, | ||
trialDays: 14, | ||
createdAt: '2024-01-01T00:00:00Z', | ||
supersededAt: null, | ||
default: false, | ||
public: true, | ||
orderable: true, | ||
isBasePlan: true, | ||
pricingModel: 'flat-rate', | ||
resources: {}, | ||
featureTypes: {}, | ||
}, | ||
resources: {}, | ||
featureTypes: { | ||
retention: { | ||
features: [ | ||
{ | ||
attributes: { | ||
maxRetentionDays: 123, | ||
}, | ||
id: '', | ||
variantId: null, | ||
name: '', | ||
price: 0, | ||
included: false, | ||
custom: false, | ||
startedAt: null, | ||
startedBy: null, | ||
endedAt: null, | ||
endedBy: null, | ||
}, | ||
], | ||
id: '', | ||
name: '', | ||
singular: false, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
export const mockUseProjectSubscriptions = useProjectSubscriptions as Mock< | ||
typeof useProjectSubscriptions | ||
> |
156 changes: 156 additions & 0 deletions
156
packages/sanity/src/core/hooks/useProjectSubscriptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import {type SanityClient} from '@sanity/client' | ||
import {useMemo} from 'react' | ||
import {useObservable} from 'react-rx' | ||
import {type Observable, of} from 'rxjs' | ||
import {catchError, map, shareReplay, startWith} from 'rxjs/operators' | ||
|
||
import {useSource} from '../studio' | ||
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../studioClient' | ||
import {useClient} from './useClient' | ||
|
||
type FeatureAttributes = Record<string, string | number | boolean | null> | ||
|
||
type Feature = { | ||
id: string | ||
variantId: string | null | ||
name: string | ||
price: number | ||
included: boolean | ||
attributes: FeatureAttributes | ||
custom: boolean | ||
startedAt: string | null | ||
startedBy: string | null | ||
endedAt: string | null | ||
endedBy: string | null | ||
} | ||
|
||
type FeatureType = { | ||
id: string | ||
name: string | ||
singular: boolean | ||
features: Feature[] | ||
} | ||
|
||
type Resource = { | ||
id: string | ||
name: string | ||
unit: string | ||
type: string | ||
quota: number | null | ||
custom: boolean | ||
overageAllowed: boolean | ||
overageChunkSize: number | ||
overageChunkPrice: number | ||
maxOverageQuota: number | null | ||
} | ||
|
||
type Plan = { | ||
id: string | ||
planTypeId: string | ||
variantId: string | null | ||
productType: string | ||
variantOfPlanId: string | null | ||
name: string | ||
variantName: string | null | ||
price: number | ||
trialDays: number | ||
createdAt: string | ||
supersededAt: string | null | ||
default: boolean | ||
public: boolean | ||
orderable: boolean | ||
isBasePlan: boolean | ||
pricingModel: string | ||
resources: Record<string, Resource> | ||
featureTypes: Record<string, FeatureType> | ||
} | ||
|
||
export type ProjectSubscriptionsResponse = { | ||
id: string | ||
projectId: string | ||
productType: string | ||
productId: string | ||
customerId: string | null | ||
planId: string | ||
previousSubscriptionId: string | null | ||
status: string | ||
startedAt: string | ||
startedBy: string | null | ||
endedAt: string | null | ||
endedBy: string | null | ||
trialUntil: string | null | ||
plan: Plan | ||
resources: Record<string, Resource> | ||
featureTypes: Record<string, FeatureType> | ||
} | ||
|
||
type ProjectSubscriptions = { | ||
error: Error | null | ||
projectSubscriptions: ProjectSubscriptionsResponse | null | ||
isLoading: boolean | ||
} | ||
|
||
const INITIAL_LOADING_STATE: ProjectSubscriptions = { | ||
error: null, | ||
projectSubscriptions: null, | ||
isLoading: true, | ||
} | ||
|
||
/** | ||
* fetches subscriptions for this project | ||
*/ | ||
function fetchProjectSubscriptions({ | ||
versionedClient, | ||
}: { | ||
versionedClient: SanityClient | ||
}): Observable<ProjectSubscriptionsResponse> { | ||
return versionedClient.observable.request<ProjectSubscriptionsResponse>({ | ||
uri: `/subscriptions/project/${versionedClient.config().projectId}`, | ||
tag: 'project-subscriptions', | ||
}) | ||
} | ||
|
||
const cachedProjectSubscriptionsRequest = new Map< | ||
string, | ||
Observable<ProjectSubscriptionsResponse> | ||
>() | ||
|
||
/** @internal */ | ||
export function useProjectSubscriptions(): ProjectSubscriptions { | ||
const versionedClient = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS) | ||
const {projectId} = useSource() | ||
|
||
if (!cachedProjectSubscriptionsRequest.get(projectId)) { | ||
const projectSubscriptions = fetchProjectSubscriptions({versionedClient}).pipe(shareReplay()) | ||
cachedProjectSubscriptionsRequest.set(projectId, projectSubscriptions) | ||
} | ||
|
||
const projectSubscriptionsObservable = useMemo(() => { | ||
const projectSubscriptions$ = cachedProjectSubscriptionsRequest.get(projectId) | ||
|
||
if (!projectSubscriptions$) | ||
return of<ProjectSubscriptions>({ | ||
isLoading: false, | ||
error: null, | ||
projectSubscriptions: null, | ||
}) | ||
|
||
return projectSubscriptions$.pipe( | ||
map((cachedSubscriptions) => ({ | ||
isLoading: false, | ||
projectSubscriptions: cachedSubscriptions, | ||
error: null, | ||
})), | ||
startWith(INITIAL_LOADING_STATE), | ||
catchError((error: Error) => | ||
of<ProjectSubscriptions>({ | ||
isLoading: false, | ||
projectSubscriptions: null, | ||
error, | ||
}), | ||
), | ||
) | ||
}, [projectId]) | ||
|
||
return useObservable(projectSubscriptionsObservable, INITIAL_LOADING_STATE) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.