-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathField.tsx
More file actions
159 lines (142 loc) · 4.35 KB
/
Field.tsx
File metadata and controls
159 lines (142 loc) · 4.35 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as React from "react"
import {styled} from '@mui/material/styles';
import IconLink from "@mui/icons-material/Link"
import Typography from "@mui/material/Typography"
import InlineCode from "@/components/InlineCode"
import {CopyIconButton} from "@/components/CopyButton"
import {Dimension, Metric} from "./useDimensionsAndMetrics"
import {QueryParam} from "."
import {AccountSummary, PropertySummary} from "@/types/ga4/StreamPicker"
import LabeledCheckbox from "@/components/LabeledCheckbox"
import {CompatibleHook} from "./useCompatibility"
import Markdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
const PREFIX = 'Field';
const classes = {
headingUIName: `${PREFIX}-headingUIName`,
heading: `${PREFIX}-heading`,
apiName: `${PREFIX}-apiName`
};
const Root = styled('div')((
{
theme
}
) => ({
[`& .${classes.headingUIName}`]: {
marginRight: theme.spacing(1),
"& > span": {
fontSize: "inherit",
},
},
[`& .${classes.heading}`]: {
display: "flex",
flexWrap: "wrap",
alignItems: "center",
marginBottom: theme.spacing(1),
"&> button": {
display: "none",
},
"&:hover": {
"&> button": {
display: "unset",
padding: "unset",
},
},
},
[`& .${classes.apiName}`]: {
margin: theme.spacing(0, 1),
fontSize: "0.75em",
}
}));
interface FieldProps extends CompatibleHook {
field:
| { type: "dimension"; value: Dimension }
| { type: "metric"; value: Metric }
account: AccountSummary | undefined
property: PropertySummary | undefined
}
const Field: React.FC<FieldProps> = props => {
const {
field,
account,
property,
incompatibleDimensions,
incompatibleMetrics,
dimensions,
metrics,
addMetric,
addDimension,
removeMetric,
removeDimension,
} = props
const apiName = field.value.apiName || ""
const uiName = field.value.uiName || ""
const description = field.value.description || ""
const link = React.useMemo(() => {
let baseURL = `${window.location.origin}${window.location.pathname}`
let search = ``
if (field.value.customDefinition && account && property) {
let urlParams = new URLSearchParams()
urlParams.append(QueryParam.Account, account.name!)
urlParams.append(QueryParam.Property, property.property!)
search = `?${urlParams.toString()}`
}
return `${baseURL}${search}#${apiName}`
}, [field, apiName, account, property])
const isCompatible = React.useMemo(() => {
return (
incompatibleDimensions?.find(d => d.apiName === field.value.apiName) ===
undefined &&
incompatibleMetrics?.find(m => m.apiName === field.value.apiName) ===
undefined
)
}, [incompatibleMetrics, incompatibleDimensions, field.value.apiName])
const checked = React.useMemo(
() =>
dimensions?.find(d => d.apiName === field.value.apiName) !== undefined ||
metrics?.find(m => m.apiName === field.value.apiName) !== undefined,
[dimensions, metrics, field.value.apiName]
)
const onChange = React.useCallback(() => {
if (checked) {
field.type === "metric"
? removeMetric(field.value)
: removeDimension(field.value)
} else {
field.type === "metric"
? addMetric(field.value)
: addDimension(field.value)
}
}, [checked, addDimension, addMetric, removeDimension, removeMetric, field])
return (
<>
{
<Root id={apiName} key={apiName}>
<Typography variant="h4" className={classes.heading}>
{property === undefined ? (
uiName
) : (
<LabeledCheckbox
className={classes.headingUIName}
checked={checked}
onChange={onChange}
disabled={!isCompatible}
>
{uiName}
</LabeledCheckbox>
)}
<InlineCode className={classes.apiName}>{apiName}</InlineCode>
<CopyIconButton
icon={<IconLink color="primary"/>}
toCopy={link}
tooltipText={`Copy link to ${apiName}`}
/>
</Typography>
<Typography><Markdown
remarkPlugins={[remarkGfm]}>{description}</Markdown></Typography>
</Root>
}
</>
);
}
export default Field