1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-10 05:30:01 +00:00

type cleanup

This commit is contained in:
✨ Audrey ✨
2025-03-17 12:04:23 -04:00
parent 7d2c123072
commit 0f7f6eb7aa
3 changed files with 23 additions and 19 deletions

View File

@@ -25,16 +25,16 @@ function active(
// 🧠 the filters could be lifted into a function argument & delivered
// as a `Map<FilterType, (monitor) => bool>
if (m.trigger === "until-earned") {
if (m.active === "until-earned") {
// monitor disabled if already achieved
return !earnedByName.has(m.achievement);
}
// monitor disabled if outside of threshold
const progress = (m.metric && progressByName.get(m.metric)) || 0;
if (progress > (m.trigger.high ?? Number.POSITIVE_INFINITY)) {
const progress = (m.active.metric && progressByName.get(m.active.metric)) || 0;
if (progress > (m.active.high ?? Number.POSITIVE_INFINITY)) {
return false;
} else if (progress < (m.trigger.low ?? 0)) {
} else if (progress < (m.active.low ?? 0)) {
return false;
}

View File

@@ -14,9 +14,8 @@ const TotallyAttachedValidator = {
achievement: TotallyAttachedAchievement,
name: "Totally attached <3",
description: "Attached a file to a send or item",
metric: ItemCreatedProgress,
validator: Type.HasTag,
trigger: "until-earned",
active: "until-earned",
hidden: false,
filter(item) {
return item.tags?.includes("with-attachment") ?? false;
@@ -37,9 +36,8 @@ const ItemCreatedTracker = {
achievement: ItemCreatedMetric,
name: `[TRACKER] ${ItemCreatedProgress}`,
description: `Measures ${ItemCreatedProgress}`,
metric: ItemCreatedProgress,
validator: Type.Threshold,
trigger: { high: 1 },
active: { metric: ItemCreatedProgress, high: 1 },
hidden: true,
filter(item) {
return item.action === "vault-item-added";
@@ -53,10 +51,9 @@ const ItemCreatedTracker = {
const ItemCreatedValidator = {
achievement: ItemCreatedAchievement,
name: "What an item!",
metric: ItemCreatedProgress,
description: "Add an item to your vault",
validator: Type.Threshold,
trigger: { high: 1 },
active: { metric: ItemCreatedProgress, high: 1 },
hidden: false,
filter(item) {
return item.action === "vault-item-added";
@@ -73,9 +70,8 @@ const ThreeItemsCreatedValidator = {
achievement: ThreeItemsCreatedAchievement,
name: "Three times a charm",
description: "Add three items to your vault",
metric: ItemCreatedProgress,
validator: Type.Threshold,
trigger: { low: 2, high: 3 },
active: { metric: ItemCreatedProgress, low: 2, high: 3 },
hidden: false,
filter(item) {
return item.action === "vault-item-added";
@@ -94,9 +90,8 @@ const FiveItemsCreatedValidator = {
achievement: FiveItemsCreatedAchievement,
name: "fiiivvve GoOoOoOolllllllD RIIIIIINGS!!!!!!",
description: "Add five items to your vault",
metric: ItemCreatedProgress,
validator: Type.Threshold,
trigger: { low: 4, high: 5 },
active: { metric: ItemCreatedProgress, low: 4, high: 5 },
hidden: false,
filter(item) {
return item.action === "vault-item-added";

View File

@@ -20,6 +20,17 @@ export type AchievementEarnedEvent = EventFormat &
UserFormat & { achievement: { type: "earned"; name: AchievementId } };
export type AchievementEvent = AchievementProgressEvent | AchievementEarnedEvent;
type MetricCriteria = {
// the metric observed by low/high triggers
metric: MetricId;
} & RequireAtLeastOne<{
// criteria fail when the metric is less than or equal to `low`
low: number;
// criteria fail when the metric is greater than `high`
high: number;
}>;
type ActiveCriteria = "until-earned" | MetricCriteria;
// consumed by validator and achievement list (should this include a "toast-alerter"?)
export type Achievement = {
// identifies the achievement being monitored
@@ -31,15 +42,13 @@ export type Achievement = {
// human-readable description of the achievement
description?: string;
// the metric observed by low/high triggers
metric?: MetricId;
// conditions that determine when the achievement validator should be loaded
// by the processor
active: ActiveCriteria;
// identifies the validator containing filter/measure/earn methods
validator: ValidatorId;
// pre-filter that disables the rule if it's met
trigger: "until-earned" | RequireAtLeastOne<{ low: number; high: number }>;
// whether or not the achievement is hidden until it is earned
hidden: boolean;
};