All files / apps/wp-editor/app/utils itemStatus.ts

60.71% Statements 17/28
44.82% Branches 13/29
66.66% Functions 2/3
72.72% Lines 16/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61        1x                               1x       2x 1x 1x 1x   1x   2x 1x   1x     1x 3x   3x               2x           1x       1x    
import type { BadgeProps } from "@nuxt/ui";
import { isApplied, isPending, isReviewRequested } from "../../shared/rules.ts";
import type { Item } from "#shared/types/Item.ts";
 
export const ITEM_FILTERS = [
    "all",
    "pending",
    "requested",
    "unreviewed",
    "seen",
    "confirmed",
    "applied",
    "rejected",
    "drift",
] as const;
export type ItemFilter = (typeof ITEM_FILTERS)[number];
 
type Reviewable = Pick<Item, "proposal" | "state">;
 
/** The one badge that best says where an item stands, most decisive state first. */
export const itemBadge = ({
    state,
    proposal,
}: Reviewable): { label: string; color: BadgeProps["color"] } => {
    if (state.drift) return { label: "drift", color: "warning" };
    Iif (state.applied && !state.restored) return { label: "applied", color: "primary" };
    Iif (state.rejected) return { label: "rejected", color: "error" };
    Iif (!proposal) return { label: "no change", color: "neutral" };
 
    const suffix = state.restored ? " ยท restored" : "";
 
    Iif (state.level === "confirmed") return { label: `confirmed${suffix}`, color: "success" };
    Iif (state.level === "seen") return { label: `seen${suffix}`, color: "info" };
 
    return { label: `unreviewed${suffix}`, color: "neutral" };
};
 
export const matchesFilter = (item: Reviewable, filter: ItemFilter): boolean => {
    const { state } = item;
 
    switch (filter) {
        case "all":
            return true;
        case "pending":
            return isPending(item);
        case "requested":
            return isPending(item) && isReviewRequested(item);
        case "applied":
            return isApplied(item);
        case "rejected":
            return !!state.rejected;
        case "drift":
            return !!state.drift;
        default:
            return isPending(item) && state.level === filter;
    }
};
 
export const formatDate = (iso: string): string =>
    iso ? new Date(iso).toLocaleDateString(undefined, { dateStyle: "medium" }) : "";