-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Add archived tiers to tier filter #28189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jonatansberg
wants to merge
1
commit into
main
Choose a base branch
from
ber-3588-add-archived-tiers-to-tiers-filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 56 additions & 5 deletions
61
apps/posts/src/hooks/filter-sources/use-tier-value-source.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,15 +1,66 @@ | ||
| import {FilterOption, ValueSource} from '@tryghost/shade/patterns'; | ||
| import {createLocalValueSource} from './create-local-value-source'; | ||
| import {useBrowseTiers} from '@tryghost/admin-x-framework/api/tiers'; | ||
| import {useEffect, useMemo} from 'react'; | ||
| import type {FilterOption, ValueSource} from '@tryghost/shade/patterns'; | ||
| import type {Tier} from '@tryghost/admin-x-framework/api/tiers'; | ||
|
|
||
| const TIER_FILTER_PAGE_LIMIT = '100'; | ||
| const TIER_FILTER_TYPE = 'type:paid'; | ||
| const ARCHIVED_TIER_LABEL_SUFFIX = ' (archived)'; | ||
| const EMPTY_TIERS: Tier[] = []; | ||
|
|
||
| type TierValueSource = ValueSource<string> & { | ||
| hasMultipleTiers: boolean; | ||
| }; | ||
|
|
||
| function toTierFilterOption(tier: Tier): FilterOption<string> { | ||
| return { | ||
| value: tier.id, | ||
| label: tier.active ? tier.name : `${tier.name}${ARCHIVED_TIER_LABEL_SUFFIX}`, | ||
| detail: tier.slug | ||
| }; | ||
| } | ||
|
|
||
| function buildTierFilterOptions(tiers: Tier[] = []): FilterOption<string>[] { | ||
| const activeTiers = tiers.filter(tier => tier.active); | ||
| const archivedTiers = tiers.filter(tier => !tier.active); | ||
|
|
||
| return [ | ||
| ...activeTiers.map(toTierFilterOption), | ||
| ...archivedTiers.map(toTierFilterOption) | ||
| ]; | ||
| } | ||
|
|
||
| export function useTierValueSource(): TierValueSource { | ||
| const { | ||
| data: tiersData, | ||
| fetchNextPage, | ||
| isFetchingNextPage, | ||
| isLoading | ||
| } = useBrowseTiers({searchParams: {filter: TIER_FILTER_TYPE, limit: TIER_FILTER_PAGE_LIMIT}}); | ||
|
|
||
| useEffect(() => { | ||
| if (tiersData?.isEnd === false && !isFetchingNextPage) { | ||
| void fetchNextPage(); | ||
| } | ||
| }, [fetchNextPage, isFetchingNextPage, tiersData?.isEnd]); | ||
|
|
||
| const tiers = tiersData?.tiers ?? EMPTY_TIERS; | ||
| const isLoadingTierOptions = isLoading || isFetchingNextPage || tiersData?.isEnd === false; | ||
| const options = useMemo(() => buildTierFilterOptions(tiers), [tiers]); | ||
| const hasMultipleTiers = tiers.length > 1 || tiersData?.isEnd === false; | ||
|
|
||
| export function useTierValueSource(options: FilterOption<string>[] = []): ValueSource<string> { | ||
| const useLocalTierValueSource = createLocalValueSource<FilterOption<string>, string>({ | ||
| id: 'posts.tiers.local', | ||
| useItems: () => ({ | ||
| data: options, | ||
| isLoading: false | ||
| data: isLoadingTierOptions ? undefined : options, | ||
| isLoading: isLoadingTierOptions | ||
| }), | ||
| toOption: option => option | ||
| }); | ||
|
|
||
| return useLocalTierValueSource(); | ||
| return { | ||
| ...useLocalTierValueSource(), | ||
| hasMultipleTiers | ||
| }; | ||
| } | ||
This file contains hidden or 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 hidden or 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
236 changes: 236 additions & 0 deletions
236
apps/posts/test/unit/hooks/use-tier-value-source.test.tsx
This file contains hidden or 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,236 @@ | ||
| import {beforeEach, describe, expect, it, vi} from 'vitest'; | ||
| import {renderHook} from '@testing-library/react'; | ||
| import {useTierValueSource} from '@src/hooks/filter-sources/use-tier-value-source'; | ||
| import type {Tier} from '@tryghost/admin-x-framework/api/tiers'; | ||
|
|
||
| const {mockUseBrowseTiers} = vi.hoisted(() => ({ | ||
| mockUseBrowseTiers: vi.fn() | ||
| })); | ||
|
|
||
| vi.mock('@tryghost/admin-x-framework/api/tiers', () => ({ | ||
| useBrowseTiers: mockUseBrowseTiers | ||
| })); | ||
|
|
||
| function tier(overrides: Partial<Tier>): Tier { | ||
| return { | ||
| id: 'tier-id', | ||
| name: 'Tier', | ||
| description: null, | ||
| slug: 'tier', | ||
| active: true, | ||
| type: 'paid', | ||
| welcome_page_url: null, | ||
| created_at: '2024-01-01T00:00:00.000Z', | ||
| updated_at: '2024-01-01T00:00:00.000Z', | ||
| visibility: 'public', | ||
| benefits: [], | ||
| trial_days: 0, | ||
| ...overrides | ||
| }; | ||
| } | ||
|
|
||
| function mockTiersResponse({ | ||
| tiers = [], | ||
| isEnd = true, | ||
| isFetchingNextPage = false, | ||
| isLoading = false, | ||
| fetchNextPage = vi.fn() | ||
| }: { | ||
| tiers?: Tier[]; | ||
| isEnd?: boolean; | ||
| isFetchingNextPage?: boolean; | ||
| isLoading?: boolean; | ||
| fetchNextPage?: ReturnType<typeof vi.fn>; | ||
| } = {}) { | ||
| mockUseBrowseTiers.mockReturnValue({ | ||
| data: { | ||
| tiers, | ||
| isEnd | ||
| }, | ||
| fetchNextPage, | ||
| isFetchingNextPage, | ||
| isLoading | ||
| }); | ||
| } | ||
|
|
||
| describe('useTierValueSource', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('exposes active and archived tier options in display order', () => { | ||
| mockTiersResponse({ | ||
| tiers: [ | ||
| tier({id: 'archived', name: 'Archived Gold', slug: 'archived-gold', active: false}), | ||
| tier({id: 'active', name: 'Active Gold', slug: 'active-gold', active: true}) | ||
| ] | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => { | ||
| const source = useTierValueSource(); | ||
| return source.useOptions({query: '', selectedValues: []}); | ||
| }); | ||
|
|
||
| expect(result.current.options).toEqual([ | ||
| { | ||
| value: 'active', | ||
| label: 'Active Gold', | ||
| detail: 'active-gold' | ||
| }, | ||
| { | ||
| value: 'archived', | ||
| label: 'Archived Gold (archived)', | ||
| detail: 'archived-gold' | ||
| } | ||
| ]); | ||
| }); | ||
|
|
||
| it('counts fetched paid tiers when deciding if the tier filter is available', () => { | ||
| const cases = [ | ||
| { | ||
| tiers: [ | ||
| tier({id: 'active', active: true}), | ||
| tier({id: 'archived', active: false}) | ||
| ], | ||
| expected: true | ||
| }, | ||
| { | ||
| tiers: [ | ||
| tier({id: 'archived-1', active: false}), | ||
| tier({id: 'archived-2', active: false}) | ||
| ], | ||
| expected: true | ||
| }, | ||
| { | ||
| tiers: [ | ||
| tier({id: 'archived', active: false}) | ||
| ], | ||
| expected: false | ||
| }, | ||
| { | ||
| tiers: [], | ||
| expected: false | ||
| } | ||
| ]; | ||
|
|
||
| for (const testCase of cases) { | ||
| mockTiersResponse({tiers: testCase.tiers}); | ||
|
|
||
| const {result} = renderHook(() => useTierValueSource()); | ||
|
|
||
| expect(result.current.hasMultipleTiers).toBe(testCase.expected); | ||
| } | ||
| }); | ||
|
|
||
| it('treats an incomplete tiers response as multiple tiers', () => { | ||
| mockTiersResponse({ | ||
| tiers: [ | ||
| tier({id: 'active', active: true}) | ||
| ], | ||
| isEnd: false | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => useTierValueSource()); | ||
|
|
||
| expect(result.current.hasMultipleTiers).toBe(true); | ||
| }); | ||
|
|
||
| it('fetches paid tiers with a numeric page limit and exposes tier options', () => { | ||
| mockTiersResponse({ | ||
| tiers: [ | ||
| tier({id: 'active-tier', name: 'Active Gold', slug: 'active-gold', active: true}), | ||
| tier({id: 'archived-tier', name: 'Archived Gold', slug: 'archived-gold', active: false}) | ||
| ] | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => { | ||
| const source = useTierValueSource(); | ||
| return { | ||
| hasMultipleTiers: source.hasMultipleTiers, | ||
| state: source.useOptions({query: '', selectedValues: []}) | ||
| }; | ||
| }); | ||
|
|
||
| expect(mockUseBrowseTiers).toHaveBeenCalledWith({searchParams: {filter: 'type:paid', limit: '100'}}); | ||
| expect(result.current.hasMultipleTiers).toBe(true); | ||
| expect(result.current.state.options).toEqual([ | ||
| { | ||
| value: 'active-tier', | ||
| label: 'Active Gold', | ||
| detail: 'active-gold' | ||
| }, | ||
| { | ||
| value: 'archived-tier', | ||
| label: 'Archived Gold (archived)', | ||
| detail: 'archived-gold' | ||
| } | ||
| ]); | ||
| }); | ||
|
|
||
| it('searches local tier options by archived label text', () => { | ||
| mockTiersResponse({ | ||
| tiers: [ | ||
| tier({id: 'active-tier', name: 'Active Gold', slug: 'active-gold', active: true}), | ||
| tier({id: 'archived-tier', name: 'Archived Gold', slug: 'archived-gold', active: false}) | ||
| ] | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => { | ||
| const source = useTierValueSource(); | ||
| return source.useOptions({query: 'archived', selectedValues: []}); | ||
| }); | ||
|
|
||
| expect(result.current.options).toEqual([ | ||
| { | ||
| value: 'archived-tier', | ||
| label: 'Archived Gold (archived)', | ||
| detail: 'archived-gold' | ||
| } | ||
| ]); | ||
| }); | ||
|
|
||
| it('keeps options in the initial load state while additional tier pages are loading', () => { | ||
| mockTiersResponse({ | ||
| tiers: [ | ||
| tier({id: 'active-tier', name: 'Active Gold', slug: 'active-gold', active: true}) | ||
| ], | ||
| isEnd: false, | ||
| isFetchingNextPage: true | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => { | ||
| const source = useTierValueSource(); | ||
| return source.useOptions({query: '', selectedValues: []}); | ||
| }); | ||
|
|
||
| expect(result.current.options).toEqual([]); | ||
| expect(result.current.isInitialLoad).toBe(true); | ||
| }); | ||
|
|
||
| it('loads the next page until the tiers response is complete', () => { | ||
| const fetchNextPage = vi.fn(); | ||
| mockTiersResponse({ | ||
| tiers: [tier({id: 'active-tier'})], | ||
| isEnd: false, | ||
| fetchNextPage | ||
| }); | ||
|
|
||
| renderHook(() => useTierValueSource()); | ||
|
|
||
| expect(fetchNextPage).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('does not load another page while a tiers page is already loading', () => { | ||
| const fetchNextPage = vi.fn(); | ||
| mockTiersResponse({ | ||
| tiers: [tier({id: 'active-tier'})], | ||
| isEnd: false, | ||
| isFetchingNextPage: true, | ||
| fetchNextPage | ||
| }); | ||
|
|
||
| renderHook(() => useTierValueSource()); | ||
|
|
||
| expect(fetchNextPage).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: TryGhost/Ghost
Length of output: 40
🏁 Script executed:
Repository: TryGhost/Ghost
Length of output: 8507
🏁 Script executed:
Repository: TryGhost/Ghost
Length of output: 4268
🏁 Script executed:
Repository: TryGhost/Ghost
Length of output: 607
🏁 Script executed:
Repository: TryGhost/Ghost
Length of output: 4121
🏁 Script executed:
Repository: TryGhost/Ghost
Length of output: 784
🏁 Script executed:
Repository: TryGhost/Ghost
Length of output: 3173
🏁 Script executed:
Repository: TryGhost/Ghost
Length of output: 16029
Localize the archived tier label suffix instead of hardcoding English
ARCHIVED_TIER_LABEL_SUFFIXis hardcoded to' (archived)'and concatenated intoFilterOption.label:Because this string is constructed outside the app’s i18n layer, the “(archived)” part won’t be translated for localized users. Build the archived label via the app’s translation mechanism instead, using a single translatable string with interpolation (e.g.
{name} (archived)).🤖 Prompt for AI Agents