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

52.52% Statements 52/99
39.47% Branches 30/76
25% Functions 3/12
57.35% Lines 39/68

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 186 187 188 189 190 191                                                                                        76x   415x     830x 415x 305x 830x     305x 305x   720x 720x     728x 830x 1245x         1025x 720x 353x   415x   768x 1121x 1474x 415x               830x 1245x 1245x 2075x                                                                                             415x 89x   89x   415x         415x 415x               415x 415x               164x     89x 89x                   89x           445x 267x          
import { Star, Users } from "lucide-react";
import { motion } from "motion/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 MotionCard = motion.create(Card);
 
const GridCard = ({ info }: CardProps) => {
  return (
    <MotionCard
      onClick={() => info.data.onClick?.()}
      aria-label={"Grid Card " + info.data.name}
      whileHover={{ scale: 1.03 }}
      whileTap={{ scale: 0.95 }}
      className="cursor-pointer"
    >
      <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>
    </MotionCard>
  );
};
 
const ListCard = ({ info }: CardProps) => {
  return (
    <MotionCard
      onClick={() => info.data.onClick?.()}
      aria-label={"List Card " + info.data.name}
      variant="list"
      whileHover={{ scale: 1.01 }}
      whileTap={{ scale: 0.97 }}
      className="cursor-pointer"
    >
      <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>
    </MotionCard>
  );
};
 
const CardGallery = (props: MainPageGalleryProps) => {
  const { data: userInfo } = useUserInfo();
 
  const cardData: CardInfo[] = 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,
    };
  });
 
  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;