2024-02-12 07:37:28 +01:00
|
|
|
import { Box, Center } from "@mantine/core"
|
2022-11-08 11:51:26 +01:00
|
|
|
import { FeedFavicon } from "components/content/FeedFavicon"
|
2023-12-28 19:54:51 +01:00
|
|
|
import React, { type ReactNode } from "react"
|
2023-12-29 21:53:41 +01:00
|
|
|
import { tss } from "tss"
|
2022-08-13 10:56:07 +02:00
|
|
|
import { UnreadCount } from "./UnreadCount"
|
|
|
|
|
|
|
|
|
|
interface TreeNodeProps {
|
|
|
|
|
id: string
|
2023-05-08 12:36:58 +02:00
|
|
|
name: ReactNode
|
|
|
|
|
icon: ReactNode
|
2022-08-13 10:56:07 +02:00
|
|
|
unread: number
|
|
|
|
|
selected: boolean
|
|
|
|
|
expanded?: boolean
|
|
|
|
|
level: number
|
|
|
|
|
hasError: boolean
|
|
|
|
|
onClick: (e: React.MouseEvent, id: string) => void
|
|
|
|
|
onIconClick?: (e: React.MouseEvent, id: string) => void
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 21:53:41 +01:00
|
|
|
const useStyles = tss
|
|
|
|
|
.withParams<{
|
|
|
|
|
selected: boolean
|
|
|
|
|
hasError: boolean
|
|
|
|
|
hasUnread: boolean
|
|
|
|
|
}>()
|
2023-12-29 23:09:30 +01:00
|
|
|
.create(({ theme, colorScheme, selected, hasError, hasUnread }) => {
|
2023-12-29 21:53:41 +01:00
|
|
|
let backgroundColor = "inherit"
|
2024-01-01 18:24:39 +01:00
|
|
|
if (selected) backgroundColor = colorScheme === "dark" ? theme.colors.dark[4] : theme.colors.gray[1]
|
2022-08-13 10:56:07 +02:00
|
|
|
|
2023-12-29 21:53:41 +01:00
|
|
|
let color
|
|
|
|
|
if (hasError) {
|
|
|
|
|
color = theme.colors.red[6]
|
2023-12-29 23:09:30 +01:00
|
|
|
} else if (colorScheme === "dark") {
|
2023-12-29 21:53:41 +01:00
|
|
|
color = hasUnread ? theme.colors.dark[0] : theme.colors.dark[3]
|
|
|
|
|
} else {
|
|
|
|
|
color = hasUnread ? theme.black : theme.colors.gray[6]
|
|
|
|
|
}
|
2022-08-13 10:56:07 +02:00
|
|
|
|
2023-12-29 21:53:41 +01:00
|
|
|
return {
|
|
|
|
|
node: {
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
cursor: "pointer",
|
|
|
|
|
color,
|
|
|
|
|
backgroundColor,
|
|
|
|
|
"&:hover": {
|
2023-12-29 23:09:30 +01:00
|
|
|
backgroundColor: colorScheme === "dark" ? theme.colors.dark[6] : theme.colors.gray[0],
|
2023-12-29 21:53:41 +01:00
|
|
|
},
|
2022-08-13 10:56:07 +02:00
|
|
|
},
|
2023-12-29 21:53:41 +01:00
|
|
|
nodeText: {
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
whiteSpace: "nowrap",
|
|
|
|
|
overflow: "hidden",
|
|
|
|
|
textOverflow: "ellipsis",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
})
|
2022-08-13 10:56:07 +02:00
|
|
|
|
|
|
|
|
export function TreeNode(props: TreeNodeProps) {
|
2023-12-29 21:53:41 +01:00
|
|
|
const { classes } = useStyles({
|
|
|
|
|
selected: props.selected,
|
|
|
|
|
hasError: props.hasError,
|
|
|
|
|
hasUnread: props.unread > 0,
|
|
|
|
|
})
|
2022-08-13 10:56:07 +02:00
|
|
|
return (
|
|
|
|
|
<Box py={1} pl={props.level * 20} className={classes.node} onClick={(e: React.MouseEvent) => props.onClick(e, props.id)}>
|
2023-12-29 21:53:41 +01:00
|
|
|
<Box mr={6} onClick={(e: React.MouseEvent) => props.onIconClick?.(e, props.id)}>
|
2023-05-23 15:17:04 +02:00
|
|
|
<Center>{typeof props.icon === "string" ? <FeedFavicon url={props.icon} /> : props.icon}</Center>
|
2022-08-13 10:56:07 +02:00
|
|
|
</Box>
|
|
|
|
|
<Box className={classes.nodeText}>{props.name}</Box>
|
|
|
|
|
{!props.expanded && (
|
|
|
|
|
<Box>
|
|
|
|
|
<UnreadCount unreadCount={props.unread} />
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</Box>
|
|
|
|
|
)
|
|
|
|
|
}
|