|
| 1 | +import type { ProjectDetail } from "@squonk/data-manager-client"; |
| 2 | + |
1 | 3 | import { AccountTreeRounded as AccountTreeRoundedIcon } from "@mui/icons-material";
|
2 |
| -import { ListItem, ListItemIcon, ListItemText } from "@mui/material"; |
| 4 | +import { ListItemButton, ListItemIcon, ListItemText, Tooltip } from "@mui/material"; |
| 5 | +import { useRouter } from "next/router"; |
| 6 | + |
| 7 | +import { useCurrentProjectId } from "../../hooks/projectHooks"; |
| 8 | + |
| 9 | +type ProjectClickActions = "select-project" | "navigate-to-project"; |
3 | 10 |
|
4 | 11 | export interface ProjectListItemProps {
|
5 |
| - projectName: string; |
| 12 | + project: ProjectDetail; |
| 13 | + clickAction: ProjectClickActions; |
6 | 14 | }
|
7 | 15 |
|
8 |
| -export const ProjectListItem = ({ projectName }: ProjectListItemProps) => { |
| 16 | +export const TOOLTIPS = { |
| 17 | + "select-project": "Select project", |
| 18 | + "navigate-to-project": "Go to project", |
| 19 | +} as const satisfies Record<ProjectClickActions, string>; |
| 20 | + |
| 21 | +export const ProjectListItem = ({ project, clickAction }: ProjectListItemProps) => { |
| 22 | + const { projectId, setCurrentProjectId } = useCurrentProjectId(); |
| 23 | + const { push } = useRouter(); |
| 24 | + |
| 25 | + const action = |
| 26 | + clickAction === "navigate-to-project" || projectId === project.project_id |
| 27 | + ? "navigate-to-project" |
| 28 | + : "select-project"; |
| 29 | + |
| 30 | + const onClick = () => { |
| 31 | + setCurrentProjectId(project.project_id); |
| 32 | + if (action === "navigate-to-project") { |
| 33 | + push("/project"); |
| 34 | + } |
| 35 | + }; |
9 | 36 | return (
|
10 |
| - <ListItem> |
11 |
| - <ListItemIcon sx={{ minWidth: "40px" }}> |
12 |
| - <AccountTreeRoundedIcon /> |
13 |
| - </ListItemIcon> |
14 |
| - <ListItemText primary={projectName} /> |
15 |
| - </ListItem> |
| 37 | + <Tooltip title={TOOLTIPS[action]}> |
| 38 | + <ListItemButton component="li" sx={{ flexGrow: 0 }} onClick={onClick}> |
| 39 | + <ListItemIcon sx={{ minWidth: "40px" }}> |
| 40 | + <AccountTreeRoundedIcon /> |
| 41 | + </ListItemIcon> |
| 42 | + <ListItemText primary={project.name} /> |
| 43 | + </ListItemButton> |
| 44 | + </Tooltip> |
16 | 45 | );
|
17 | 46 | };
|
0 commit comments