forked from openshift/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.tsx
125 lines (115 loc) · 3.83 KB
/
common.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
import * as React from 'react';
import { Button } from '@patternfly/react-core';
import { EyeIcon } from '@patternfly/react-icons/dist/esm/icons/eye-icon';
import { EyeSlashIcon } from '@patternfly/react-icons/dist/esm/icons/eye-slash-icon';
import * as _ from 'lodash';
import { Trans, useTranslation } from 'react-i18next';
import { SecretValue } from '@console/internal/components/configmap-and-secret-data';
import { DetailsItem, ResourceLink } from '@console/internal/components/utils';
import { YellowExclamationTriangleIcon } from '@console/shared';
import { REGEXP_K8S_RESOURCE_SUFFIX } from './const';
import { CapabilityProps, SpecCapability, StatusCapability } from './types';
export const Invalid: React.FC<{ path: string }> = ({ path }) => {
return (
<span className="text-muted olm-descriptors__invalid-pod-descriptor">
<YellowExclamationTriangleIcon />
<Trans ns="olm">
The field <code className="co-code">{{ path }}</code> is invalid.
</Trans>
</span>
);
};
export const DefaultCapability: React.FC<CommonCapabilityProps<string | number | boolean>> = ({
description,
label,
obj,
fullPath,
value,
}) => {
const { t } = useTranslation();
const detail = React.useMemo(() => {
if (_.isEmpty(value) && !_.isFinite(value) && !_.isBoolean(value)) {
return <span className="text-muted">{t('public~None')}</span>;
}
return _.toString(value);
}, [t, value]);
return (
<DetailsItem description={description} label={label} obj={obj} path={fullPath}>
{detail}
</DetailsItem>
);
};
export const K8sResourceLinkCapability: React.FC<CommonCapabilityProps<string>> = ({
capability,
description,
descriptor,
fullPath,
label,
obj,
value,
}) => {
const { t } = useTranslation();
const detail = React.useMemo(() => {
if (!value) {
return <span className="text-muted">{t('public~None')}</span>;
}
const [, suffix] = capability.match(REGEXP_K8S_RESOURCE_SUFFIX) ?? [];
const gvk = suffix?.replace(/:/g, '~');
if (!_.isString(value)) {
// eslint-disable-next-line no-console
console.warn(
`[Invalid descriptor] descriptor is incompatible with property ${descriptor.path} and will have no effect`,
descriptor,
);
return null;
}
return <ResourceLink kind={gvk} name={value} namespace={obj.metadata.namespace} />;
}, [value, capability, obj.metadata.namespace, t, descriptor]);
return (
<DetailsItem description={description} label={label} obj={obj} path={fullPath}>
{detail}
</DetailsItem>
);
};
export const SecretCapability: React.FC<CommonCapabilityProps<string>> = ({
description,
label,
obj,
fullPath,
value,
}) => {
const { t } = useTranslation();
const [reveal, setReveal] = React.useState(false);
return (
<DetailsItem description={description} label={label} obj={obj} path={fullPath}>
<div className="co-toggle-reveal-value">
<Button
type="button"
variant="link"
isInline
className="pf-m-link--align-right co-toggle-reveal-value__btn"
onClick={() => setReveal(!reveal)}
>
{reveal ? (
<>
<EyeSlashIcon className="co-icon-space-r" />
{t('olm~Hide values')}
</>
) : (
<>
<EyeIcon className="co-icon-space-r" />
{t('olm~Reveal values')}
</>
)}
</Button>
<SecretValue value={value} encoded={false} reveal={reveal} />
</div>
</DetailsItem>
);
};
type CommonCapabilityProps<V = any> = CapabilityProps<SpecCapability | StatusCapability, V>;
Invalid.displayName = 'Invalid';
DefaultCapability.displayName = 'DefaultCapability';
K8sResourceLinkCapability.displayName = 'K8sResourceLinkCapability';
SecretCapability.displayName = 'SecretCapability';