Repository navigation
fix: allow users to choose properties like data block table #1914
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
Closed
webmagic123
wants to merge
7
commits into
master
from
fix/allow-user-to-choose-properties-to-show-in-the-ranking-cards
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d43258f
fix: allow users to choose properties like data block table
webmagic123 d967647
fix: hydrate ranking entry properties so selected columns render on c…
webmagic123 a14b95f
fix: ranking card image follows Cover/Avatar property choice and show…
webmagic123 f06dcc6
fix: run format
webmagic123 b66c87f
fix: resolve ranking card image by chosen Cover/Avatar property at th…
webmagic123 ae0361a
fix: single card config builder and one useDataBlock per ranking surface
webmagic123 987728a
Merge origin/master into fix/allow-user-to-choose-properties-to-show-…
webmagic123 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
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
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
95 changes: 95 additions & 0 deletions
95
apps/web/partials/blocks/table/ranking-card-config.test.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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { ContentIds, SystemIds } from '@geoprotocol/geo-sdk/lite'; | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import type { Property } from '~/core/types'; | ||
|
|
||
| import { | ||
| buildRankingCardConfig, | ||
| selectRankingCardImageProperty, | ||
| selectRankingCardProperties, | ||
| } from './ranking-card-config'; | ||
|
|
||
| function prop(id: string, name: string | null = null): Property { | ||
| return { id, name, dataType: 'RELATION' }; | ||
| } | ||
|
|
||
| function dashed(id: string): string { | ||
| return [id.slice(0, 8), id.slice(8, 12), id.slice(12, 16), id.slice(16, 20), id.slice(20)].join('-'); | ||
| } | ||
|
|
||
| describe('selectRankingCardProperties', () => { | ||
| const custom = prop('custom-prop', 'Custom'); | ||
|
|
||
| it('drops name, description, cover and avatar', () => { | ||
| const properties = [ | ||
| prop(SystemIds.NAME_PROPERTY), | ||
| prop(SystemIds.DESCRIPTION_PROPERTY), | ||
| prop(SystemIds.COVER_PROPERTY), | ||
| prop(ContentIds.AVATAR_PROPERTY), | ||
| custom, | ||
| ]; | ||
|
|
||
| expect(selectRankingCardProperties(properties)).toEqual([custom]); | ||
| }); | ||
|
|
||
| it('drops excluded properties whose ids arrive dashed', () => { | ||
| const properties = [prop(dashed(SystemIds.COVER_PROPERTY)), prop(dashed(ContentIds.AVATAR_PROPERTY)), custom]; | ||
|
|
||
| expect(selectRankingCardProperties(properties)).toEqual([custom]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('selectRankingCardImageProperty', () => { | ||
| it('prefers cover when the cover column is shown', () => { | ||
| expect(selectRankingCardImageProperty([SystemIds.COVER_PROPERTY])).toBe('cover'); | ||
| expect(selectRankingCardImageProperty([dashed(SystemIds.COVER_PROPERTY)])).toBe('cover'); | ||
| }); | ||
|
|
||
| it('falls back to avatar otherwise', () => { | ||
| expect(selectRankingCardImageProperty([ContentIds.AVATAR_PROPERTY])).toBe('avatar'); | ||
| expect(selectRankingCardImageProperty([])).toBe('avatar'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('selector agreement', () => { | ||
| it.each([SystemIds.COVER_PROPERTY, dashed(SystemIds.COVER_PROPERTY)])('cover id %s is never both', coverId => { | ||
| expect(selectRankingCardImageProperty([coverId])).toBe('cover'); | ||
| expect(selectRankingCardProperties([prop(coverId)])).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('buildRankingCardConfig', () => { | ||
| const source = { type: 'GEO' } as const; | ||
| const custom = prop('custom-prop', 'Custom'); | ||
|
|
||
| it('applies both selectors and passes source through', () => { | ||
| expect( | ||
| buildRankingCardConfig({ | ||
| properties: [prop(SystemIds.COVER_PROPERTY), custom], | ||
| shownColumnIds: [SystemIds.COVER_PROPERTY, custom.id], | ||
| source, | ||
| }) | ||
| ).toEqual({ properties: [custom], source, imageProperty: 'cover' }); | ||
| }); | ||
|
|
||
| it('falls back to avatar when no cover column is shown', () => { | ||
| expect(buildRankingCardConfig({ properties: [custom], shownColumnIds: [custom.id], source }).imageProperty).toBe( | ||
| 'avatar' | ||
| ); | ||
| }); | ||
|
|
||
| it.each([SystemIds.COVER_PROPERTY, dashed(SystemIds.COVER_PROPERTY)])( | ||
| 'never emits the image property as a field (%s)', | ||
| coverId => { | ||
| const config = buildRankingCardConfig({ | ||
| properties: [prop(coverId), custom], | ||
| shownColumnIds: [coverId, custom.id], | ||
| source, | ||
| }); | ||
|
|
||
| expect(config.imageProperty).toBe('cover'); | ||
| expect(config.properties).toEqual([custom]); | ||
| } | ||
| ); | ||
| }); |
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,73 @@ | ||
| 'use client'; | ||
|
|
||
| import { ContentIds, SystemIds } from '@geoprotocol/geo-sdk/lite'; | ||
|
|
||
| import * as React from 'react'; | ||
|
|
||
| import type { Source } from '~/core/blocks/data/source'; | ||
| import { ID } from '~/core/id'; | ||
| import type { Property } from '~/core/types'; | ||
|
|
||
| /** | ||
| * Normalized so membership matches `ID.equals` semantics | ||
| */ | ||
| const RANKING_CARD_EXCLUDED_PROPERTY_IDS = new Set<string>( | ||
| [SystemIds.NAME_PROPERTY, SystemIds.DESCRIPTION_PROPERTY, SystemIds.COVER_PROPERTY, ContentIds.AVATAR_PROPERTY].map( | ||
| id => ID.uuidToHex(id) | ||
| ) | ||
| ); | ||
|
|
||
| export type RankingCardImageProperty = 'avatar' | 'cover'; | ||
|
|
||
| export type RankingCardConfig = { | ||
| properties: Property[]; | ||
| source: Source; | ||
| imageProperty: RankingCardImageProperty; | ||
| }; | ||
|
|
||
| export function selectRankingCardProperties(properties: Property[]): Property[] { | ||
| return properties.filter(property => !RANKING_CARD_EXCLUDED_PROPERTY_IDS.has(ID.uuidToHex(property.id))); | ||
| } | ||
|
|
||
| export function selectRankingCardImageProperty(shownColumnIds: string[]): RankingCardImageProperty { | ||
| return shownColumnIds.some(id => ID.equals(id, SystemIds.COVER_PROPERTY)) ? 'cover' : 'avatar'; | ||
| } | ||
|
|
||
| /** | ||
| * Single definition of a ranking card's config. Callers already hold the data block fields | ||
| */ | ||
| export function buildRankingCardConfig({ | ||
| properties, | ||
| shownColumnIds, | ||
| source, | ||
| }: { | ||
| properties: Property[]; | ||
| shownColumnIds: string[]; | ||
| source: Source; | ||
| }): RankingCardConfig { | ||
| return { | ||
| properties: selectRankingCardProperties(properties), | ||
| source, | ||
| imageProperty: selectRankingCardImageProperty(shownColumnIds), | ||
| }; | ||
| } | ||
|
|
||
| const RankingCardConfigContext = React.createContext<RankingCardConfig>({ | ||
| properties: [], | ||
| source: { type: 'GEO' }, | ||
| imageProperty: 'avatar', | ||
| }); | ||
|
|
||
| export function RankingCardConfigProvider({ | ||
| value, | ||
| children, | ||
| }: { | ||
| value: RankingCardConfig; | ||
| children: React.ReactNode; | ||
| }) { | ||
| return <RankingCardConfigContext.Provider value={value}>{children}</RankingCardConfigContext.Provider>; | ||
| } | ||
|
|
||
| export function useRankingCardConfig(): RankingCardConfig { | ||
| return React.useContext(RankingCardConfigContext); | ||
| } |
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
Oops, something went wrong.
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.
This rebuilds the same card config inline instead of reusing useRankingShownProperties. Works fine, but it could drift from the hook over time if one changes.