-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathnamespace-param.tsx
174 lines (157 loc) · 3.96 KB
/
namespace-param.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* eslint-disable react/prop-types */
import React, { useMemo } from 'react';
import {
InlineDefinition,
spacing,
css,
cx,
ContentWithFallback,
Placeholder,
keyframes,
SignalPopover,
} from '@mongodb-js/compass-components';
import type { ViewType } from './use-view-type';
import { usePreference } from 'compass-preferences-model/provider';
const namespaceParam = css({
display: 'flex',
gap: '1ch',
flex: 1,
overflow: 'hidden',
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
minWidth: 0,
maxWidth: spacing[1600] * 4,
});
const multiline = css({
display: 'flex',
flexDirection: 'column',
gap: 0,
});
const namespaceParamLabel = css({
fontWeight: 'bold',
});
const namespaceParamValueContainer = css({
position: 'relative',
width: '100%',
// Keeping container height for the placeholder to appear
minHeight: 20,
});
const namespaceParamValueContainerWithInsights = css({
display: 'flex',
alignItems: 'center',
gap: spacing[200],
});
const namespaceParamValue = css({
opacity: 1,
transition: 'opacity .16s linear',
});
const namespaceParamValueRefreshing = css({
opacity: 0.3,
});
const namespaceParamValueMissing = css({
opacity: 0.3,
});
const namespaceParamValuePlaceholder = css({
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0,
display: 'flex',
opacity: 0,
transition: 'opacity .16s ease-out',
});
const visible = css({
opacity: 1,
transitionTimingFunction: 'ease-in',
});
const fadeInAnimation = keyframes({
from: {
opacity: 0,
},
to: {
opacity: 1,
},
});
const fadeIn = css({
animation: `${fadeInAnimation} .16s ease-out`,
});
export const NamespaceParam: React.FunctionComponent<{
label: React.ReactNode;
value: React.ReactNode;
status: 'initial' | 'fetching' | 'refreshing' | 'ready' | 'error';
hint?: React.ReactNode;
viewType: ViewType;
insights?: React.ComponentProps<typeof SignalPopover>['signals'];
}> = ({ label, value, status, hint, viewType, insights }) => {
const showInsights = usePreference('showInsights');
const renderedValue = useMemo(() => {
const isReady = status !== 'initial' && status !== 'fetching';
return (
<ContentWithFallback
isContentReady={isReady}
content={(shouldRender, shouldAnimate) => {
if (!shouldRender) {
return null;
}
// eslint-disable-next-line eqeqeq
const missingValue = value == null || status === 'error';
return (
<span
className={cx(
namespaceParamValue,
missingValue && namespaceParamValueMissing,
status === 'refreshing' && namespaceParamValueRefreshing,
shouldAnimate && fadeIn
)}
>
{missingValue ? '—' : value}
</span>
);
}}
fallback={(shouldRender) => (
<span
data-testid="namespace-param-fallback"
data-ready={isReady}
className={cx(
namespaceParamValuePlaceholder,
shouldRender && visible
)}
>
<Placeholder maxChar={10}></Placeholder>
</span>
)}
></ContentWithFallback>
);
}, [value, status]);
return (
<div className={cx(namespaceParam, viewType === 'list' && multiline)}>
<span className={namespaceParamLabel}>
{hint ? (
<InlineDefinition
tooltipProps={{
align: 'top',
justify: 'start',
}}
definition={hint}
>
{label}:
</InlineDefinition>
) : (
<>{label}:</>
)}
</span>
<span
className={cx(
namespaceParamValueContainer,
insights && namespaceParamValueContainerWithInsights
)}
>
{renderedValue}
{showInsights && insights && (
<SignalPopover signals={insights}></SignalPopover>
)}
</span>
</div>
);
};