From 2f189e77ad4444d38b71a1ddcf0f18e4edfa8147 Mon Sep 17 00:00:00 2001 From: lemu Date: Mon, 4 Sep 2023 15:28:10 -0300 Subject: [PATCH] chore: show expired badges considering spec expiration date (#1234) * fix: conversion from string date to string iso date * fix: avoid trying to airdrop when badge spec upload went wrong * fix: use utc time for string conversion * chore: show expired badges considering spec expiration date * refactor: renames --- src/clients/OtterspaceSubgraph.ts | 2 +- src/components/User/Badges/Badge.css | 2 +- src/components/User/Badges/Badge.tsx | 4 +--- src/entities/Badges/types.ts | 30 ++++++++++++++++++++++++++++ src/services/BadgesService.ts | 25 ++++------------------- 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/clients/OtterspaceSubgraph.ts b/src/clients/OtterspaceSubgraph.ts index 1707201d5..6742a5cd5 100644 --- a/src/clients/OtterspaceSubgraph.ts +++ b/src/clients/OtterspaceSubgraph.ts @@ -90,7 +90,7 @@ export type OtterspaceBadgeSpec = { metadata: { name: string description: string - expiresAt?: number | null + expiresAt?: string | null image: string } raft?: { diff --git a/src/components/User/Badges/Badge.css b/src/components/User/Badges/Badge.css index cc1952cbe..9b1b57d86 100644 --- a/src/components/User/Badges/Badge.css +++ b/src/components/User/Badges/Badge.css @@ -46,6 +46,6 @@ background-position: right bottom; } -.Badge__Icon--revoked { +.Badge__Icon--past { filter: grayscale(1); } diff --git a/src/components/User/Badges/Badge.tsx b/src/components/User/Badges/Badge.tsx index 6396ddd6e..f6b392502 100644 --- a/src/components/User/Badges/Badge.tsx +++ b/src/components/User/Badges/Badge.tsx @@ -40,15 +40,13 @@ function getVariantClass(variant: BadgeVariant) { } export default function Badge({ badge, className, iconClassName, variant = BadgeVariant.Primary }: Props) { - const isRevoked = badge.status === BadgeStatus.Revoked - return (
0 && Time.utc(badge.spec.metadata.expiresAt).isBefore(Time.utc())) + ) +} + +export function toGovernanceBadge(otterspaceBadge: OtterspaceBadge) { + const { name, description, image } = otterspaceBadge.spec.metadata + const badge: Badge = { + name, + description, + createdAt: otterspaceBadge.createdAt, + image: getIpfsHttpsLink(image), + status: toBadgeStatus(otterspaceBadge.status), + isPastBadge: isPastBadge(otterspaceBadge), + } + return badge +} + +function getIpfsHttpsLink(ipfsLink: string) { + return ipfsLink.replace('ipfs://', 'https://ipfs.io/ipfs/') +} + export function isOtterspaceRevokeReason(value: string | null | undefined): boolean { switch (value) { case OtterspaceRevokeReason.Abuse: diff --git a/src/services/BadgesService.ts b/src/services/BadgesService.ts index 2f772be49..64825d879 100644 --- a/src/services/BadgesService.ts +++ b/src/services/BadgesService.ts @@ -19,12 +19,11 @@ import { Badge, BadgeCreationResult, BadgeStatus, - BadgeStatusReason, ErrorReason, OtterspaceRevokeReason, RevokeOrReinstateResult, UserBadges, - toBadgeStatus, + toGovernanceBadge, } from '../entities/Badges/types' import { getLandOwnerAddresses, @@ -54,18 +53,10 @@ export class BadgesService { const expiredBadges: Badge[] = [] for (const otterspaceBadge of otterspaceBadges) { try { - const status = toBadgeStatus(otterspaceBadge.status) - if (status !== BadgeStatus.Burned) { + const badge = toGovernanceBadge(otterspaceBadge) + if (badge.status !== BadgeStatus.Burned) { if (otterspaceBadge.spec.metadata) { - const { name, description, image } = otterspaceBadge.spec.metadata - const badge = { - name, - description, - status, - image: BadgesService.getIpfsHttpsLink(image), - createdAt: otterspaceBadge.createdAt, - } - if (this.badgeExpired(status, otterspaceBadge.statusReason)) { + if (badge.isPastBadge) { expiredBadges.push(badge) } else { currentBadges.push(badge) @@ -84,14 +75,6 @@ export class BadgesService { return { currentBadges, expiredBadges, total: currentBadges.length + expiredBadges.length } } - private static badgeExpired(status: BadgeStatus, statusReason: string) { - return status === BadgeStatus.Revoked && statusReason === BadgeStatusReason.TenureEnded - } - - private static getIpfsHttpsLink(ipfsLink: string) { - return ipfsLink.replace('ipfs://', 'https://ipfs.io/ipfs/') - } - public static async giveBadgeToUsers(badgeCid: string, users: string[]): Promise { try { const { eligibleUsers, usersWithBadgesToReinstate, error } = await getValidatedUsersForBadge(badgeCid, users)