Skip to content
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

fix(frontend): Better CopyToClipboardInline typing #18723

Merged
merged 7 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 11 additions & 8 deletions frontend/src/lib/components/CopyToClipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { IconCopy } from 'lib/lemon-ui/icons'
import { LemonButton } from 'lib/lemon-ui/LemonButton'

interface InlineProps extends HTMLProps<HTMLSpanElement> {
children?: JSX.Element | string
explicitValue?: string
interface InlinePropsBase extends HTMLProps<HTMLSpanElement> {
description?: string
/** Makes text selectable instead of copying on click anywhere */
selectable?: boolean
Expand All @@ -16,6 +14,15 @@ interface InlineProps extends HTMLProps<HTMLSpanElement> {
iconPosition?: 'end' | 'start'
style?: React.CSSProperties
}
interface InlinePropsWithStringInside extends InlinePropsBase {
children: string
explicitValue?: string
}
interface InlinePropsWithJSXInside extends InlinePropsBase {
children?: JSX.Element
explicitValue: string
}
type InlineProps = InlinePropsWithStringInside | InlinePropsWithJSXInside

export function CopyToClipboardInline({
children,
Expand All @@ -29,10 +36,6 @@ export function CopyToClipboardInline({
style,
...props
}: InlineProps): JSX.Element {
if (typeof children !== 'string' && !explicitValue) {
throw new Error('CopyToClipboardInline must have a string child or explicitValue prop')
}

const copy = async (): Promise<boolean> => await copyToClipboard((explicitValue ?? children) as string, description)

const content = (
Expand All @@ -54,7 +57,7 @@ export function CopyToClipboardInline({
onClick={!selectable ? copy : undefined}
{...props}
>
<span className={iconPosition === 'start' ? 'grow-1' : undefined}>{children}</span>
{children && <span className={iconPosition === 'start' ? 'grow-1' : undefined}>{children}</span>}
<LemonButton
size="small"
icon={<IconCopy style={{ ...iconStyle }} />}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Meta, StoryFn } from '@storybook/react'
import { PropertiesTable as PropertiesTableComponent } from '.'
import { PropertyDefinitionType } from '~/types'

const meta: Meta<typeof PropertiesTableComponent> = {
title: 'Components/Properties Table',
component: PropertiesTableComponent,
}
export default meta

export const PropertiesTable: StoryFn = () => {
const properties = {
name: 'John Doe',
age: 30,
url: 'https://www.google.com',
is_good: true,
evil_level: null,
tags: ['best', 'cool', 'awesome'],
location: {
city: 'Prague',
country: 'Czechia',
},
}
return <PropertiesTableComponent type={PropertyDefinitionType.Event} properties={properties} />
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,10 @@ export function PropertiesTable({
title: '',
width: 0,
render: function Copy(_, item: any): JSX.Element | false {
if (Array.isArray(item[1]) || item[1] instanceof Object || item[1] === null) {
return false
}
return (
<CopyToClipboardInline
description="property value"
explicitValue={item[1]}
explicitValue={typeof item[1] === 'object' ? JSON.stringify(item[1]) : String(item[1])}
selectable
isValueSensitive
style={{ verticalAlign: 'middle' }}
Expand Down
14 changes: 8 additions & 6 deletions frontend/src/scenes/experiments/Experiment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,14 @@ export function Experiment(): JSX.Element {
title={`${experiment?.name}`}
buttons={
<>
<CopyToClipboardInline
explicitValue={experiment.feature_flag?.key}
iconStyle={{ color: 'var(--muted-alt)' }}
>
<span className="text-muted">{experiment.feature_flag?.key}</span>
</CopyToClipboardInline>
{experiment.feature_flag && (
<CopyToClipboardInline
explicitValue={experiment.feature_flag.key}
iconStyle={{ color: 'var(--muted-alt)' }}
>
<span className="text-muted">{experiment.feature_flag.key}</span>
</CopyToClipboardInline>
)}
<StatusTag experiment={experiment} />
<ResultsTag />
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function ConfigureSAMLModal(): JSX.Element {
<CopyToClipboardInline>{`${siteUrl}/complete/saml/`}</CopyToClipboardInline>
</Field>
<Field label="RelayState" name="_RelayState">
<CopyToClipboardInline>{configureSAMLModalId ?? undefined}</CopyToClipboardInline>
<CopyToClipboardInline>{configureSAMLModalId || 'unknown'}</CopyToClipboardInline>
</Field>
<Field label="Audience / Entity ID" name="_Audience">
<CopyToClipboardInline>{siteUrl}</CopyToClipboardInline>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ export function VerifyDomainModal(): JSX.Element {
<div className="border rounded p-2 h-10 flex-1">
{domainBeingVerified?.verification_challenge}
</div>
<CopyToClipboardInline
explicitValue={domainBeingVerified?.verification_challenge}
/>
{domainBeingVerified && (
<CopyToClipboardInline
explicitValue={domainBeingVerified.verification_challenge}
/>
)}
</div>
</PureField>
<PureField label="TTL">
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/scenes/settings/user/PersonalAPIKeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ function PersonalAPIKeysTable(): JSX.Element {
dataIndex: 'value',
render: function RenderValue(value) {
return value ? (
<CopyToClipboardInline description="personal API key value">{`${value}`}</CopyToClipboardInline>
<CopyToClipboardInline description="personal API key value">
{String(value)}
</CopyToClipboardInline>
) : (
<i>secret</i>
)
Expand Down
Loading