- Notifications
You must be signed in to change notification settings - Fork928
chore(site): minor refactor to the resource metadata code#11746
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { MockWorkspaceResource } from "testHelpers/entities"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { ResourceMetadata } from "./ResourceMetadata"; | ||
const meta: Meta<typeof ResourceMetadata> = { | ||
title: "pages/WorkspacePage/ResourceMetadata", | ||
component: ResourceMetadata, | ||
}; | ||
export default meta; | ||
type Story = StoryObj<typeof ResourceMetadata>; | ||
export const Markdown: Story = { | ||
args: { | ||
resource: { | ||
...MockWorkspaceResource, | ||
metadata: [ | ||
{ key: "text", value: "hello", sensitive: false }, | ||
{ key: "link", value: "[hello](#)", sensitive: false }, | ||
{ key: "b/i", value: "_hello_, **friend**!", sensitive: false }, | ||
{ key: "coder", value: "`beep boop`", sensitive: false }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Should we have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yes, we should. I missed this comment so, I'm going to add it in a next PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. | ||
], | ||
}, | ||
}, | ||
}; | ||
export const WithLongStrings: Story = { | ||
args: { | ||
resource: { | ||
...MockWorkspaceResource, | ||
metadata: [ | ||
{ | ||
key: "xxxxxxxxxxxx", | ||
value: "14", | ||
sensitive: false, | ||
}, | ||
{ | ||
key: "Long", | ||
value: "The quick brown fox jumped over the lazy dog", | ||
sensitive: false, | ||
}, | ||
{ | ||
key: "Really long", | ||
value: | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", | ||
sensitive: false, | ||
}, | ||
], | ||
}, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { MemoizedInlineMarkdown } from "components/Markdown/Markdown"; | ||
import { SensitiveValue } from "components/Resources/SensitiveValue"; | ||
import { CopyableValue } from "components/CopyableValue/CopyableValue"; | ||
import { WorkspaceResource } from "api/typesGenerated"; | ||
import { Children, FC, HTMLAttributes, PropsWithChildren } from "react"; | ||
import { Interpolation, Theme } from "@emotion/react"; | ||
type ResourceMetadataProps = Omit<HTMLAttributes<HTMLElement>, "resource"> & { | ||
resource: WorkspaceResource; | ||
}; | ||
export const ResourceMetadata: FC<ResourceMetadataProps> = ({ | ||
resource, | ||
...headerProps | ||
}) => { | ||
const metadata = resource.metadata ? [...resource.metadata] : []; | ||
if (resource.daily_cost > 0) { | ||
metadata.push({ | ||
key: "Daily cost", | ||
value: resource.daily_cost.toString(), | ||
sensitive: false, | ||
}); | ||
} | ||
if (metadata.length === 0) { | ||
return null; | ||
} | ||
return ( | ||
<header css={styles.root} {...headerProps}> | ||
{metadata.map((meta) => { | ||
return ( | ||
<div css={styles.item} key={meta.key}> | ||
<div css={styles.value}> | ||
{meta.sensitive ? ( | ||
<SensitiveValue value={meta.value} /> | ||
) : ( | ||
<MemoizedInlineMarkdown components={{ p: MetaValue }}> | ||
{meta.value} | ||
</MemoizedInlineMarkdown> | ||
)} | ||
</div> | ||
<div css={styles.label}>{meta.key}</div> | ||
</div> | ||
); | ||
})} | ||
</header> | ||
); | ||
}; | ||
const MetaValue = ({ children }: PropsWithChildren) => { | ||
const childrenArray = Children.toArray(children); | ||
if (childrenArray.every((child) => typeof child === "string")) { | ||
return ( | ||
<CopyableValue value={childrenArray.join("")}>{children}</CopyableValue> | ||
); | ||
} | ||
return <>{children}</>; | ||
}; | ||
const styles = { | ||
root: (theme) => ({ | ||
padding: 24, | ||
display: "flex", | ||
flexWrap: "wrap", | ||
gap: 48, | ||
rowGap: 24, | ||
marginBottom: 24, | ||
fontSize: 14, | ||
background: `linear-gradient(180deg, ${theme.palette.background.default} 0%, rgba(0, 0, 0, 0) 100%)`, | ||
}), | ||
item: () => ({ | ||
lineHeight: "1.5", | ||
}), | ||
label: (theme) => ({ | ||
fontSize: 13, | ||
color: theme.palette.text.secondary, | ||
textOverflow: "ellipsis", | ||
overflow: "hidden", | ||
whiteSpace: "nowrap", | ||
}), | ||
value: () => ({ | ||
textOverflow: "ellipsis", | ||
overflow: "hidden", | ||
whiteSpace: "nowrap", | ||
}), | ||
} satisfies Record<string, Interpolation<Theme>>; |