All files / src/pages/main-page/components CardGallery.tsx

80.95% Statements 17/21
60% Branches 12/20
63.63% Functions 7/11
80.95% Lines 17/21

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                                                                                        49x 215x   3x                                                     5x                           49x                                                                               49x 86x   86x 86x   215x         215x 215x               215x 215x   215x                 86x             215x                                  
import { Star, Users } from "lucide-react";
import { useMemo } from "react";
import { Badge } from "@/ahuora-design-system/ui/badge";
import { ToolTipCover } from "@/ahuora-design-system/ui/tooltip";
import { OwnerRead, UserInfo } from "@/api/apiStore.gen";
import { useUserInfo } from "@/hooks/useUserInfo";
import EllipsisMenu from "@/pages/main-page/components/EllipsisMenu";
import {
  Card,
  CardContent,
  CardFooter,
} from "../../../ahuora-design-system/ui/card";
 
export type FlowsheetCardData = {
  id: number;
  name: string;
  owner: OwnerRead;
  type: string;
  imgUrl: string;
  description: string;
  badges?: string[];
  lastAccessed?: string;
  binnedAt?: string;
  isStarred?: boolean;
  isBinned?: boolean;
  onClick?: () => void;
};
 
type MainPageGalleryProps = {
  data: FlowsheetCardData[];
  viewMode: string;
};
 
type CardInfo = {
  data: FlowsheetCardData;
  date: string;
  dateText: string;
  isSharred: boolean;
};
 
type CardProps = {
  info: CardInfo;
};
 
const GridCard = ({ info }: CardProps) => {
  return (
    <Card
      onClick={() => info.data.onClick?.()}
      aria-label={"Grid Card " + info.data.name}
    >
      <CardContent>
        {info.data.imgUrl && (
          <img src={info.data.imgUrl} className="w-full h-40 object-cover" />
        )}
      </CardContent>
      <CardFooter className="flex flex-col gap-3 mt-1">
        <div className="flex flex-row items-center justify-between">
          <p className="line-clamp-2">{info.data.name}</p>
          <EllipsisMenu flowsheetCardInfo={info.data} />
        </div>
        <div className="flex flex-col justify-between items-start flex-wrap gap-3">
          <div className="flex flex-row gap-3">
            <p className="font-light text-xs">
              {info.dateText} {info.date}
            </p>
            {info.data.isStarred && <Star />}
            <ToolTipCover
              asChild
              content={`Shared by ${info.data.owner.email}`}
            >
              {info.isSharred && <Users />}
            </ToolTipCover>
          </div>
          {info.data.badges?.map((badgeItem) => (
            <Badge
              key={badgeItem}
              variant="secondary"
              className="flex flex-row items-center font-light rounded-xl px-5"
            >
              {badgeItem}
            </Badge>
          ))}
        </div>
      </CardFooter>
    </Card>
  );
};
 
const ListCard = ({ info }: CardProps) => {
  return (
    <Card
      onClick={() => info.data.onClick?.()}
      aria-label={"List Card " + info.data.name}
      variant="list"
    >
      <CardFooter className="flex flex-col gap-3 ml-2 mt-1">
        <div className="flex flex-row items-center justify-between">
          <div>
            <p className="line-clamp-2 mb-3">{info.data.name}</p>
            <div className="flex flex-row gap-5">
              <p className="flex flex-row items-center gap-1 font-light text-xs">
                {info.dateText} {info.date}
              </p>
              {info.data.isStarred && <Star />}
              <ToolTipCover
                asChild
                content={`Shared by ${info.data.owner.email}`}
              >
                {info.isSharred && <Users />}
              </ToolTipCover>
              {info.data.badges?.map((badgeItem) => (
                <Badge
                  key={badgeItem}
                  variant="secondary"
                  className="flex flex-row items-center font-light rounded-xl px-5"
                >
                  {badgeItem}
                </Badge>
              ))}
            </div>
          </div>
          <EllipsisMenu flowsheetCardInfo={info.data} />
        </div>
      </CardFooter>
    </Card>
  );
};
 
const CardGallery = (props: MainPageGalleryProps) => {
  const { data: userInfo } = useUserInfo();
 
  const cardData: CardInfo[] = useMemo(() => {
    return props.data.map((info: FlowsheetCardData) => {
      const dateText =
        info.type === "template"
          ? "Created"
          : info.isBinned
            ? "Binned"
            : "Opened";
      const binnedDate = new Date(info.binnedAt!).getTime();
      const binnedDateAsString = new Date(binnedDate).toLocaleDateString(
        "en-GB",
        {
          year: "numeric",
          month: "short",
          day: "numeric",
        },
      );
      const date = info.isBinned ? binnedDateAsString : info.description;
      const isSharred = userInfo?.id !== info.owner.id;
 
      return {
        data: info,
        date: date,
        dateText: dateText,
        isSharred: isSharred,
      };
    });
  }, [props.data, userInfo]);
 
  return (
    <>
      {props.viewMode === "grid" && (
        <div
          className={`w-full grid grid-cols-1 mb-56 gap-4 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 `}
        >
          {cardData.map((info: CardInfo, index: number) => (
            <GridCard key={index} info={info} />
          ))}
        </div>
      )}
 
      {props.viewMode === "list" && (
        <div className="grid grid-rows-1">
          {cardData.map((info: CardInfo, index: number) => (
            <ListCard key={index} info={info} />
          ))}
        </div>
      )}
    </>
  );
};
 
export default CardGallery;