|
| 1 | +import React, { useMemo, useState } from 'react'; |
| 2 | +import { NoSSR } from '../NoSSR'; |
| 3 | +import type { ShareScopeAlignmentItem } from './computeInitContainerShareScopeAlignment'; |
| 4 | +import { computeInitContainerShareScopeAlignment } from './computeInitContainerShareScopeAlignment'; |
| 5 | + |
| 6 | +type InputKind = 'unset' | 'string' | 'array'; |
| 7 | + |
| 8 | +function parseStringArray(raw: string): string[] { |
| 9 | + if (!raw.trim()) return []; |
| 10 | + return raw |
| 11 | + .split(',') |
| 12 | + .map((s) => s.trim()) |
| 13 | + .filter(Boolean); |
| 14 | +} |
| 15 | + |
| 16 | +function toShareScopeKey( |
| 17 | + kind: InputKind, |
| 18 | + raw: string, |
| 19 | +): string | string[] | undefined { |
| 20 | + if (kind === 'unset') return undefined; |
| 21 | + if (kind === 'string') { |
| 22 | + const v = raw.trim(); |
| 23 | + return v ? v : undefined; |
| 24 | + } |
| 25 | + return parseStringArray(raw); |
| 26 | +} |
| 27 | + |
| 28 | +export default function ShareScopeAlignmentPlayground() { |
| 29 | + const [hostKind, setHostKind] = useState<InputKind>('array'); |
| 30 | + const [hostRaw, setHostRaw] = useState('default,scope1'); |
| 31 | + |
| 32 | + const [remoteKind, setRemoteKind] = useState<InputKind>('string'); |
| 33 | + const [remoteRaw, setRemoteRaw] = useState('default'); |
| 34 | + |
| 35 | + const hostShareScopeKeys = useMemo( |
| 36 | + () => toShareScopeKey(hostKind, hostRaw), |
| 37 | + [hostKind, hostRaw], |
| 38 | + ); |
| 39 | + const remoteShareScopeKey = useMemo( |
| 40 | + () => toShareScopeKey(remoteKind, remoteRaw), |
| 41 | + [remoteKind, remoteRaw], |
| 42 | + ); |
| 43 | + |
| 44 | + const result = useMemo(() => { |
| 45 | + return computeInitContainerShareScopeAlignment({ |
| 46 | + hostShareScopeKeys, |
| 47 | + remoteShareScopeKey, |
| 48 | + }); |
| 49 | + }, [hostShareScopeKeys, remoteShareScopeKey]); |
| 50 | + |
| 51 | + const inputStyle: React.CSSProperties = { |
| 52 | + width: '100%', |
| 53 | + maxWidth: 520, |
| 54 | + padding: '8px 10px', |
| 55 | + border: '1px solid var(--rp-c-divider, #e5e7eb)', |
| 56 | + borderRadius: 8, |
| 57 | + background: 'var(--rp-c-bg, #fff)', |
| 58 | + color: 'var(--rp-c-text-1, #111827)', |
| 59 | + }; |
| 60 | + |
| 61 | + const selectStyle: React.CSSProperties = { |
| 62 | + ...inputStyle, |
| 63 | + maxWidth: 200, |
| 64 | + }; |
| 65 | + |
| 66 | + const cardStyle: React.CSSProperties = { |
| 67 | + border: '1px solid var(--rp-c-divider, #e5e7eb)', |
| 68 | + borderRadius: 12, |
| 69 | + padding: 14, |
| 70 | + background: 'var(--rp-c-bg, #fff)', |
| 71 | + margin: '12px 0', |
| 72 | + }; |
| 73 | + |
| 74 | + const labelStyle: React.CSSProperties = { |
| 75 | + fontSize: 13, |
| 76 | + color: 'var(--rp-c-text-2, #374151)', |
| 77 | + marginBottom: 6, |
| 78 | + }; |
| 79 | + |
| 80 | + const rowStyle: React.CSSProperties = { |
| 81 | + display: 'flex', |
| 82 | + gap: 12, |
| 83 | + flexWrap: 'wrap', |
| 84 | + alignItems: 'center', |
| 85 | + }; |
| 86 | + |
| 87 | + const blockTitleStyle: React.CSSProperties = { |
| 88 | + fontSize: 13, |
| 89 | + fontWeight: 600, |
| 90 | + color: 'var(--rp-c-text-1, #111827)', |
| 91 | + margin: '14px 0 8px', |
| 92 | + }; |
| 93 | + |
| 94 | + return ( |
| 95 | + <NoSSR> |
| 96 | + <div style={cardStyle}> |
| 97 | + <div style={rowStyle}> |
| 98 | + <div style={{ flex: '1 1 320px' }}> |
| 99 | + <div style={labelStyle}> |
| 100 | + HostShareScope (remotes[remote].shareScope) |
| 101 | + </div> |
| 102 | + <div style={rowStyle}> |
| 103 | + <select |
| 104 | + value={hostKind} |
| 105 | + onChange={(e) => setHostKind(e.target.value as InputKind)} |
| 106 | + style={selectStyle} |
| 107 | + > |
| 108 | + <option value="unset">unset</option> |
| 109 | + <option value="string">string</option> |
| 110 | + <option value="array">array</option> |
| 111 | + </select> |
| 112 | + <input |
| 113 | + value={hostRaw} |
| 114 | + onChange={(e) => setHostRaw(e.target.value)} |
| 115 | + placeholder="default,scope1" |
| 116 | + style={inputStyle} |
| 117 | + disabled={hostKind === 'unset'} |
| 118 | + /> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + |
| 122 | + <div style={{ flex: '1 1 320px' }}> |
| 123 | + <div style={labelStyle}>RemoteShareScope (shareScope)</div> |
| 124 | + <div style={rowStyle}> |
| 125 | + <select |
| 126 | + value={remoteKind} |
| 127 | + onChange={(e) => setRemoteKind(e.target.value as InputKind)} |
| 128 | + style={selectStyle} |
| 129 | + > |
| 130 | + <option value="unset">unset</option> |
| 131 | + <option value="string">string</option> |
| 132 | + <option value="array">array</option> |
| 133 | + </select> |
| 134 | + <input |
| 135 | + value={remoteRaw} |
| 136 | + onChange={(e) => setRemoteRaw(e.target.value)} |
| 137 | + placeholder="default" |
| 138 | + style={inputStyle} |
| 139 | + disabled={remoteKind === 'unset'} |
| 140 | + /> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + |
| 145 | + {result.warnings.length > 0 ? ( |
| 146 | + <div style={{ marginTop: 12 }}> |
| 147 | + <div style={blockTitleStyle}>Warnings</div> |
| 148 | + <ul style={{ margin: 0, paddingLeft: 18 }}> |
| 149 | + {result.warnings.map((w: string) => ( |
| 150 | + <li key={w} style={{ color: 'var(--rp-c-text-2, #374151)' }}> |
| 151 | + {w} |
| 152 | + </li> |
| 153 | + ))} |
| 154 | + </ul> |
| 155 | + </div> |
| 156 | + ) : null} |
| 157 | + |
| 158 | + <div style={blockTitleStyle}>Alignment (pseudo code)</div> |
| 159 | + <pre |
| 160 | + style={{ |
| 161 | + margin: 0, |
| 162 | + padding: 12, |
| 163 | + borderRadius: 10, |
| 164 | + border: '1px solid var(--rp-c-divider, #e5e7eb)', |
| 165 | + background: 'var(--rp-code-bg, rgba(0,0,0,0.03))', |
| 166 | + overflowX: 'auto', |
| 167 | + }} |
| 168 | + > |
| 169 | + {result.alignment.length > 0 |
| 170 | + ? result.alignment |
| 171 | + .map((a: ShareScopeAlignmentItem) => { |
| 172 | + if ('note' in a) return `${a.expression}\n${a.note}`; |
| 173 | + return a.expression; |
| 174 | + }) |
| 175 | + .join('\n') |
| 176 | + : '(no alignment)'} |
| 177 | + </pre> |
| 178 | + |
| 179 | + <div style={blockTitleStyle}>Initialized Scopes</div> |
| 180 | + <pre |
| 181 | + style={{ |
| 182 | + margin: 0, |
| 183 | + padding: 12, |
| 184 | + borderRadius: 10, |
| 185 | + border: '1px solid var(--rp-c-divider, #e5e7eb)', |
| 186 | + background: 'var(--rp-code-bg, rgba(0,0,0,0.03))', |
| 187 | + overflowX: 'auto', |
| 188 | + }} |
| 189 | + > |
| 190 | + {JSON.stringify(result.initializedScopes, null, 2)} |
| 191 | + </pre> |
| 192 | + </div> |
| 193 | + </NoSSR> |
| 194 | + ); |
| 195 | +} |
0 commit comments