From 2f457729e61020295ad3be6e1e58dc971db8be23 Mon Sep 17 00:00:00 2001 From: Hussein Martinez Date: Mon, 2 Dec 2024 02:32:29 +0700 Subject: [PATCH] fix: storybook and invalid badges --- src/components/Badges/PoolBadge/PoolBadge.mdx | 8 ++++---- .../Badges/PoolBadge/PoolBadge.stories.tsx | 6 ++++-- .../PoolStatusBadge.stories.tsx | 2 +- .../PoolStatusBadge/PoolStatusBadge.tsx | 20 +++++++++++-------- .../PoolTypeBadge/PoolTypeBadge.stories.tsx | 2 +- .../Badges/PoolTypeBadge/PoolTypeBadge.tsx | 20 +++++++++++-------- 6 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/components/Badges/PoolBadge/PoolBadge.mdx b/src/components/Badges/PoolBadge/PoolBadge.mdx index f1f2a8b..d649633 100644 --- a/src/components/Badges/PoolBadge/PoolBadge.mdx +++ b/src/components/Badges/PoolBadge/PoolBadge.mdx @@ -11,17 +11,17 @@ The `PoolBadge` component is a versatile UI element that indicates either the st # Example - + # Controls - + - + # Controls - + # Other variations diff --git a/src/components/Badges/PoolBadge/PoolBadge.stories.tsx b/src/components/Badges/PoolBadge/PoolBadge.stories.tsx index 81ec17d..5df1757 100644 --- a/src/components/Badges/PoolBadge/PoolBadge.stories.tsx +++ b/src/components/Badges/PoolBadge/PoolBadge.stories.tsx @@ -31,7 +31,8 @@ type Story = StoryObj; export const Default: Story = {}; -export const TypePoolStatus: Story = { +export const PoolStatusStory: Story = { + name: "Pool Status Badge", argTypes: { badge: { control: "select", @@ -52,7 +53,8 @@ export const TypePoolStatus: Story = { }; // Story for poolType -export const TypePoolType: Story = { +export const PoolTypeStory: Story = { + name: "Pool Type Badge", argTypes: { badge: { control: "select", diff --git a/src/components/Badges/PoolStatusBadge/PoolStatusBadge.stories.tsx b/src/components/Badges/PoolStatusBadge/PoolStatusBadge.stories.tsx index 10dc93a..acbb26c 100644 --- a/src/components/Badges/PoolStatusBadge/PoolStatusBadge.stories.tsx +++ b/src/components/Badges/PoolStatusBadge/PoolStatusBadge.stories.tsx @@ -5,7 +5,7 @@ import { PoolStatus } from "@/types"; import { PoolStatusBadge } from "./PoolStatusBadge"; const meta: Meta = { - title: "Components/Badges/PoolStatusBadge", + title: "Components/Badges/PoolBadge/PoolStatusBadge", component: PoolStatusBadge, parameters: { toc: { disable: true }, diff --git a/src/components/Badges/PoolStatusBadge/PoolStatusBadge.tsx b/src/components/Badges/PoolStatusBadge/PoolStatusBadge.tsx index 34730df..6e4282e 100644 --- a/src/components/Badges/PoolStatusBadge/PoolStatusBadge.tsx +++ b/src/components/Badges/PoolStatusBadge/PoolStatusBadge.tsx @@ -1,10 +1,11 @@ import * as React from "react"; import { tv } from "tailwind-variants"; +import { match, P } from "ts-pattern"; import { cn } from "@/lib/utils"; import { Badge } from "@/primitives"; -import { PoolStatus } from "@/types"; +import { isPoolStatus, PoolStatus } from "@/types"; const variants = tv({ variants: { @@ -17,24 +18,27 @@ const variants = tv({ }, }); -const statusBadgeTexts = { +const badgeTexts = { [PoolStatus.PreRound]: "Pre round", [PoolStatus.RoundInProgress]: "Round in progress", [PoolStatus.ApplicationsInProgress]: "Applications in progress", [PoolStatus.FundingPending]: "Funding pending", }; +const invalidValueText = "Error: Invalid status"; + export interface PoolStatusBadgeProps { value?: PoolStatus; className?: string; } export const PoolStatusBadge: React.FC = ({ value, className }) => { - const statusVariant = variants({ value }); + const variantClass = variants({ value }); - return value ? ( - {statusBadgeTexts[value]} - ) : ( - - ); + return match({ value }) + .with({ value: undefined }, () => ) + .with({ value: P.when(isPoolStatus) }, ({ value }) => ( + {badgeTexts[value]} + )) + .otherwise(() => {invalidValueText}); }; diff --git a/src/components/Badges/PoolTypeBadge/PoolTypeBadge.stories.tsx b/src/components/Badges/PoolTypeBadge/PoolTypeBadge.stories.tsx index f8c0207..9120f72 100644 --- a/src/components/Badges/PoolTypeBadge/PoolTypeBadge.stories.tsx +++ b/src/components/Badges/PoolTypeBadge/PoolTypeBadge.stories.tsx @@ -5,7 +5,7 @@ import { PoolType } from "@/types"; import { PoolTypeBadge } from "./PoolTypeBadge"; const meta: Meta = { - title: "Components/Badges/PoolTypeBadge", + title: "Components/Badges/PoolBadge/PoolTypeBadge", component: PoolTypeBadge, }; diff --git a/src/components/Badges/PoolTypeBadge/PoolTypeBadge.tsx b/src/components/Badges/PoolTypeBadge/PoolTypeBadge.tsx index c42e504..e5e262b 100644 --- a/src/components/Badges/PoolTypeBadge/PoolTypeBadge.tsx +++ b/src/components/Badges/PoolTypeBadge/PoolTypeBadge.tsx @@ -1,10 +1,11 @@ import * as React from "react"; import { tv } from "tailwind-variants"; +import { match, P } from "ts-pattern"; import { cn } from "@/lib/utils"; import { Badge } from "@/primitives"; -import { PoolType } from "@/types"; +import { isPoolType, PoolType } from "@/types"; const variants = tv({ base: "border-transparent", @@ -21,17 +22,20 @@ export interface PoolTypeBadgeProps { className?: string; } -const typeBadgeTexts = { +const badgeTexts = { [PoolType.QuadraticFunding]: "Quadratic funding", [PoolType.DirectGrants]: "Direct grants", }; +const invalidValueText = "Error: Invalid type"; + export const PoolTypeBadge: React.FC = ({ value, className }) => { - const typeVariant = variants({ value }); + const variantClass = variants({ value }); - return value ? ( - {typeBadgeTexts[value]} - ) : ( - - ); + return match({ value }) + .with({ value: undefined }, () => ) + .with({ value: P.when(isPoolType) }, ({ value }) => ( + {badgeTexts[value]} + )) + .otherwise(() => {invalidValueText}); };