/**
* (c) 2026, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import {
Button,
Card,
CardBody,
Checkbox,
HStack,
Icon,
IconButton,
LinkBox,
LinkOverlayButton,
MenuItem,
MenuList,
MenuTrigger,
Stack,
SystemStyleObject,
Text,
VisuallyHidden,
} from "@microbit/ui";
import { ReactNode, useCallback, useRef } from "react";
import { MdMoreVert } from "react-icons/md";
import {
RiDeleteBin2Line,
RiEdit2Line,
RiFileCopyLine,
RiFolderOpenLine,
} from "react-icons/ri";
import { FormattedMessage, useIntl } from "react-intl";
import { uiPatternsMessage } from "../messages";
import { formatTimeAgo } from "./time-ago";
import { ProjectSummary } from "./types";
export interface ProjectCardProps {
project: ProjectSummary;
/**
* The app's picture of the project (a logo, a glyph, a preview), placed
* between the actions row and the name.
*/
children?: ReactNode;
/** One line under the name, e.g. what the project contains. */
description?: ReactNode;
isSelected?: boolean;
/**
* When set the card shows a selection checkbox, and this is called with the
* project's id when it is toggled.
*/
onToggleSelected?: (id: string) => void;
/**
* When set, a selected card carries a hidden "skip to toolbar" button for
* keyboard users, so they need not tab through every card to act on a
* selection.
*/
onSkipToToolbar?: () => void;
onOpen: (id: string) => void;
/**
* Rename, duplicate and delete come with the menu button that started
* them, so a dialog can return focus there once it closes (the menu has
* gone by then). useProjectActions handles this.
*/
onRename: (id: string, trigger?: HTMLElement) => void;
onDuplicate: (id: string, trigger?: HTMLElement) => void;
onDelete: (id: string, trigger?: HTMLElement) => void;
/** Per-instance overrides for the card body, e.g. tighter padding. */
bodyCss?: SystemStyleObject;
className?: string;
}
/**
* A project in a grid or carousel: actions row (optional checkbox, more
* menu), the app's content, then the name as the card's link and when it was
* last modified.
*/
export const ProjectCard = ({
project,
children,
description,
isSelected,
onToggleSelected,
onSkipToToolbar,
onOpen,
onRename,
onDuplicate,
onDelete,
bodyCss,
className,
}: ProjectCardProps) => {
const intl = useIntl();
const { id, name, timestamp } = project;
const handleOpen = useCallback(() => onOpen(id), [id, onOpen]);
const date = new Date(timestamp);
return (
{children}
{name}
{description !== undefined && (
{description}
)}
);
};
interface ProjectCardActionsProps {
id: string;
name: string;
isSelected?: boolean;
onToggleSelected?: (id: string) => void;
onSkipToToolbar?: () => void;
onOpen: (id: string) => void;
onRename: (id: string, trigger?: HTMLElement) => void;
onDuplicate: (id: string, trigger?: HTMLElement) => void;
onDelete: (id: string, trigger?: HTMLElement) => void;
}
const ProjectCardActions = ({
id,
name,
isSelected,
onToggleSelected,
onSkipToToolbar,
onOpen,
onRename,
onDuplicate,
onDelete,
}: ProjectCardActionsProps) => {
const intl = useIntl();
const menuButtonRef = useRef(null);
const trigger = () => menuButtonRef.current ?? undefined;
return (
{onToggleSelected && (
onToggleSelected(id)}
css={{
px: 5,
py: 5,
color: "brand.600",
zIndex: 1,
borderColor: "gray.500",
_hover: { backgroundColor: "blackAlpha.50" },
borderEndEndRadius: "md",
h: "60px",
}}
>
)}
{onSkipToToolbar && (
)}
}
onAction={() => onOpen(id)}
textValue={intl.formatMessage(
uiPatternsMessage("ui-patterns.open-project-action"),
)}
>
}
onAction={() => onRename(id, trigger())}
textValue={intl.formatMessage(
uiPatternsMessage("ui-patterns.rename-project-action"),
)}
>
}
onAction={() => onDuplicate(id, trigger())}
textValue={intl.formatMessage(
uiPatternsMessage("ui-patterns.duplicate-project-action"),
)}
>
}
onAction={() => onDelete(id, trigger())}
textValue={intl.formatMessage(
uiPatternsMessage("ui-patterns.delete-project-action"),
{ count: 1 },
)}
>
);
};