mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 02:42:24 +03:00
feat(gamification): show the real daily streak on the profile page (#12377)
The Profile page already rendered a streak card but fed it a hard-coded useState(0) with a "streak data comes from future API" note — while streaks.ts tracked per-key streaks all along and the MCP gamification_profile tool already returned them. GET /api/gamification/level now returns streak: { current, longest } next to level: the key's own streak with apiKeyId, the operator-wide maximum otherwise, matching the aggregate mode getAggregateXp uses (#3484). No new route, no OpenAPI change, no new i18n keys; a missing or zero streak keeps the card hidden exactly as before.
Validated in a combined worktree with all 25 PRs of this batch boarded together: typecheck:core clean, 443/443 node-runner tests plus 14/14 vitest across every test file the batch touches, and check-changelog-integrity, check:cycles (418 files), check:provider-consistency (272 REGISTRY entries, 355 canonical providers), check:docs-counts, check:docs-sync (42 locales) and check-file-size all green.
Thanks @pacocartones.
This commit is contained in:
@@ -79,6 +79,14 @@ function BadgeIcon({ icon, earned }: { icon: string | null; earned: boolean }) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Current daily streak carried by `/api/gamification/level` (#2403). Older or partial
|
||||
* payloads without a `streak` field, or with a non-numeric count, render as no streak.
|
||||
*/
|
||||
function readStreakCount(data: { streak?: { current?: unknown } | null }): number {
|
||||
return Number(data.streak?.current) || 0;
|
||||
}
|
||||
|
||||
const RARITY_COLORS: Record<string, string> = {
|
||||
common: "text-gray-400 border-gray-500/30",
|
||||
uncommon: "text-green-400 border-green-500/30",
|
||||
@@ -97,7 +105,7 @@ export default function ProfilePage() {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
const [selectedBadge, setSelectedBadge] = useState<BadgeDef | null>(null);
|
||||
const [streak] = useState(0); // streak data comes from future API
|
||||
const [streak, setStreak] = useState(0);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
@@ -114,6 +122,7 @@ export default function ProfilePage() {
|
||||
if (levelRes.ok) {
|
||||
const data = await levelRes.json();
|
||||
setUserLevel(data.level ?? data);
|
||||
setStreak(readStreakCount(data));
|
||||
}
|
||||
if (badgesRes.ok) {
|
||||
const data = await badgesRes.json();
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/**
|
||||
* GET /api/gamification/level — current XP/level for a key, or the operator-wide
|
||||
* aggregate when no `apiKeyId` is supplied (the dashboard profile page case). (#3484)
|
||||
* The daily streak rides along in the same payload so the profile streak card can show
|
||||
* real data without a second round trip. (#2403)
|
||||
*
|
||||
* LOCAL_ONLY: not process-spawning; management-scoped via requireManagementAuth.
|
||||
*/
|
||||
@@ -8,6 +10,7 @@ import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
import { CORS_HEADERS, handleCorsOptions } from "@/shared/utils/cors";
|
||||
import { getXp, getAggregateXp } from "@/lib/db/gamification";
|
||||
import { getStreak, getAggregateStreak } from "@/lib/gamification/streaks";
|
||||
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
|
||||
|
||||
export async function OPTIONS() {
|
||||
@@ -20,5 +23,7 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
const apiKeyId = new URL(request.url).searchParams.get("apiKeyId");
|
||||
const level = apiKeyId ? getXp(apiKeyId) : getAggregateXp();
|
||||
return NextResponse.json({ level }, { headers: CORS_HEADERS });
|
||||
const streakData = apiKeyId ? await getStreak(apiKeyId) : await getAggregateStreak();
|
||||
const streak = { current: streakData.currentStreak, longest: streakData.longestStreak };
|
||||
return NextResponse.json({ level, streak }, { headers: CORS_HEADERS });
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ export {
|
||||
XP_REWARDS,
|
||||
type XpAction,
|
||||
} from "./xp";
|
||||
export { updateStreak } from "./streaks";
|
||||
export { getStreak, getAggregateStreak, updateStreak, type StreakData } from "./streaks";
|
||||
export {
|
||||
recordBadgeUnlock,
|
||||
consumeBadgeUnlocks,
|
||||
|
||||
@@ -107,6 +107,38 @@ export async function getStreak(apiKeyId: string): Promise<StreakData> {
|
||||
return parseStreakJson(row.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Operator-wide streak for the dashboard profile page, which has no single API key
|
||||
* (the aggregate mode of `/api/gamification/level`, #3484): the best `currentStreak`
|
||||
* and the best `longestStreak` over every key in the namespace. Both are maxima, not
|
||||
* sums, and may come from different keys. Malformed rows count as zero.
|
||||
*
|
||||
* @returns The highest current/longest streak across all API keys
|
||||
*
|
||||
* @example
|
||||
* const agg = await getAggregateStreak();
|
||||
* console.log(agg.currentStreak); // 7
|
||||
*/
|
||||
export async function getAggregateStreak(): Promise<
|
||||
Pick<StreakData, "currentStreak" | "longestStreak">
|
||||
> {
|
||||
const aggregate = { currentStreak: 0, longestStreak: 0 };
|
||||
if (isBuildPhase || isCloud) return aggregate;
|
||||
|
||||
const db = getDbInstance() as unknown as DbLike;
|
||||
const rows = db
|
||||
.prepare("SELECT value FROM key_value WHERE namespace = ?")
|
||||
.all(NAMESPACE) as KeyValueRow[];
|
||||
|
||||
for (const row of rows) {
|
||||
const streak = parseStreakJson(row.value);
|
||||
aggregate.currentStreak = Math.max(aggregate.currentStreak, streak.currentStreak);
|
||||
aggregate.longestStreak = Math.max(aggregate.longestStreak, streak.longestStreak);
|
||||
}
|
||||
|
||||
return aggregate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update streak for today. Returns the new current streak count.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user