All files / src/utils/components/question-bank QuestionBankFilters.jsx

70.83% Statements 17/24
60% Branches 15/25
66.66% Functions 6/9
76.19% Lines 16/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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                      3x                   667x   3x 667x   667x                                     3x 116x   116x                 116x                                 667x 667x 667x   667x                                                                                                                         3x             29x                                                                                           116x                      
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Checkbox from "@mui/material/Checkbox";
import Divider from "@mui/material/Divider";
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
import { alpha, useTheme } from "@mui/material/styles";
import SoftChip from "@/utils/components/feature-access/SoftChip";
import { getSoftBadgeTypographySx } from "@/utils/features/featureAccess";
 
/** Friendlier labels for the sidebar; filter `value` stays unchanged for the API. */
const FILTER_DISPLAY_LABELS = {
    "Multiple-choice": "Multiple choice",
    "Free-response": "Free response",
    "Drop-down-Menu": "Drop-down menu",
    "Drag-and-Drop": "Drag and drop",
    "Embedded-Multiple-Choice": "Embedded multiple-choice",
    "Single-Stimulus": "Single stimulus",
    "Multi-Part-Question": "Multi-part question",
};
 
const getDisplayLabel = (label) => FILTER_DISPLAY_LABELS[label] ?? label;
 
const FilterCountBadge = ({ count }) => {
    const theme = useTheme();
 
    return (
        <SoftChip
            label={count ?? 0}
            color="info"
            size="sm"
            sx={{
                ...getSoftBadgeTypographySx(theme, "sm"),
                flexShrink: 0,
                minWidth: 28,
                height: 22,
                "& .MuiChip-label": {
                    px: 0.75,
                    fontVariantNumeric: "tabular-nums",
                },
            }}
        />
    );
};
 
export const FilterGroup = ({ id, title, options, selected, onChange, onToggle, counts, sx, optionIdPrefix }) => {
    const theme = useTheme();
 
    const handleChange = (event) => {
        if (onToggle) {
            const { value, checked } = event.target;
            onToggle((prev) => (checked ? [...prev, value] : prev.filter((item) => item !== value)));
            return;
        }
        onChange?.(event);
    };
 
    return (
        <Box component="section" aria-labelledby={id} sx={sx}>
            <Typography
                id={id}
                variant="subtitle2"
                component="h3"
                sx={{
                    fontWeight: 600,
                    color: "text.primary",
                    letterSpacing: "0.02em",
                    mb: 1,
                }}
            >
                {title}
            </Typography>
            <Stack spacing={0.25} role="group" aria-label={title}>
                {options.map(({ value, label }) => {
                    const checked = selected.includes(value);
                    const displayLabel = getDisplayLabel(label);
                    const checkboxId = optionIdPrefix ? `${optionIdPrefix}-${value}` : `${id}-${value}`;
 
                    return (
                        <Box
                            key={value}
                            component="label"
                            htmlFor={checkboxId}
                            sx={{
                                display: "flex",
                                alignItems: "center",
                                gap: 1,
                                py: 0.5,
                                px: 1,
                                mx: -1,
                                borderRadius: 1,
                                cursor: "pointer",
                                transition: theme.transitions.create("background-color", {
                                    duration: theme.transitions.duration.shortest,
                                }),
                                ...(checked && {
                                    bgcolor: alpha(theme.palette.primary.main, 0.08),
                                }),
                                "&:hover": {
                                    bgcolor: alpha(
                                        theme.palette.primary.main,
                                        checked ? 0.1 : 0.06
                                    ),
                                },
                            }}
                        >
                            <Checkbox
                                id={checkboxId}
                                size="small"
                                value={value}
                                checked={checked}
                                onChange={handleChange}
                                sx={{ p: 0.25, flexShrink: 0 }}
                            />
                            <Typography
                                variant="body2"
                                component="span"
                                sx={{
                                    flex: 1,
                                    minWidth: 0,
                                    lineHeight: 1.4,
                                    color: "text.primary",
                                    fontFamily: theme.typography.fontFamily,
                                }}
                            >
                                {displayLabel}
                            </Typography>
                            <FilterCountBadge count={counts?.[label] ?? counts?.[value] ?? 0} />
                        </Box>
                    );
                })}
            </Stack>
        </Box>
    );
};
 
/**
 * Question bank left-rail filters (assignment type, question type, category, difficulty).
 */
const QuestionBankFilters = ({
    role = "teacher",
    groups,
    onClearAll,
    clearDisabled = false,
    sx,
}) => {
    return (
        <Box
            className="question-bank-filters"
            sx={{
                display: "flex",
                flexDirection: "column",
                flex: "0 0 240px",
                minWidth: 220,
                maxWidth: 260,
                borderRight: 1,
                borderColor: "divider",
                pr: 2.5,
                ...sx,
            }}
        >
            <Stack spacing={2.5} sx={{ pt: 4, pb: 5 }}>
                <Box
                    sx={{
                        display: "flex",
                        alignItems: "center",
                        justifyContent: "space-between",
                        gap: 1,
                    }}
                >
                    <Typography variant="subtitle1" component="h2" fontWeight={600}>
                        Filters
                    </Typography>
                    <Button
                        size="small"
                        variant="text"
                        color="primary"
                        disabled={clearDisabled}
                        onClick={onClearAll}
                        sx={{
                            flexShrink: 0,
                            textTransform: "none",
                            fontWeight: 500,
                            minWidth: "auto",
                            px: 1,
                        }}
                    >
                        Clear all
                    </Button>
                </Box>
 
                {groups.map((group, index) => (
                    <Box key={group.id ?? group.title}>
                        {index > 0 && <Divider sx={{ mb: 2.5 }} />}
                        <FilterGroup {...group} id={group.id ?? `${role}-question-bank-filter-${index}`} />
                    </Box>
                ))}
            </Stack>
        </Box>
    );
};
 
export default QuestionBankFilters;