-
Notifications
You must be signed in to change notification settings - Fork 488
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
Add links to make values in tags or log properties clickable #223
Changes from 15 commits
33fcae9
83c257e
1330d0c
f305938
7849922
efd44fd
0901e53
0544a62
858bb10
136c149
73db8e9
a159459
dd795d2
9acf208
68c5d05
13d66cc
6048d0a
185c93e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,10 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import React from 'react'; | ||
import * as React from 'react'; | ||
import jsonMarkup from 'json-markup'; | ||
import { Dropdown, Icon, Menu } from 'antd'; | ||
import type { KeyValuePair, Link } from '../../../../types'; | ||
|
||
import './KeyValuesTable.css'; | ||
|
||
|
@@ -30,27 +32,70 @@ function parseIfJson(value) { | |
return value; | ||
} | ||
|
||
const LinkValue = (props: { href: string, title?: string, children: React.Node }) => ( | ||
<a href={props.href} title={props.title} target="_blank" rel="noopener noreferrer"> | ||
{props.children} <Icon className="KeyValueTable--linkIcon" type="export" /> | ||
</a> | ||
); | ||
|
||
const linkValueList = (links: Link[]) => ( | ||
<Menu> | ||
{links.map(({ text, url }, index) => ( | ||
// `index` is necessary in the key because url can repeat | ||
// eslint-disable-next-line react/no-array-index-key | ||
<Menu.Item key={`${url}-${index}`}> | ||
<LinkValue href={url}>{text}</LinkValue> | ||
</Menu.Item> | ||
))} | ||
</Menu> | ||
); | ||
|
||
type KeyValuesTableProps = { | ||
data: { key: string, value: any }[], | ||
data: KeyValuePair[], | ||
linksGetter: ?(KeyValuePair[], number) => Link[], | ||
}; | ||
|
||
export default function KeyValuesTable(props: KeyValuesTableProps) { | ||
const { data } = props; | ||
const { data, linksGetter } = props; | ||
return ( | ||
<div className="KeyValueTable u-simple-scrollbars"> | ||
<table className="u-width-100"> | ||
<tbody className="KeyValueTable--body"> | ||
{data.map((row, i) => { | ||
const jsonTable = ( | ||
// eslint-disable-next-line react/no-danger | ||
<div dangerouslySetInnerHTML={{ __html: jsonMarkup(parseIfJson(row.value)) }} /> | ||
); | ||
const markup = { | ||
__html: jsonMarkup(parseIfJson(row.value)), | ||
}; | ||
// eslint-disable-next-line react/no-danger | ||
const jsonTable = <div className="ub-inline-block" dangerouslySetInnerHTML={markup} />; | ||
const links = linksGetter ? linksGetter(data, i) : null; | ||
let valueMarkup; | ||
if (links && links.length === 1) { | ||
valueMarkup = ( | ||
<div> | ||
<LinkValue href={links[0].url} title={links[0].text}> | ||
{jsonTable} | ||
</LinkValue> | ||
</div> | ||
); | ||
} else if (links && links.length > 1) { | ||
valueMarkup = ( | ||
<div> | ||
<Dropdown overlay={linkValueList(links)} placement="bottomRight" trigger={['click']}> | ||
<a> | ||
{jsonTable} <Icon className="KeyValueTable--linkIcon" type="profile" /> | ||
</a> | ||
</Dropdown> | ||
</div> | ||
); | ||
} else { | ||
valueMarkup = jsonTable; | ||
} | ||
return ( | ||
// `i` is necessary in the key because row.key can repeat | ||
// eslint-disable-next-line react/no-array-index-key | ||
<tr key={`${row.key}-${i}`}> | ||
<td className="KeyValueTable--keyColumn">{row.key}</td> | ||
<td>{jsonTable}</td> | ||
<td>{valueMarkup}</td> | ||
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. Seems like the code above is sufficiently intricate to warrant being broken down into sub-components at the top of the file, similar to // Individual link
<LinkValue
href={href}
title={title}
content={jsonMarkup}
/>
// List of links
<LinkValueList
links={links}
content={jsonMarkup}
/> What do you think? Also, I think it would make sense for the link to always show the icon? 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. I have taken into account this suggestion and updated the 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. Looks great, thank you. |
||
</tr> | ||
); | ||
})} | ||
|
@@ -59,3 +104,5 @@ export default function KeyValuesTable(props: KeyValuesTableProps) { | |
</div> | ||
); | ||
} | ||
|
||
KeyValuesTable.LinkValue = LinkValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file looks great! Awesome.
(Not sure how to add a comment to a file instead of a line...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your encouraging feedback!