noogle/components/functionItem/functionItem.tsx

76 lines
2.2 KiB
TypeScript
Raw Normal View History

import { ListItemText, Paper, Stack, Typography } from "@mui/material";
2022-12-11 23:22:45 +03:00
import { useMemo } from "react";
import { DocItem } from "../../types/nix";
2022-12-11 23:22:45 +03:00
import { Preview } from "../preview/preview";
2022-11-26 12:36:08 +03:00
interface FunctionItemProps {
selected: boolean;
name: String;
docItem: DocItem;
2022-12-11 23:22:45 +03:00
handleClose: () => void;
2022-11-26 12:36:08 +03:00
}
export default function FunctionItem(props: FunctionItemProps) {
2022-12-11 23:22:45 +03:00
const { name, docItem, selected, handleClose } = props;
const { fn_type, category, description } = docItem;
const descriptionPreview = useMemo(() => {
const getFirstWords = (s: string) => {
const indexOfDot = s.indexOf(".");
if (indexOfDot) {
return s.slice(0, indexOfDot + 1);
}
return s.split(" ").filter(Boolean).slice(0, 10).join(" ");
};
if (typeof description === "object") {
const singleString = description.join("");
return getFirstWords(singleString);
} else if (description) {
return getFirstWords(description);
} else {
return "";
}
}, [description]);
2022-11-26 12:36:08 +03:00
return (
<Paper
elevation={0}
sx={{
2022-12-11 23:22:45 +03:00
cursor: !selected ? "pointer" : "default",
2022-11-26 12:36:08 +03:00
display: "flex",
justifyContent: "left",
2022-12-12 16:24:16 +03:00
px: { xs: 0, md: 2 },
2022-11-27 16:16:52 +03:00
py: 1,
2022-11-26 12:36:08 +03:00
color: selected ? "primary.main" : undefined,
2022-12-11 23:22:45 +03:00
borderColor: selected ? "primary.light" : "none",
2022-11-26 12:36:08 +03:00
borderWidth: 1,
borderStyle: selected ? "solid" : "none",
2022-12-11 23:22:45 +03:00
"&:hover": !selected
? {
backgroundColor: "action.hover",
}
: {},
2022-11-26 12:36:08 +03:00
}}
>
2022-12-11 23:22:45 +03:00
<Stack sx={{ width: "100%" }}>
{!selected && (
<>
<ListItemText
primary={`${
category.includes(".nix") ? "lib" : "builtins"
}.${name}`}
secondary={category}
/>
<ListItemText secondary={descriptionPreview} />
<Typography
sx={{
color: !fn_type ? "text.secondary" : "text.primary",
}}
>
2022-12-12 10:17:07 +03:00
{`${fn_type || "No type provided yet."} `}
2022-12-11 23:22:45 +03:00
</Typography>
</>
)}
{selected && <Preview docItem={docItem} handleClose={handleClose} />}
2022-11-26 12:36:08 +03:00
</Stack>
</Paper>
);
}