noogle/components/preview/preview.tsx

240 lines
7.3 KiB
TypeScript
Raw Normal View History

import React from "react";
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
List,
ListItem,
ListItemIcon,
ListItemText,
Tooltip,
2022-11-26 12:36:08 +03:00
Typography,
2022-12-12 16:24:16 +03:00
useTheme,
Link as MuiLink,
2022-11-26 12:36:08 +03:00
} from "@mui/material";
import { DocItem } from "../../models/nix";
import CodeIcon from "@mui/icons-material/Code";
import ExpandLessIcon from "@mui/icons-material/ExpandLess";
import LocalLibraryIcon from "@mui/icons-material/LocalLibrary";
import InputIcon from "@mui/icons-material/Input";
import { MarkdownPreview } from "../markdownPreview";
import { CodeHighlight } from "../codeHighlight";
2022-12-12 16:24:16 +03:00
2022-11-26 12:36:08 +03:00
interface PreviewProps {
docItem: DocItem;
2022-12-18 19:31:42 +03:00
closeComponent?: React.ReactNode;
handleClose?: () => void;
2022-11-26 12:36:08 +03:00
}
export const Preview = (props: PreviewProps) => {
2022-12-18 19:31:42 +03:00
const { docItem, handleClose, closeComponent = undefined } = props;
2023-01-08 18:54:23 +03:00
const { name, description, category, example, fn_type, id } = docItem;
2022-12-12 16:24:16 +03:00
const theme = useTheme();
2022-12-09 18:41:59 +03:00
const prefix = category.split(/([\/.])/gm).at(4) || "builtins";
const libName = category
.match(/(?:[a-zA-Z]*)\.nix/gm)?.[0]
?.replace(".nix", "");
2023-01-23 15:43:48 +03:00
const sanitizedName = name.replace("'", "-prime");
const libDocsRef = `https://nixos.org/manual/nixpkgs/stable/#function-library-lib.${libName}.${sanitizedName}`;
2022-12-12 16:24:16 +03:00
const builtinsDocsRef = `https://nixos.org/manual/nix/stable/language/builtins.html#builtins-${name}`;
2022-11-26 12:36:08 +03:00
return (
2022-12-03 19:38:50 +03:00
<Box
sx={{
2022-12-12 16:24:16 +03:00
p: { xs: 0.5, md: 1 },
2022-12-03 19:38:50 +03:00
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"
2022-12-18 19:31:42 +03:00
sx={{ wordWrap: "normal", lineBreak: "anywhere" }}
2023-01-08 18:54:23 +03:00
>
{`${id}`}
</Typography>
2022-12-18 19:31:42 +03:00
{closeComponent || (
<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>
)}
2022-12-11 23:22:45 +03:00
</Box>
{prefix !== "builtins" && id.includes("lib.") && (
2023-01-08 18:54:23 +03:00
<Box sx={{ my: 1 }}>
<Typography variant="subtitle1">
{`short form: lib.${name}`}
</Typography>
2023-01-08 18:54:23 +03:00
</Box>
)}
2022-12-12 16:24:16 +03:00
<List sx={{ width: "100%" }} disablePadding>
<ListItem sx={{ flexDirection: { xs: "column", sm: "row" }, px: 0 }}>
2022-11-26 12:36:08 +03:00
<ListItemIcon>
2022-12-12 16:24:16 +03:00
<Tooltip title={"read docs"}>
2023-01-14 03:23:08 +03:00
<MuiLink
sx={{ m: "auto", color: "primary.light" }}
target="_blank"
href={!id.includes("builtins") ? libDocsRef : builtinsDocsRef}
>
<LocalLibraryIcon sx={{ m: "auto" }} />
2022-12-12 16:24:16 +03:00
</MuiLink>
</Tooltip>
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-12 16:24:16 +03:00
width: "100%",
px: 0,
}}
2022-12-09 18:41:59 +03:00
primaryTypographyProps={{
color: "text.secondary",
fontSize: 14,
}}
secondaryTypographyProps={{
color: "text.primary",
fontSize: "1rem",
component: "div",
2022-12-09 18:41:59 +03:00
}}
2022-12-12 16:24:16 +03:00
primary={
2023-01-08 18:54:23 +03:00
!id.includes("builtins") ? (
2022-12-12 16:24:16 +03:00
<Tooltip title={"browse source code"}>
2023-01-14 03:23:08 +03:00
<MuiLink
target={"_blank"}
href={`https://github.com/NixOS/nixpkgs/blob/master/${category.replace(
"./",
""
)}`}
>
{"github:NixOS/nixpkgs/" + category.replace("./", "")}
2022-12-12 16:24:16 +03:00
</MuiLink>
</Tooltip>
) : (
"github:NixOS/nix/" + category.replace("./", "")
)
}
2022-12-09 18:41:59 +03:00
secondary={
<Container
2022-12-11 23:22:45 +03:00
component={"div"}
sx={{
ml: "0 !important",
pl: "0 !important",
overflow: "visible",
}}
maxWidth="md"
2022-12-09 18:41:59 +03:00
>
2023-01-08 18:54:23 +03:00
{Array.isArray(description)
2022-12-09 18:41:59 +03:00
? description.map((d, idx) => (
<MarkdownPreview key={idx} description={d} />
2022-12-09 18:41:59 +03:00
))
: description && (
<MarkdownPreview description={description} />
)}
2022-12-09 18:41:59 +03:00
</Container>
}
2022-11-26 12:36:08 +03:00
/>
</ListItem>
2022-12-12 16:24:16 +03:00
<ListItem sx={{ flexDirection: { xs: "column", sm: "row" }, px: 0 }}>
2022-11-26 12:36:08 +03:00
<ListItemIcon>
2022-12-12 16:24:16 +03:00
<Tooltip title={"browse source code"}>
2023-01-14 03:23:08 +03:00
<MuiLink
sx={{ m: "auto", color: "primary.light" }}
target="_blank"
href={`https://github.com/NixOS/nixpkgs/blob/master/${category.replace(
"./",
""
)}`}
>
<InputIcon sx={{ m: "auto" }} />
2022-12-12 16:24:16 +03:00
</MuiLink>
</Tooltip>
2022-11-26 12:36:08 +03:00
</ListItemIcon>
<ListItemText
sx={{
overflow: "hidden",
2022-12-12 16:24:16 +03:00
width: "100%",
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",
gutterBottom: true,
2022-12-09 18:41:59 +03:00
fontSize: 14,
}}
secondaryTypographyProps={{
color: fn_type ? "text.primary" : "text.secondary",
2022-12-09 18:41:59 +03:00
fontSize: "1rem",
}}
secondary={
fn_type ? (
<CodeHighlight
code={fn_type}
theme={theme.palette.mode}
lang={"Haskell"}
/>
) : (
"no type provided yet."
)
}
2022-12-09 18:41:59 +03:00
primary="function signature "
/>
2022-11-26 12:36:08 +03:00
</ListItem>
2022-12-18 19:31:42 +03:00
{example && (
<ListItem
sx={{
backgroundColor: "background.paper",
flexDirection: { xs: "column", sm: "row" },
px: 0,
}}
>
2022-12-12 16:24:16 +03:00
<ListItemIcon>
<CodeIcon sx={{ m: "auto" }} />
</ListItemIcon>
2022-12-19 11:28:29 +03:00
2022-12-12 16:24:16 +03:00
<ListItemText
sx={{
overflow: "hidden",
textOverflow: "ellipsis",
alignSelf: "flex-start",
width: "100%",
px: 0,
}}
disableTypography
primary={
2022-12-08 01:10:58 +03:00
<Typography sx={{ color: "text.secondary" }}>
Example
</Typography>
2022-12-12 16:24:16 +03:00
}
secondary={
<CodeHighlight
code={example}
theme={theme.palette.mode}
background={
theme.palette.mode === "light" ? "action.hover" : undefined
}
/>
2022-12-12 16:24:16 +03:00
}
/>
2022-12-18 19:31:42 +03:00
</ListItem>
)}
2022-11-26 12:36:08 +03:00
</List>
</Box>
);
};