-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathcontextCard.tsx
118 lines (107 loc) · 3.05 KB
/
contextCard.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import styled from '@emotion/styled';
import startCase from 'lodash/startCase';
import type {ContextValue} from 'sentry/components/events/contexts';
import {
getContextIcon,
getContextMeta,
getContextTitle,
getFormattedContextData,
} from 'sentry/components/events/contexts/utils';
import * as KeyValueData from 'sentry/components/keyValueData/card';
import type {Event, Group, KeyValueListDataItem, Project} from 'sentry/types';
import {isEmptyObject} from 'sentry/utils/object/isEmptyObject';
import useOrganization from 'sentry/utils/useOrganization';
interface ContextCardProps {
alias: string;
event: Event;
type: string;
group?: Group;
project?: Project;
value?: ContextValue;
}
interface ContextCardContentConfig {
// Omit error styling from being displayed, even if context is invalid
disableErrors?: boolean;
// Displays value as plain text, rather than a hyperlink if applicable
disableLink?: boolean;
// Includes the Context Type as a prefix to the key. Useful if displaying a single Context key
// apart from the rest of that Context. E.g. 'Email' -> 'User: Email'
includeAliasInSubject?: boolean;
}
export interface ContextCardContentProps {
item: KeyValueListDataItem;
meta: Record<string, any>;
alias?: string;
config?: ContextCardContentConfig;
}
export function ContextCardContent({
item,
alias,
meta,
config,
...props
}: ContextCardContentProps) {
const {key: contextKey, subject} = item;
if (contextKey === 'type') {
return null;
}
const contextMeta = meta?.[contextKey];
const contextErrors = contextMeta?.['']?.err ?? [];
const contextSubject =
config?.includeAliasInSubject && alias ? `${startCase(alias)}: ${subject}` : subject;
return (
<KeyValueData.Content
item={{...item, subject: contextSubject}}
meta={contextMeta}
errors={config?.disableErrors ? [] : contextErrors}
disableLink={config?.disableLink ?? false}
{...props}
/>
);
}
export default function ContextCard({
alias,
event,
type,
project,
value = {},
}: ContextCardProps) {
const organization = useOrganization();
if (isEmptyObject(value)) {
return null;
}
const meta = getContextMeta(event, type === 'default' ? alias : type);
const contextItems = getFormattedContextData({
event,
contextValue: value,
contextType: type,
organization,
project,
});
const contentItems = contextItems.map<KeyValueData.ContentProps>(item => {
const itemMeta: KeyValueData.ContentProps['meta'] = meta?.[item?.key];
const itemErrors: KeyValueData.ContentProps['errors'] = itemMeta?.['']?.err ?? [];
return {
item,
meta: itemMeta,
errors: itemErrors,
};
});
return (
<KeyValueData.Card
contentItems={contentItems}
title={
<Title>
<div>{getContextTitle({alias, type, value})}</div>
<div>{getContextIcon({alias, type, value})}</div>
</Title>
}
sortAlphabetically
/>
);
}
const Title = styled('div')`
display: flex;
justify-content: space-between;
align-items: center;
`;