Awards can now upgrade through a series: collection fields and migration, grant flow support, profile and account UI updates, and regression coverage. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
84 lines
3.6 KiB
TypeScript
84 lines
3.6 KiB
TypeScript
import type { RibbonDesign } from "@/components/awards/ribbonDesign/types";
|
|
import { RibbonViewer } from "@/components/frontend/account/RibbonViewer";
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
|
import type { Award } from "@/payload-types";
|
|
import { convertLexicalToPlaintext } from "@payloadcms/richtext-lexical/plaintext";
|
|
|
|
function compareAdvancementAwards(candidate: Award, current: Award): number {
|
|
const tierDifference =
|
|
(candidate.advancement?.tier ?? Number.NEGATIVE_INFINITY) -
|
|
(current.advancement?.tier ?? Number.NEGATIVE_INFINITY);
|
|
if (tierDifference !== 0) return tierDifference;
|
|
|
|
const experienceDifference = (candidate.experiencePoints ?? 0) - (current.experiencePoints ?? 0);
|
|
if (experienceDifference !== 0) return experienceDifference;
|
|
|
|
if (candidate.name !== current.name) return candidate.name < current.name ? 1 : -1;
|
|
return current.id - candidate.id;
|
|
}
|
|
|
|
export function dedupeAdvancementAwards(awards: Award[]): Award[] {
|
|
const seriesCounts = new Map<string, number>();
|
|
for (const award of awards) {
|
|
const seriesKey = award.advancement?.seriesKey;
|
|
if (seriesKey == null) continue;
|
|
seriesCounts.set(seriesKey, (seriesCounts.get(seriesKey) ?? 0) + 1);
|
|
}
|
|
|
|
const duplicateSeriesKeys = new Set(
|
|
[...seriesCounts.entries()].filter(([, count]) => count > 1).map(([seriesKey]) => seriesKey),
|
|
);
|
|
if (duplicateSeriesKeys.size === 0) return awards;
|
|
|
|
const winnerIndexBySeries = new Map<string, number>();
|
|
for (let index = 0; index < awards.length; index += 1) {
|
|
const award = awards[index];
|
|
if (!award) continue;
|
|
const seriesKey = award.advancement?.seriesKey;
|
|
if (seriesKey == null || !duplicateSeriesKeys.has(seriesKey)) continue;
|
|
|
|
const currentIndex = winnerIndexBySeries.get(seriesKey);
|
|
const currentAward = currentIndex == null ? undefined : awards[currentIndex];
|
|
if (currentAward == null || compareAdvancementAwards(award, currentAward) > 0) {
|
|
winnerIndexBySeries.set(seriesKey, index);
|
|
}
|
|
}
|
|
|
|
return awards.filter((award, index) => {
|
|
const seriesKey = award.advancement?.seriesKey;
|
|
if (seriesKey == null || !duplicateSeriesKeys.has(seriesKey)) return true;
|
|
return winnerIndexBySeries.get(seriesKey) === index;
|
|
});
|
|
}
|
|
|
|
export const RibbonRack = ({ awards }: { awards: Award[] }) => {
|
|
return (
|
|
<div className="flex flex-wrap w-[19.625rem] max-w-full gap-x-1 gap-y-px">
|
|
{dedupeAdvancementAwards(
|
|
awards.filter((award) => award.type === "ribbon" && award.ribbonDesign != null),
|
|
)
|
|
.sort((a, b) => (b.experiencePoints ?? 0) - (a.experiencePoints ?? 0))
|
|
.map((award, key) => {
|
|
const ribbon = award.ribbonDesign as RibbonDesign;
|
|
return (
|
|
<Tooltip key={key}>
|
|
<TooltipTrigger className="w-[6.375rem] max-w-full h-[1.6875rem] hover:scale-110 shadow-black shadow-xs hover:shadow-xl transition-transform">
|
|
<RibbonViewer ribbonDesign={ribbon} showRibbonOnly />
|
|
</TooltipTrigger>
|
|
<TooltipContent>
|
|
<div className="flex flex-col gap-1">
|
|
<div className="flex flex-row items-center justify-between gap-3">
|
|
<span className="font-bold text-lg">{award.name}</span>
|
|
<span className="text-amber-400/75">{award.experiencePoints} XP</span>
|
|
</div>
|
|
<p className="opacity-75">
|
|
{convertLexicalToPlaintext({ data: award.description })}
|
|
</p>
|
|
</div>
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
};
|