2026-08-29 09:37:34 -04:00
|
|
|
import Stack from "@mui/material/Stack";
|
2026-08-29 10:51:07 -04:00
|
|
|
import Box from "@mui/material/Box";
|
2026-08-29 09:37:34 -04:00
|
|
|
import Typography from "@mui/material/Typography";
|
|
|
|
|
import FormControlLabel from "@mui/material/FormControlLabel";
|
|
|
|
|
import { useTheme } from "@mui/material/styles";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import { useMemo } from "react";
|
|
|
|
|
|
2026-08-29 10:03:59 -04:00
|
|
|
import { Dialog, Checkbox } from "@/Components/inputs";
|
2026-08-29 09:37:34 -04:00
|
|
|
import { LAYOUT } from "@/Utils/Theme/constants";
|
|
|
|
|
import { CardGroups, type CardDefinition, type CardGroup, type CardId } from "../cards";
|
|
|
|
|
|
|
|
|
|
interface CardPickerProps {
|
|
|
|
|
open: boolean;
|
|
|
|
|
available: CardDefinition[];
|
|
|
|
|
selected: CardId[];
|
|
|
|
|
onAdd: (id: CardId) => void;
|
|
|
|
|
onRemove: (id: CardId) => void;
|
|
|
|
|
onReset: () => void;
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cards are grouped by sidebar section, not by load cost: you look for a card
|
|
|
|
|
* where you look for the feature it belongs to. Admin-only cards never reach
|
|
|
|
|
* `available` for a non-admin, so nobody is offered a card they cannot load.
|
|
|
|
|
*/
|
|
|
|
|
export const CardPicker = ({
|
|
|
|
|
open,
|
|
|
|
|
available,
|
|
|
|
|
selected,
|
|
|
|
|
onAdd,
|
|
|
|
|
onRemove,
|
|
|
|
|
onReset,
|
|
|
|
|
onClose,
|
|
|
|
|
}: CardPickerProps) => {
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const grouped = useMemo(() => {
|
|
|
|
|
return CardGroups.map((group: CardGroup) => ({
|
|
|
|
|
group,
|
|
|
|
|
cards: available.filter((card) => card.group === group),
|
|
|
|
|
})).filter((entry) => entry.cards.length > 0);
|
|
|
|
|
}, [available]);
|
|
|
|
|
|
|
|
|
|
const chosen = new Set(selected);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog
|
|
|
|
|
open={open}
|
|
|
|
|
maxWidth="sm"
|
|
|
|
|
fullWidth
|
|
|
|
|
title={t("pages.dashboard.picker.title")}
|
2026-08-29 10:03:59 -04:00
|
|
|
// The dialog lays its actions out as cancel → additional → confirm, all
|
|
|
|
|
// right-aligned. Reset takes the cancel slot with an auto right margin
|
|
|
|
|
// so it sits alone on the left, away from Done — a destructive action
|
|
|
|
|
// should not be adjacent to the one people reach for by reflex.
|
|
|
|
|
onCancel={onReset}
|
|
|
|
|
cancelText={t("pages.dashboard.picker.reset")}
|
|
|
|
|
cancelSx={{ marginRight: "auto" }}
|
2026-08-29 10:51:07 -04:00
|
|
|
// Escape must dismiss, not reset. The dialog sends Escape to onCancel by
|
|
|
|
|
// default, and this one's cancel slot resets the selection — so without
|
|
|
|
|
// this, Escape would wipe the user's cards instead of closing.
|
|
|
|
|
onEscape={onClose}
|
2026-08-29 10:03:59 -04:00
|
|
|
// Changes apply immediately, so Done only dismisses; it is the primary
|
|
|
|
|
// action here and carries the usual contained styling.
|
|
|
|
|
onConfirm={onClose}
|
|
|
|
|
confirmText={t("pages.dashboard.picker.done")}
|
2026-08-29 09:37:34 -04:00
|
|
|
>
|
2026-08-29 10:51:07 -04:00
|
|
|
{/*
|
|
|
|
|
* Two columns: seventeen cards in one list makes the dialog scroll,
|
|
|
|
|
* which hides most of the catalogue behind a scrollbar. Groups flow
|
|
|
|
|
* down the columns and are kept whole, so a group is never split
|
|
|
|
|
* across the gap.
|
|
|
|
|
*/}
|
|
|
|
|
<Box
|
2026-08-29 09:37:34 -04:00
|
|
|
pt={theme.spacing(LAYOUT.SM)}
|
2026-08-29 10:51:07 -04:00
|
|
|
sx={{
|
|
|
|
|
columnCount: { xs: 1, sm: 2 },
|
|
|
|
|
columnGap: theme.spacing(LAYOUT.XL),
|
|
|
|
|
}}
|
2026-08-29 09:37:34 -04:00
|
|
|
>
|
|
|
|
|
{grouped.map(({ group, cards }) => (
|
|
|
|
|
<Stack
|
|
|
|
|
key={group}
|
|
|
|
|
gap={theme.spacing(LAYOUT.XXS)}
|
2026-08-29 10:51:07 -04:00
|
|
|
pb={theme.spacing(LAYOUT.MD)}
|
|
|
|
|
sx={{ breakInside: "avoid" }}
|
2026-08-29 09:37:34 -04:00
|
|
|
>
|
|
|
|
|
<Typography
|
|
|
|
|
variant="eyebrow"
|
|
|
|
|
color={theme.palette.text.disabled}
|
|
|
|
|
>
|
2026-08-29 10:03:59 -04:00
|
|
|
{/*
|
|
|
|
|
* The picker has its own group labels rather than reusing the
|
|
|
|
|
* sidebar's: the Logs page hosts logs, queue and diagnostics
|
|
|
|
|
* tabs, so "Logs" would misdescribe the three cards under it.
|
|
|
|
|
*/}
|
|
|
|
|
{t(`pages.dashboard.picker.groups.${group}`, {
|
|
|
|
|
defaultValue: group,
|
|
|
|
|
})}
|
2026-08-29 09:37:34 -04:00
|
|
|
</Typography>
|
|
|
|
|
{cards.map((card) => (
|
|
|
|
|
<FormControlLabel
|
|
|
|
|
key={card.id}
|
|
|
|
|
label={
|
2026-08-29 11:37:10 -04:00
|
|
|
<Typography color={theme.palette.text.primary}>
|
2026-08-29 09:37:34 -04:00
|
|
|
{t(`pages.dashboard.cards.${card.key}.title`)}
|
|
|
|
|
</Typography>
|
|
|
|
|
}
|
|
|
|
|
control={
|
|
|
|
|
<Checkbox
|
|
|
|
|
checked={chosen.has(card.id)}
|
|
|
|
|
onChange={() =>
|
|
|
|
|
chosen.has(card.id) ? onRemove(card.id) : onAdd(card.id)
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Stack>
|
|
|
|
|
))}
|
2026-08-29 10:51:07 -04:00
|
|
|
</Box>
|
2026-08-29 09:37:34 -04:00
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
};
|