-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddPlugShowcase.tsx
More file actions
166 lines (148 loc) · 5.07 KB
/
Copy pathAddPlugShowcase.tsx
File metadata and controls
166 lines (148 loc) · 5.07 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
160
161
162
163
164
165
166
import { useState } from 'react';
import { useMPTContext, useMPTModal } from '@mpt-extension/sdk-react';
import { Button } from '@softwareone-platform/sdk-react-ui-v0/button';
import { Card } from '@softwareone-platform/sdk-react-ui-v0/card';
import { Divider } from '@softwareone-platform/sdk-react-ui-v0/divider';
import { Input } from '@softwareone-platform/sdk-react-ui-v0/input';
import { Switcher } from '@softwareone-platform/sdk-react-ui-v0/switcher';
import './AddPlugShowcase.scss';
type Format = 'yaml' | 'json';
const formatOptions = [
{ value: 'yaml', label: 'YAML' },
{ value: 'json', label: 'JSON' },
];
interface PlugConfig {
id: string;
name: string;
description: string;
href: string;
condition: string;
}
function toYaml(plug: PlugConfig, socket: string): string {
const lines = [`- id: ${plug.id}`, ` socket: ${socket}`];
if (plug.name) lines.push(` name: ${plug.name}`);
if (plug.description) lines.push(` description: ${plug.description}`);
if (plug.href) lines.push(` href: ${plug.href}`);
if (plug.condition) lines.push(` condition: "${plug.condition}"`);
return lines.join('\n');
}
function toJson(plug: PlugConfig, socket: string): string {
const obj: Record<string, string> = { id: plug.id, socket };
if (plug.name) obj.name = plug.name;
if (plug.description) obj.description = plug.description;
if (plug.href) obj.href = plug.href;
if (plug.condition) obj.condition = plug.condition;
return JSON.stringify(obj, null, 2);
}
/**
* SDK UI showcase: a "plug here" form whose target socket is supplied by the
* per-socket module that mounts it. Each socket has its own thin module entry
* (src/modules/add-*), so no build-time socket injection or shared socket
* manifest is needed — the socket travels as an ordinary prop.
*/
export function AddPlugShowcase({ socket }: { socket: string }) {
const { close } = useMPTModal();
const context = useMPTContext();
const [format, setFormat] = useState<Format>('yaml');
const [plug, setPlug] = useState<PlugConfig>({
id: '',
name: '',
description: '',
href: '/static/',
condition: '',
});
const update =
(field: keyof PlugConfig) => (e: React.ChangeEvent<unknown>) =>
setPlug((p) => ({ ...p, [field]: (e.target as HTMLInputElement).value }));
const output = format === 'yaml' ? toYaml(plug, socket) : toJson(plug, socket);
const body = (
<>
<p>
Fill in the fields below to generate plug configuration. In this template plugs are declared
in Python via a <code>PlugRouter</code> — but the same fields (<code>id</code>,{' '}
<code>name</code>, <code>description</code>, <code>socket</code>, <code>href</code>,{' '}
<code>condition</code>) map one-to-one, so you can use this as a scratchpad. Then build a JS
bundle at the <code>href</code> path you specified.
</p>
<div className="add-plug__form">
<Input
name="id"
label="Plug ID"
placeholder="my-plug"
value={plug.id}
onChange={update('id')}
description="Unique identifier (required)"
/>
<Input
name="name"
label="Name"
placeholder="My Plug"
value={plug.name}
onChange={update('name')}
/>
<Input
name="description"
label="Description"
placeholder="Short description"
value={plug.description}
onChange={update('description')}
/>
<Input
name="href"
label="Bundle href"
placeholder="/static/my-plug/index.js"
value={plug.href}
onChange={update('href')}
description="Relative path to the JS bundle"
/>
<Input
name="condition"
label="Condition (RQL)"
placeholder="eq(product.id,PRD-1234-5678)"
value={plug.condition}
onChange={update('condition')}
description="Optional RQL expression to control visibility"
/>
<details className="add-plug__context">
<summary>Current context</summary>
<pre>
<code>{JSON.stringify(context, null, 2)}</code>
</pre>
</details>
</div>
<Divider />
<Switcher
name="format"
label="Output format"
options={formatOptions}
value={format}
onChange={(e) => setFormat(e.target.value as Format)}
/>
<div className="add-plug__output">
<pre>
<code>{output}</code>
</pre>
</div>
</>
);
if (socket.endsWith('.actions')) {
return (
<div className="dialog">
<div className="dialog__header">
<div className="dialog__title">Add a Plug to {socket}</div>
</div>
<div className="dialog__content">{body}</div>
<div className="dialog__actions">
<div />
<Button type="secondary" onClick={() => close()}>
Cancel
</Button>
</div>
</div>
);
}
if (socket === 'portal') {
return <Card>{body}</Card>;
}
return <div className="add-plug">{body}</div>;
}