-
Notifications
You must be signed in to change notification settings - Fork 75
Surface daily quest and streak in the editor #3326
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { deriveQuestChipState } from './questChip'; | ||
|
|
||
| jest.mock('@ecency/sdk', () => ({ | ||
| getQuestCatalogEntry: jest.fn((tier: string, id: string) => | ||
| tier === 'daily' && id === 'post' | ||
| ? { id: 'post', tier: 'daily', goal: 1, i18nKey: 'post', icon: 'pencil' } | ||
| : undefined, | ||
| ), | ||
| })); | ||
|
|
||
| const quests = (overrides: any = {}) => ({ | ||
| period: { day: '2026-07-06', week: '2026-W28', month: '2026-07', day_resets_in_secs: 3600 }, | ||
| daily: [{ id: 'post', progress: 0, cap: 3 }], | ||
| weekly: [], | ||
| monthly: [], | ||
| streak: { current: 0, best: 0, at_risk: false }, | ||
| ...overrides, | ||
| }); | ||
|
|
||
| describe('deriveQuestChipState', () => { | ||
| it('returns null until quest data has arrived', () => { | ||
| expect(deriveQuestChipState(undefined)).toBeNull(); | ||
| expect(deriveQuestChipState(null)).toBeNull(); | ||
| }); | ||
|
|
||
| it('is visible when the daily post quest is incomplete and there is no streak', () => { | ||
| expect(deriveQuestChipState(quests() as any)).toEqual({ | ||
| visible: true, | ||
| postedToday: false, | ||
| postProgress: 0, | ||
| postGoal: 1, | ||
| streakCurrent: 0, | ||
| atRisk: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('hides once the daily post quest is complete and there is no streak', () => { | ||
| const state = deriveQuestChipState( | ||
| quests({ daily: [{ id: 'post', progress: 1, cap: 3 }] }) as any, | ||
| ); | ||
| expect(state).toEqual({ | ||
| visible: false, | ||
| postedToday: true, | ||
| postProgress: 1, | ||
| postGoal: 1, | ||
| streakCurrent: 0, | ||
| atRisk: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('stays visible after posting when a streak is active', () => { | ||
| const state = deriveQuestChipState( | ||
| quests({ | ||
| daily: [{ id: 'post', progress: 2, cap: 3 }], | ||
| streak: { current: 5, best: 9, at_risk: false }, | ||
| }) as any, | ||
| ); | ||
| expect(state).toEqual({ | ||
| visible: true, | ||
| postedToday: true, | ||
| postProgress: 2, | ||
| postGoal: 1, | ||
| streakCurrent: 5, | ||
| atRisk: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('flags at-risk only while a streak is active', () => { | ||
| const atRisk = deriveQuestChipState( | ||
| quests({ streak: { current: 3, best: 3, at_risk: true } }) as any, | ||
| ); | ||
| expect(atRisk?.atRisk).toBe(true); | ||
| expect(atRisk?.visible).toBe(true); | ||
|
|
||
| const noStreak = deriveQuestChipState( | ||
| quests({ streak: { current: 0, best: 3, at_risk: true } }) as any, | ||
| ); | ||
| expect(noStreak?.atRisk).toBe(false); | ||
| }); | ||
|
|
||
| it('treats a missing daily post quest as zero progress', () => { | ||
| const state = deriveQuestChipState( | ||
| quests({ daily: [{ id: 'comment', progress: 4, cap: 25 }] }) as any, | ||
| ); | ||
| expect(state).toEqual({ | ||
| visible: true, | ||
| postedToday: false, | ||
| postProgress: 0, | ||
| postGoal: 1, | ||
| streakCurrent: 0, | ||
| atRisk: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('tolerates missing daily and streak sections', () => { | ||
| const state = deriveQuestChipState(quests({ daily: undefined, streak: undefined }) as any); | ||
| expect(state).toEqual({ | ||
| visible: true, | ||
| postedToday: false, | ||
| postProgress: 0, | ||
| postGoal: 1, | ||
| streakCurrent: 0, | ||
| atRisk: false, | ||
| }); | ||
| }); | ||
| }); |
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,39 @@ | ||
| import { getQuestCatalogEntry } from '@ecency/sdk'; | ||
| import type { QuestsResponse } from '@ecency/sdk'; | ||
|
|
||
| export interface QuestChipState { | ||
| visible: boolean; | ||
| postedToday: boolean; | ||
| postProgress: number; | ||
| postGoal: number; | ||
| streakCurrent: number; | ||
| atRisk: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Derives the editor quest chip state from the quests endpoint response. | ||
| * Returns null until quest data has arrived. The chip is worth showing when | ||
| * the user has an active streak to protect or has not completed the daily | ||
| * post quest yet. | ||
| */ | ||
| export const deriveQuestChipState = (quests?: QuestsResponse | null): QuestChipState | null => { | ||
| if (!quests) { | ||
| return null; | ||
| } | ||
|
|
||
| const postQuest = quests.daily?.find((q) => q.id === 'post'); | ||
| const postGoal = getQuestCatalogEntry('daily', 'post')?.goal ?? 1; | ||
| const postProgress = Math.max(0, postQuest?.progress ?? 0); | ||
| const postedToday = postProgress >= postGoal; | ||
| const streakCurrent = Math.max(0, quests.streak?.current ?? 0); | ||
| const atRisk = streakCurrent > 0 && !!quests.streak?.at_risk; | ||
|
|
||
| return { | ||
| visible: streakCurrent > 0 || !postedToday, | ||
| postedToday, | ||
| postProgress, | ||
| postGoal, | ||
| streakCurrent, | ||
| atRisk, | ||
| }; | ||
| }; |
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.
Uh oh!
There was an error while loading. Please reload this page.