8000 feat: parse resource metadata values as markdown by aslilac · Pull Request #10521 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: parse resource metadata values as markdown #10521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use _.isEqual for memo
  • Loading branch information
aslilac committed Nov 7, 2023
commit 653ff5f100aab4307af6f3c46666d98f93a8a441
14 changes: 11 additions & 3 deletions site/src/components/Markdown/Markdown.tsx
8000
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import { type Interpolation, type Theme } from "@emotion/react";
import isEqual from "lodash/isEqual";
import { type FC, memo } from "react";
import ReactMarkdown, { type Options } from "react-markdown";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
Expand All @@ -20,10 +21,15 @@ interface MarkdownProps {
children: string;

className?: string;

/**
* Can override the behavior of the generated elements
*/
components?: Options["components"];
}

export const Markdown: FC<MarkdownProps> = (props) => {
const { children, className } = props;
const { children, className, components = {} } = props;

return (
<ReactMarkdown
Expand Down Expand Up @@ -106,6 +112,8 @@ export const Markdown: FC<MarkdownProps> = (props) => {
th: ({ children }) => {
return <TableCell>{children}</TableCell>;
},

...components,
}}
>
{children}
Expand Down Expand Up @@ -171,8 +179,8 @@ export const InlineMarkdown: FC<MarkdownInlineProps> = (props) => {
);
};

export const MemoizedMarkdown = memo(Markdown);
export const MemoizedInlineMarkdown = memo(InlineMarkdown);
export const MemoizedMarkdown = memo(Markdown, isEqual);
export const MemoizedInlineMarkdown = memo(InlineMarkdown, isEqual);

const markdownStyles: Interpolation<Theme> = (theme: Theme) => ({
fontSize: 16,
Expand Down
18 changes: 7 additions & 11 deletions site/src/components/Resources/ResourceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,12 @@ export interface ResourceCardProps {
agentRow: (agent: WorkspaceAgent) => JSX.Element;
}

const markdownComponents = {
p: ({ children }: PropsWithChildren) => {
const childrens = Children.toArray(children);
if (childrens.every((child) => typeof child === "string")) {
return (
<CopyableValue value={childrens.join("")}>{children}</CopyableValue>
);
}
return <>{children}</>;
},
const p = ({ children }: PropsWithChildren) => {
const childrens = Children.toArray(children);
if (childrens.every((child) => typeof child === "string")) {
return <CopyableValue value={childrens.join("")}>{children}</CopyableValue>;
}
return <>{children}</>;
};

export const ResourceCard: FC<ResourceCardProps> = ({ resource, agentRow }) => {
Expand Down Expand Up @@ -150,7 +146,7 @@ export const ResourceCard: FC<ResourceCardProps> = ({ resource, agentRow }) => {
{meta.sensitive ? (
<SensitiveValue value={meta.value} />
) : (
<MemoizedInlineMarkdown components={markdownComponents}>
<MemoizedInlineMarkdown components={{ p }}>
{meta.value}
</MemoizedInlineMarkdown>
)}
Expand Down
0