noogle/components/preview/preview.tsx

197 lines
5.8 KiB
TypeScript
Raw Normal View History

2022-11-26 12:36:08 +03:00
import {
Box,
2022-12-09 18:41:59 +03:00
Container,
IconButton,
2022-11-26 12:36:08 +03:00
Link,
List,
ListItem,
ListItemButton,
2022-11-26 12:36:08 +03:00
ListItemIcon,
ListItemSecondaryAction,
ListItemText,
Tooltip,
2022-11-26 12:36:08 +03:00
Typography,
} from "@mui/material";
// import CloseIcon from "@mui/icons-material/Close";
import Highlight from "react-highlight";
2022-11-26 12:36:08 +03:00
import LocalLibraryIcon from "@mui/icons-material/LocalLibrary";
import InputIcon from "@mui/icons-material/Input";
import { DocItem } from "../../types/nix";
import CodeIcon from "@mui/icons-material/Code";
2022-12-09 18:41:59 +03:00
import ReactMarkdown from "react-markdown";
import ExpandLessIcon from "@mui/icons-material/ExpandLess";
2022-12-11 23:22:45 +03:00
import styles from "./preview.module.css";
// import hljs from "highlight.js";
// needed for nextjs to import the classes of github theme
import "highlight.js/styles/github.css";
2022-11-26 12:36:08 +03:00
interface PreviewProps {
docItem: DocItem;
handleClose: () => void;
2022-11-26 12:36:08 +03:00
}
export const Preview = (props: PreviewProps) => {
const { docItem, handleClose } = props;
const { name, description, category, example, fn_type } = docItem;
2022-12-09 18:41:59 +03:00
const getGeneratedExamples = () => {
if (description) {
const regex = /(```.+?```)/gms;
console.log({ description, regex });
if (typeof description === "object") {
return description
.join(" ")
.match(regex)
?.join("\n#---\n")
?.replaceAll("```nix", "")
?.replaceAll("```", "");
2022-12-09 18:41:59 +03:00
} else {
return description
.match(regex)
?.join("\n#---\n")
?.replaceAll("```nix", "")
?.replaceAll("```", "");
2022-12-09 18:41:59 +03:00
}
}
};
const finalExample = example || getGeneratedExamples();
console.log({ finalExample });
const prefix = category.split(/([\/.])/gm).at(4) || "builtins";
const libName = category
.match(/(?:[a-zA-Z]*)\.nix/gm)?.[0]
?.replace(".nix", "");
const docsRef = `https://nixos.org/manual/nixpkgs/stable/#function-library-lib.${libName}.${name}`;
2022-11-26 12:36:08 +03:00
return (
2022-12-03 19:38:50 +03:00
<Box
sx={{
p: 1,
width: "100%",
2022-12-11 23:22:45 +03:00
overflow: "none",
2022-12-03 19:38:50 +03:00
}}
>
2022-12-11 23:22:45 +03:00
<Box
sx={{
display: { md: "flex", xs: "flex" },
flexDirection: { md: "row", xs: "column-reverse" },
width: "100%",
}}
>
<Typography
variant="h4"
sx={{ wordWrap: "normal", lineBreak: "anywhere" }}
>{`${prefix}.${name}`}</Typography>
<Tooltip title="close details">
<IconButton
sx={{
mx: { xs: "auto", md: 1 },
left: { lg: "calc(50% - 2rem)", xs: "unset" },
position: { lg: "absolute", xs: "relative" },
}}
size="small"
onClick={() => handleClose()}
>
<ExpandLessIcon fontSize="large" />
</IconButton>
</Tooltip>
</Box>
2022-11-26 12:36:08 +03:00
<List sx={{ width: "100%" }}>
2022-12-11 23:22:45 +03:00
<ListItem sx={{ flexDirection: { xs: "column", sm: "row" } }}>
2022-11-26 12:36:08 +03:00
<ListItemIcon>
2022-12-11 23:22:45 +03:00
<LocalLibraryIcon sx={{ m: "auto" }} />
2022-11-26 12:36:08 +03:00
</ListItemIcon>
<ListItemText
sx={{
overflow: "hidden",
textOverflow: "ellipsis",
2022-12-11 23:22:45 +03:00
alignSelf: "flex-start",
}}
2022-12-09 18:41:59 +03:00
primaryTypographyProps={{
color: "text.secondary",
fontSize: 14,
}}
secondaryTypographyProps={{
color: "text.primary",
fontSize: "1rem",
}}
primary={"nixpkgs/" + category.replace("./", "")}
secondary={
<Container
2022-12-11 23:22:45 +03:00
component={"div"}
2022-12-09 18:41:59 +03:00
sx={{ ml: "0 !important", pl: "0 !important" }}
maxWidth="lg"
>
{typeof description === "object"
? description.map((d, idx) => (
<ReactMarkdown key={idx}>{d}</ReactMarkdown>
))
: description && <ReactMarkdown>{description}</ReactMarkdown>}
</Container>
}
2022-11-26 12:36:08 +03:00
/>
</ListItem>
2022-12-11 23:22:45 +03:00
<ListItem sx={{ flexDirection: { xs: "column", sm: "row" } }}>
2022-11-26 12:36:08 +03:00
<ListItemIcon>
2022-12-11 23:22:45 +03:00
<InputIcon sx={{ m: "auto" }} />
2022-11-26 12:36:08 +03:00
</ListItemIcon>
<ListItemText
sx={{
overflow: "hidden",
textOverflow: "ellipsis",
2022-12-11 23:22:45 +03:00
alignSelf: "flex-start",
}}
2022-12-09 18:41:59 +03:00
primaryTypographyProps={{
color: "text.secondary",
fontSize: 14,
}}
secondaryTypographyProps={{
color: "text.primary",
fontSize: "1rem",
}}
secondary={fn_type || "no type yet provided"}
primary="function signature "
/>
2022-11-26 12:36:08 +03:00
</ListItem>
<ListItem
sx={{
backgroundColor: "background.paper",
2022-12-11 23:22:45 +03:00
flexDirection: { xs: "column", sm: "row" },
}}
>
2022-11-26 12:36:08 +03:00
<ListItemIcon>
2022-12-11 23:22:45 +03:00
<CodeIcon sx={{ m: "auto" }} />
2022-11-26 12:36:08 +03:00
</ListItemIcon>
<ListItemText
sx={{
overflow: "hidden",
textOverflow: "ellipsis",
2022-12-11 23:22:45 +03:00
alignSelf: "flex-start",
}}
disableTypography
primary={
2022-12-09 18:41:59 +03:00
finalExample && (
2022-12-08 01:10:58 +03:00
<Typography sx={{ color: "text.secondary" }}>
Example
</Typography>
)
}
secondary={
2022-12-09 18:41:59 +03:00
finalExample ? (
2022-12-11 23:22:45 +03:00
<Box sx={{ mt: -2 }}>
<Highlight className={`nix ${styles.hljs}`}>
{finalExample}
</Highlight>
2022-12-08 01:10:58 +03:00
</Box>
) : (
<Typography
sx={{ color: "text.secondary" }}
>{`no example yet provided`}</Typography>
)
}
/>
2022-11-26 12:36:08 +03:00
</ListItem>
</List>
</Box>
);
};