-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathtarget-env.tsx
More file actions
109 lines (103 loc) · 3.12 KB
/
Copy pathtarget-env.tsx
File metadata and controls
109 lines (103 loc) · 3.12 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
import { GlobeIcon } from 'lucide-react';
import { Editor } from '../components/laboratory/editor';
import { LaboratoryPlugin } from '../lib/plugins';
export const TargetEnvPlugin = (props: {
organizationSlug: string;
projectSlug: string;
targetSlug: string;
}) => {
const targetId = `${props.organizationSlug}/${props.projectSlug}/${props.targetSlug}`;
return {
id: 'targetEnv',
name: 'Target Environment',
description: 'Environment variables for the target',
preflight: {
lab: {
definition: `
targetEnvironment: {
set: (key: string, value: string) => void;
get: (key: string) => string;
delete: (key: string) => void;
};
`,
props: {
targetId,
},
object: (props, state, setState) => {
return {
targetEnvironment: {
set: (key: string, value: string) => {
setState({
...state,
[props.targetId]: {
...state[props.targetId],
[key]: value,
},
});
},
get: (key: string) => {
return state[props.targetId]?.[key];
},
delete: (key: string) => {
const newState = JSON.parse(JSON.stringify(state));
delete newState[props.targetId][key];
setState(newState);
},
},
};
},
},
},
commands: [
{
name: 'Open Target Environment Variables',
icon: <GlobeIcon />,
onClick: laboratory => {
const tab =
laboratory.tabs.find(t => t.type === 'target-env') ??
laboratory.addTab({
type: 'target-env',
data: {},
});
laboratory.setActiveTab(tab);
},
},
],
tabs: [
{
type: 'target-env',
name: 'Target Environment Variables',
icon: <GlobeIcon className="size-4 text-orange-400" />,
component: (_tab, _laboratory, state, setState) => {
return (
<Editor
defaultValue={Object.entries(state?.[targetId] ?? {})
.map(([key, value]) => `${key}=${value}`)
.join('\n')}
onChange={value => {
setState({
...state,
[targetId]: Object.fromEntries(
value
?.split('\n')
.filter(line => line.trim() && !line.trim().startsWith('#'))
.map(line => {
const parts = line.split(/=(.*)/s);
return [parts[0].trim(), (parts[1] ?? '').trim()];
}) ?? [],
),
});
}}
language="dotenv"
options={{
scrollbar: {
horizontal: 'hidden',
},
}}
/>
);
},
},
],
} satisfies LaboratoryPlugin<Record<string, Record<string, string>>>;
};