@orsetra/shared-ui 1.10.25 → 1.10.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/extends/CPUNumber/index.tsx +3 -29
- package/components/extends/DiskNumber/index.tsx +3 -30
- package/components/extends/MemoryNumber/index.tsx +3 -30
- package/components/extends/QuantityInput/index.tsx +66 -0
- package/components/extends/index.ts +1 -0
- package/components/layout/sidebar/main-sidebar-flyout.tsx +0 -4
- package/components/layout/sidebar/quick-access-tracker.tsx +12 -4
- package/lib/quantity.ts +50 -0
- package/package.json +1 -1
|
@@ -1,33 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import QuantityInput, { type QuantityInputProps } from '../QuantityInput';
|
|
3
|
+
import { CPU_UNITS } from '../../../lib/quantity';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
value?: any;
|
|
6
|
-
id: string;
|
|
7
|
-
onChange: (value: any) => void;
|
|
8
|
-
disabled: boolean;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const CPUNumber: React.FC<Props> = ({ value, id, onChange, disabled }) => {
|
|
12
|
-
const initValue = value ? parseFloat(value) : undefined;
|
|
13
|
-
|
|
14
|
-
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
15
|
-
onChange(e.target.value);
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
return (
|
|
19
|
-
<div className="flex items-center gap-2">
|
|
20
|
-
<Input
|
|
21
|
-
id={id}
|
|
22
|
-
type="number"
|
|
23
|
-
disabled={disabled}
|
|
24
|
-
onChange={handleChange}
|
|
25
|
-
value={initValue}
|
|
26
|
-
className="flex-1"
|
|
27
|
-
/>
|
|
28
|
-
<span className="text-sm text-ibm-gray-70">Core</span>
|
|
29
|
-
</div>
|
|
30
|
-
);
|
|
31
|
-
};
|
|
5
|
+
const CPUNumber: React.FC<QuantityInputProps> = props => <QuantityInput {...props} units={CPU_UNITS} />;
|
|
32
6
|
|
|
33
7
|
export default CPUNumber;
|
|
@@ -1,34 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import QuantityInput, { type QuantityInputProps } from '../QuantityInput';
|
|
3
|
+
import { DISK_UNITS } from '../../../lib/quantity';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
id: string;
|
|
6
|
-
onChange: (value: any) => void;
|
|
7
|
-
value?: any;
|
|
8
|
-
disabled: boolean;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const DiskNumber: React.FC<Props> = ({ value, id, onChange, disabled }) => {
|
|
12
|
-
const initValue = value ? parseInt(value.replace('Gi', ''), 10) : undefined;
|
|
13
|
-
|
|
14
|
-
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
15
|
-
onChange(e.target.value + 'Gi');
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
return (
|
|
19
|
-
<div className="flex items-center gap-2">
|
|
20
|
-
<Input
|
|
21
|
-
id={id}
|
|
22
|
-
type="number"
|
|
23
|
-
min="0"
|
|
24
|
-
disabled={disabled}
|
|
25
|
-
onChange={handleChange}
|
|
26
|
-
value={initValue}
|
|
27
|
-
className="flex-1"
|
|
28
|
-
/>
|
|
29
|
-
<span className="text-sm text-ibm-gray-70">Gi</span>
|
|
30
|
-
</div>
|
|
31
|
-
);
|
|
32
|
-
};
|
|
5
|
+
const DiskNumber: React.FC<QuantityInputProps> = props => <QuantityInput {...props} units={DISK_UNITS} />;
|
|
33
6
|
|
|
34
7
|
export default DiskNumber;
|
|
@@ -1,34 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import QuantityInput, { type QuantityInputProps } from '../QuantityInput';
|
|
3
|
+
import { MEMORY_UNITS } from '../../../lib/quantity';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
id: string;
|
|
6
|
-
onChange: (value: any) => void;
|
|
7
|
-
value?: any;
|
|
8
|
-
disabled: boolean;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const MemoryNumber: React.FC<Props> = ({ value, id, onChange, disabled }) => {
|
|
12
|
-
const initValue = value ? parseInt(value.replace('Mi', ''), 10) : undefined;
|
|
13
|
-
|
|
14
|
-
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
15
|
-
onChange(e.target.value + 'Mi');
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
return (
|
|
19
|
-
<div className="flex items-center gap-2">
|
|
20
|
-
<Input
|
|
21
|
-
id={id}
|
|
22
|
-
type="number"
|
|
23
|
-
min="0"
|
|
24
|
-
disabled={disabled}
|
|
25
|
-
onChange={handleChange}
|
|
26
|
-
value={initValue}
|
|
27
|
-
className="flex-1"
|
|
28
|
-
/>
|
|
29
|
-
<span className="text-sm text-ibm-gray-70">Mi</span>
|
|
30
|
-
</div>
|
|
31
|
-
);
|
|
32
|
-
};
|
|
5
|
+
const MemoryNumber: React.FC<QuantityInputProps> = props => <QuantityInput {...props} units={MEMORY_UNITS} />;
|
|
33
6
|
|
|
34
7
|
export default MemoryNumber;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import React, { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { Input } from '../../ui/input';
|
|
3
|
+
import { formatQuantity, parseQuantity, unitsFor, type QuantityUnit } from '../../../lib/quantity';
|
|
4
|
+
|
|
5
|
+
export type QuantityInputProps = {
|
|
6
|
+
id: string;
|
|
7
|
+
value?: any;
|
|
8
|
+
onChange: (value: any) => void;
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
className?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type Props = QuantityInputProps & {
|
|
14
|
+
units: QuantityUnit[];
|
|
15
|
+
step?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const QuantityInput: React.FC<Props> = ({ id, value, onChange, disabled, className, units, step = 'any' }) => {
|
|
19
|
+
const parsed = parseQuantity(value, units);
|
|
20
|
+
const [amount, setAmount] = useState(parsed.amount);
|
|
21
|
+
const [suffix, setSuffix] = useState(parsed.unit.suffix);
|
|
22
|
+
const lastEmitted = useRef<any>(value);
|
|
23
|
+
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (value === lastEmitted.current) return;
|
|
26
|
+
lastEmitted.current = value;
|
|
27
|
+
const next = parseQuantity(value, units);
|
|
28
|
+
setAmount(next.amount);
|
|
29
|
+
if (next.amount !== '') setSuffix(next.unit.suffix);
|
|
30
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
31
|
+
}, [value]);
|
|
32
|
+
|
|
33
|
+
const emit = (nextAmount: string, nextSuffix: string) => {
|
|
34
|
+
const quantity = formatQuantity(nextAmount, nextSuffix);
|
|
35
|
+
lastEmitted.current = quantity;
|
|
36
|
+
onChange(quantity);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<div className="flex items-stretch">
|
|
41
|
+
<Input
|
|
42
|
+
id={id}
|
|
43
|
+
type="number"
|
|
44
|
+
min="0"
|
|
45
|
+
step={step}
|
|
46
|
+
disabled={disabled}
|
|
47
|
+
value={amount}
|
|
48
|
+
onChange={event => { setAmount(event.target.value); emit(event.target.value, suffix); }}
|
|
49
|
+
className={`flex-1 rounded-none ${className ?? ''}`}
|
|
50
|
+
/>
|
|
51
|
+
<select
|
|
52
|
+
aria-label="Unit"
|
|
53
|
+
disabled={disabled}
|
|
54
|
+
value={suffix}
|
|
55
|
+
onChange={event => { setSuffix(event.target.value); emit(amount, event.target.value); }}
|
|
56
|
+
className="w-32 -ml-px border border-ibm-gray-30 bg-ibm-gray-10 px-2 text-sm text-ibm-gray-100 rounded-none focus:outline-none focus:border-ibm-blue-60 disabled:opacity-50"
|
|
57
|
+
>
|
|
58
|
+
{unitsFor(value, units).map(unit => (
|
|
59
|
+
<option key={unit.suffix} value={unit.suffix}>{unit.label}</option>
|
|
60
|
+
))}
|
|
61
|
+
</select>
|
|
62
|
+
</div>
|
|
63
|
+
);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export default QuantityInput;
|
|
@@ -15,6 +15,7 @@ export { default as KV } from './KV'
|
|
|
15
15
|
export { default as MemoryNumber } from './MemoryNumber'
|
|
16
16
|
export { default as Numbers } from './Numbers'
|
|
17
17
|
export { default as PVCSelect } from './PVCSelect'
|
|
18
|
+
export { default as QuantityInput } from './QuantityInput'
|
|
18
19
|
export { default as PolicySelect } from './PolicySelect'
|
|
19
20
|
export { default as SecretKeySelect } from './SecretKeySelect'
|
|
20
21
|
export { default as SecretSelect } from './SecretSelect'
|
|
@@ -79,8 +79,6 @@ export function MainSidebarFlyout({
|
|
|
79
79
|
<a
|
|
80
80
|
key={sub.id}
|
|
81
81
|
href={sub.disabled ? undefined : sub.href}
|
|
82
|
-
target={sub.target}
|
|
83
|
-
rel={sub.target === "_blank" ? "noopener noreferrer" : undefined}
|
|
84
82
|
aria-disabled={sub.disabled}
|
|
85
83
|
title={sub.disabled ? "Select a project/environment first" : undefined}
|
|
86
84
|
onClick={(e) => { if (sub.disabled) e.preventDefault() }}
|
|
@@ -151,7 +149,6 @@ export function MainSidebarFlyout({
|
|
|
151
149
|
<a
|
|
152
150
|
key={i}
|
|
153
151
|
href={link.href}
|
|
154
|
-
{...(isExternal ? { target: "_blank", rel: "noopener noreferrer" } : {})}
|
|
155
152
|
className="flex items-start gap-x-3 px-2 py-2 border-l-4 border-transparent hover:bg-ui-background hover:border-interactive transition-colors"
|
|
156
153
|
>
|
|
157
154
|
<LinkIcon className="h-4 w-4 flex-shrink-0 mt-0.5 text-text-secondary" />
|
|
@@ -206,7 +203,6 @@ export function MainSidebarFlyout({
|
|
|
206
203
|
<a
|
|
207
204
|
key={i}
|
|
208
205
|
href={link.href}
|
|
209
|
-
{...(isExternal ? { target: "_blank", rel: "noopener noreferrer" } : {})}
|
|
210
206
|
className="flex items-start gap-x-3 px-2 py-2 border-l-4 border-transparent hover:bg-ui-background hover:border-interactive transition-colors"
|
|
211
207
|
>
|
|
212
208
|
<LinkIcon className="h-4 w-4 flex-shrink-0 mt-0.5 text-text-secondary" />
|
|
@@ -11,6 +11,13 @@ interface QuickAccessTrackerProps {
|
|
|
11
11
|
icon?: string
|
|
12
12
|
/** Set to false to skip tracking this page (e.g. list/index pages) */
|
|
13
13
|
enabled?: boolean
|
|
14
|
+
/**
|
|
15
|
+
* Canonical href for this resource — overrides the current page URL.
|
|
16
|
+
* Pass the resource root (e.g. "/my-app") so that visiting any tab of
|
|
17
|
+
* the same resource tracks a single entry instead of one per tab.
|
|
18
|
+
* Absolute URL or path relative to the current origin.
|
|
19
|
+
*/
|
|
20
|
+
href?: string
|
|
14
21
|
}
|
|
15
22
|
|
|
16
23
|
/**
|
|
@@ -18,7 +25,7 @@ interface QuickAccessTrackerProps {
|
|
|
18
25
|
* sidebar's quick-access list. Tracks on mount and whenever pathname changes.
|
|
19
26
|
* Renders nothing.
|
|
20
27
|
*/
|
|
21
|
-
export function QuickAccessTracker({ label, icon, enabled = true }: QuickAccessTrackerProps) {
|
|
28
|
+
export function QuickAccessTracker({ label, icon, enabled = true, href: hrefProp }: QuickAccessTrackerProps) {
|
|
22
29
|
const pathname = usePathname()
|
|
23
30
|
const { addItem } = useQuickAccess()
|
|
24
31
|
// Stable ref so the effect doesn't re-run when addItem reference changes
|
|
@@ -27,11 +34,12 @@ export function QuickAccessTracker({ label, icon, enabled = true }: QuickAccessT
|
|
|
27
34
|
|
|
28
35
|
useEffect(() => {
|
|
29
36
|
if (!enabled || !label || !pathname) return
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
const href = hrefProp
|
|
38
|
+
? (hrefProp.startsWith("http") ? hrefProp : window.location.origin + hrefProp)
|
|
39
|
+
: window.location.origin + window.location.pathname
|
|
32
40
|
addRef.current({ label, href, icon })
|
|
33
41
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
34
|
-
}, [pathname, label, icon, enabled])
|
|
42
|
+
}, [pathname, label, icon, enabled, hrefProp])
|
|
35
43
|
|
|
36
44
|
return null
|
|
37
45
|
}
|
package/lib/quantity.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export interface QuantityUnit {
|
|
2
|
+
suffix: string
|
|
3
|
+
label: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export const CPU_UNITS: QuantityUnit[] = [
|
|
7
|
+
{ suffix: "m", label: "millicores" },
|
|
8
|
+
{ suffix: "", label: "cores" },
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
export const MEMORY_UNITS: QuantityUnit[] = [
|
|
12
|
+
{ suffix: "Mi", label: "Mi" },
|
|
13
|
+
{ suffix: "Gi", label: "Gi" },
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
export const DISK_UNITS: QuantityUnit[] = [
|
|
17
|
+
{ suffix: "Gi", label: "Gi" },
|
|
18
|
+
{ suffix: "Mi", label: "Mi" },
|
|
19
|
+
{ suffix: "Ti", label: "Ti" },
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
export interface ParsedQuantity {
|
|
23
|
+
amount: string
|
|
24
|
+
unit: QuantityUnit
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const QUANTITY = /^([0-9]*\.?[0-9]+)\s*([A-Za-z]*)$/
|
|
28
|
+
|
|
29
|
+
export function parseQuantity(value: unknown, units: QuantityUnit[]): ParsedQuantity {
|
|
30
|
+
const fallback = units[0]
|
|
31
|
+
if (value === undefined || value === null || value === "") return { amount: "", unit: fallback }
|
|
32
|
+
|
|
33
|
+
const match = QUANTITY.exec(String(value).trim())
|
|
34
|
+
if (!match) return { amount: "", unit: fallback }
|
|
35
|
+
|
|
36
|
+
const [, amount, suffix] = match
|
|
37
|
+
const unit = units.find(candidate => candidate.suffix === suffix) ?? { suffix, label: suffix }
|
|
38
|
+
return { amount, unit }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function formatQuantity(amount: string, suffix: string): string {
|
|
42
|
+
const text = (amount ?? "").trim()
|
|
43
|
+
if (text === "" || Number.isNaN(Number(text))) return ""
|
|
44
|
+
return `${Number(text)}${suffix}`
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function unitsFor(value: unknown, units: QuantityUnit[]): QuantityUnit[] {
|
|
48
|
+
const { unit } = parseQuantity(value, units)
|
|
49
|
+
return units.some(candidate => candidate.suffix === unit.suffix) ? units : [...units, unit]
|
|
50
|
+
}
|