@sirenodeck/sirenodeck 0.2.6 → 0.2.9
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/config-ui/index.html +13 -0
- package/config-ui/src/App.tsx +675 -0
- package/config-ui/src/DeckFrame.tsx +445 -0
- package/config-ui/src/Shell.tsx +108 -0
- package/config-ui/src/SidePanel.tsx +96 -0
- package/config-ui/src/bridge-log-store.ts +115 -0
- package/config-ui/src/bridge.ts +303 -0
- package/config-ui/src/components/ActionValueEditor.tsx +209 -0
- package/config-ui/src/components/ButtonActionsEditor.tsx +84 -0
- package/config-ui/src/components/DisconnectedOverlay.tsx +77 -0
- package/config-ui/src/components/EmojiPicker.tsx +65 -0
- package/config-ui/src/components/IconPicker.tsx +264 -0
- package/config-ui/src/components/MacroPreview.tsx +22 -0
- package/config-ui/src/components/ReconnectingBanner.tsx +35 -0
- package/config-ui/src/gesture.ts +45 -0
- package/config-ui/src/index.css +8 -0
- package/config-ui/src/main.tsx +20 -0
- package/config-ui/src/pages/AboutPage.tsx +65 -0
- package/config-ui/src/pages/AddonsPage.tsx +232 -0
- package/config-ui/src/pages/BridgeLogsPage.tsx +94 -0
- package/config-ui/src/pages/ButtonConfigEditor/ButtonAppearanceFields.tsx +77 -0
- package/config-ui/src/pages/ButtonConfigEditor/ButtonTypePicker.tsx +80 -0
- package/config-ui/src/pages/ButtonConfigEditor/ConfigForm.tsx +50 -0
- package/config-ui/src/pages/ButtonConfigEditor/FormFieldParts.tsx +23 -0
- package/config-ui/src/pages/ButtonConfigEditor/PositionPicker.tsx +68 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/ActionControl.tsx +27 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/ArrayControl.tsx +77 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/BooleanControl.tsx +34 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/DeckSelectControl.tsx +47 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/EmojiControl.tsx +70 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/IconControl.tsx +31 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/ObjectControl.tsx +52 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/RecordControl.tsx +104 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/SelectControl.tsx +46 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/ShellControl.tsx +29 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/TextControl.tsx +36 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/UnionControl.tsx +96 -0
- package/config-ui/src/pages/ButtonConfigEditor/controls/registry.ts +17 -0
- package/config-ui/src/pages/ButtonConfigEditor/types.ts +67 -0
- package/config-ui/src/pages/ButtonConfigEditor/utils.ts +56 -0
- package/config-ui/src/pages/ButtonConfigEditor.tsx +226 -0
- package/config-ui/src/pages/ConfigPage.tsx +326 -0
- package/config-ui/src/pages/DecksPage.tsx +104 -0
- package/config-ui/src/pages/EditorPage.tsx +1697 -0
- package/config-ui/src/pages/ServiceLogsPage.tsx +130 -0
- package/config-ui/vite.config.ts +105 -0
- package/dist/main.mjs +403 -41501
- package/frontend/index.html +12 -0
- package/frontend/src/App.tsx +536 -0
- package/frontend/src/__mocks__/addons-registry.ts +2 -0
- package/frontend/src/__mocks__/theme.ts +1 -0
- package/frontend/src/__mocks__/themes-manifest.tsx +78 -0
- package/frontend/src/__mocks__/token.ts +2 -0
- package/frontend/src/bridge/client.ts +186 -0
- package/frontend/src/bridge/use-button-action.ts +31 -0
- package/frontend/src/bridge/ws-context.tsx +24 -0
- package/frontend/src/components/Deck.tsx +486 -0
- package/frontend/src/components/DisconnectedOverlay.tsx +82 -0
- package/frontend/src/components/ErrorBoundary.tsx +92 -0
- package/frontend/src/components/ReconnectingBanner.tsx +40 -0
- package/frontend/src/index.css +40 -0
- package/frontend/src/main.tsx +23 -0
- package/frontend/vite.config.ts +132 -0
- package/package.json +20 -6
- package/src/builtin-addons/internal-settings/index.ts +0 -1
- package/src/builtin-addons/internal-settings/logo72x72.png +0 -0
- package/src/cli/commands/emulator-mode.ts +26 -4
- package/src/cli/commands/run.ts +28 -1
- package/src/cli/commands/service-manager.ts +53 -1
- package/src/cli/commands/start.ts +17 -0
- package/src/cli/startup-display.ts +1 -1
- package/src/core/settings.ts +5 -3
- package/src/device/black-frame.ts +5 -14
- package/src/outputClient/real.ts +2 -0
- package/src/vite/virtual-modules.ts +16 -1
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { ReactNode } from "react"
|
|
2
|
+
|
|
3
|
+
import type { PendingIconAsset } from "../../components/IconPicker"
|
|
4
|
+
|
|
5
|
+
export interface JsonSchema {
|
|
6
|
+
readonly type?: string
|
|
7
|
+
readonly title?: string
|
|
8
|
+
readonly description?: string
|
|
9
|
+
readonly default?: unknown
|
|
10
|
+
readonly const?: unknown
|
|
11
|
+
readonly enum?: unknown[]
|
|
12
|
+
readonly properties?: Record<string, JsonSchema>
|
|
13
|
+
readonly required?: string[]
|
|
14
|
+
readonly items?: JsonSchema
|
|
15
|
+
readonly minItems?: number
|
|
16
|
+
readonly maxItems?: number
|
|
17
|
+
readonly oneOf?: JsonSchema[]
|
|
18
|
+
readonly anyOf?: JsonSchema[]
|
|
19
|
+
readonly additionalProperties?: JsonSchema | boolean
|
|
20
|
+
readonly $defs?: Record<string, JsonSchema>
|
|
21
|
+
readonly $ref?: string
|
|
22
|
+
readonly internal?: boolean
|
|
23
|
+
readonly "x-control"?: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ValidationState {
|
|
27
|
+
readonly requestId: string
|
|
28
|
+
readonly valid: boolean
|
|
29
|
+
readonly errors: string[]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ButtonAppearance {
|
|
33
|
+
readonly variant: string
|
|
34
|
+
readonly actions: { tap?: string; dbltap?: string; hold?: string }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ButtonThemeVariant {
|
|
38
|
+
readonly background: string
|
|
39
|
+
readonly border: string
|
|
40
|
+
readonly foreground: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ConfigFormProps {
|
|
44
|
+
readonly schema: JsonSchema
|
|
45
|
+
readonly value: unknown
|
|
46
|
+
readonly path: string
|
|
47
|
+
readonly onChange: (path: string, value: unknown) => void
|
|
48
|
+
readonly deckOptions?: readonly { id: string; name: string }[]
|
|
49
|
+
readonly compact?: boolean
|
|
50
|
+
readonly onPendingAssetChange?: (
|
|
51
|
+
path: string,
|
|
52
|
+
asset: PendingIconAsset | null,
|
|
53
|
+
) => void
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ControlProps extends ConfigFormProps {
|
|
57
|
+
readonly label: string
|
|
58
|
+
readonly description?: string
|
|
59
|
+
readonly renderConfig: (props: ConfigFormProps) => ReactNode
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface FieldPartsProps {
|
|
63
|
+
readonly label: string
|
|
64
|
+
readonly description?: string
|
|
65
|
+
readonly error?: string
|
|
66
|
+
readonly children: ReactNode
|
|
67
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { JsonSchema } from "./types"
|
|
2
|
+
|
|
3
|
+
export const labelFor = (key: string): string =>
|
|
4
|
+
key
|
|
5
|
+
.replaceAll(/[-_]/g, " ")
|
|
6
|
+
.replace(/\b\w/g, (letter) => letter.toUpperCase())
|
|
7
|
+
|
|
8
|
+
export const joinPath = (path: string, key: string | number): string =>
|
|
9
|
+
path === "" ? String(key) : `${path}.${key}`
|
|
10
|
+
|
|
11
|
+
export const valueAt = (value: unknown, path: string): unknown => {
|
|
12
|
+
let current = value
|
|
13
|
+
for (const segment of path.split(".").filter(Boolean)) {
|
|
14
|
+
if (typeof current !== "object" || current === null) return undefined
|
|
15
|
+
current = (current as Record<string, unknown>)[segment]
|
|
16
|
+
}
|
|
17
|
+
return current
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const setAt = (value: unknown, path: string, next: unknown): unknown => {
|
|
21
|
+
const segments = path.split(".").filter(Boolean)
|
|
22
|
+
if (segments.length === 0) return next
|
|
23
|
+
const root =
|
|
24
|
+
typeof value === "object" && value !== null && !Array.isArray(value)
|
|
25
|
+
? { ...(value as Record<string, unknown>) }
|
|
26
|
+
: {}
|
|
27
|
+
let current = root
|
|
28
|
+
for (const segment of segments.slice(0, -1)) {
|
|
29
|
+
const child = current[segment]
|
|
30
|
+
current[segment] =
|
|
31
|
+
typeof child === "object" && child !== null && !Array.isArray(child)
|
|
32
|
+
? { ...(child as Record<string, unknown>) }
|
|
33
|
+
: {}
|
|
34
|
+
current = current[segment] as Record<string, unknown>
|
|
35
|
+
}
|
|
36
|
+
current[segments.at(-1)!] = next
|
|
37
|
+
return root
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const defaultValue = (schema: JsonSchema): unknown => {
|
|
41
|
+
if (schema.default !== undefined) return schema.default
|
|
42
|
+
if (schema.const !== undefined) return schema.const
|
|
43
|
+
if (schema.enum?.[0] !== undefined) return schema.enum[0]
|
|
44
|
+
if (schema.type === "object") {
|
|
45
|
+
return Object.fromEntries(
|
|
46
|
+
Object.entries(schema.properties ?? {})
|
|
47
|
+
.filter(([, child]) => child.internal !== true)
|
|
48
|
+
.filter(([key]) => schema.required?.includes(key))
|
|
49
|
+
.map(([key, child]) => [key, defaultValue(child)]),
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
if (schema.type === "array") return []
|
|
53
|
+
if (schema.type === "boolean") return false
|
|
54
|
+
if (schema.type === "number" || schema.type === "integer") return 0
|
|
55
|
+
return ""
|
|
56
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { useEffect, useState } from "react"
|
|
2
|
+
|
|
3
|
+
import { Button, Tabs, TextArea } from "@heroui/react"
|
|
4
|
+
import { Check, CircleX } from "lucide-react"
|
|
5
|
+
import { parse, stringify } from "yaml"
|
|
6
|
+
|
|
7
|
+
import type { WsClient } from "../bridge"
|
|
8
|
+
import { ConfigForm } from "./ButtonConfigEditor/ConfigForm"
|
|
9
|
+
import { setAt } from "./ButtonConfigEditor/utils"
|
|
10
|
+
import type { JsonSchema, ValidationState } from "./ButtonConfigEditor/types"
|
|
11
|
+
import type { PendingIconAsset } from "../components/IconPicker"
|
|
12
|
+
|
|
13
|
+
export type { JsonSchema, ValidationState } from "./ButtonConfigEditor/types"
|
|
14
|
+
|
|
15
|
+
export interface ButtonConfigEditorProps {
|
|
16
|
+
readonly wsClient: WsClient | null
|
|
17
|
+
readonly revision: number
|
|
18
|
+
readonly buttonType: string
|
|
19
|
+
readonly config: unknown
|
|
20
|
+
readonly schema?: JsonSchema
|
|
21
|
+
readonly validation: ValidationState | null
|
|
22
|
+
readonly onSave: (config: Record<string, unknown>) => void
|
|
23
|
+
readonly onCancel?: () => void
|
|
24
|
+
readonly deckOptions?: readonly { id: string; name: string }[]
|
|
25
|
+
readonly saveLabel?: string
|
|
26
|
+
readonly actionsInHeader?: boolean
|
|
27
|
+
readonly hideActions?: boolean
|
|
28
|
+
readonly onDraftChange?: (config: Record<string, unknown> | null) => void
|
|
29
|
+
readonly onPreviewChange?: (config: Record<string, unknown> | null) => void
|
|
30
|
+
readonly onPendingAssetChange?: (
|
|
31
|
+
path: string,
|
|
32
|
+
asset: PendingIconAsset | null,
|
|
33
|
+
) => void
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let validationNumber = 0
|
|
37
|
+
|
|
38
|
+
export const ButtonConfigEditor = ({
|
|
39
|
+
wsClient,
|
|
40
|
+
revision,
|
|
41
|
+
buttonType,
|
|
42
|
+
config,
|
|
43
|
+
schema,
|
|
44
|
+
validation,
|
|
45
|
+
onSave,
|
|
46
|
+
onCancel,
|
|
47
|
+
deckOptions,
|
|
48
|
+
saveLabel = "Save button config",
|
|
49
|
+
actionsInHeader = false,
|
|
50
|
+
hideActions = false,
|
|
51
|
+
onDraftChange,
|
|
52
|
+
onPreviewChange,
|
|
53
|
+
onPendingAssetChange,
|
|
54
|
+
}: ButtonConfigEditorProps) => {
|
|
55
|
+
const initial =
|
|
56
|
+
typeof config === "object" && config !== null && !Array.isArray(config)
|
|
57
|
+
? config
|
|
58
|
+
: {}
|
|
59
|
+
const [value, setValue] = useState<unknown>(initial)
|
|
60
|
+
const [yaml, setYaml] = useState(() => stringify(initial))
|
|
61
|
+
const [yamlError, setYamlError] = useState<string | null>(null)
|
|
62
|
+
const [requestId, setRequestId] = useState("")
|
|
63
|
+
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
setValue(initial)
|
|
66
|
+
setYaml(stringify(initial))
|
|
67
|
+
setYamlError(null)
|
|
68
|
+
}, [config])
|
|
69
|
+
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
if (wsClient === null) return
|
|
72
|
+
const nextRequestId = `validation-${Date.now()}-${validationNumber++}`
|
|
73
|
+
setRequestId(nextRequestId)
|
|
74
|
+
wsClient.send(
|
|
75
|
+
JSON.stringify({
|
|
76
|
+
type: "editor-validation-request",
|
|
77
|
+
requestId: nextRequestId,
|
|
78
|
+
revision,
|
|
79
|
+
buttonType,
|
|
80
|
+
config: value,
|
|
81
|
+
}),
|
|
82
|
+
)
|
|
83
|
+
}, [buttonType, revision, value, wsClient])
|
|
84
|
+
|
|
85
|
+
const change = (path: string, next: unknown): void => {
|
|
86
|
+
const nextValue = setAt(value, path, next)
|
|
87
|
+
setValue(nextValue)
|
|
88
|
+
setYaml(stringify(nextValue))
|
|
89
|
+
setYamlError(null)
|
|
90
|
+
setRequestId("")
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const yamlChange = (next: string): void => {
|
|
94
|
+
setYaml(next)
|
|
95
|
+
setRequestId("")
|
|
96
|
+
try {
|
|
97
|
+
const parsed = parse(next)
|
|
98
|
+
if (
|
|
99
|
+
typeof parsed !== "object" ||
|
|
100
|
+
parsed === null ||
|
|
101
|
+
Array.isArray(parsed)
|
|
102
|
+
)
|
|
103
|
+
throw new Error("Config YAML must be an object")
|
|
104
|
+
setValue(parsed)
|
|
105
|
+
setYamlError(null)
|
|
106
|
+
} catch (error) {
|
|
107
|
+
setYamlError(error instanceof Error ? error.message : "Invalid YAML")
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const errors =
|
|
112
|
+
yamlError !== null
|
|
113
|
+
? [yamlError]
|
|
114
|
+
: validation?.requestId === requestId && !validation.valid
|
|
115
|
+
? validation.errors
|
|
116
|
+
: []
|
|
117
|
+
const canSave =
|
|
118
|
+
yamlError === null &&
|
|
119
|
+
validation?.requestId === requestId &&
|
|
120
|
+
validation.valid === true &&
|
|
121
|
+
typeof value === "object" &&
|
|
122
|
+
value !== null &&
|
|
123
|
+
!Array.isArray(value)
|
|
124
|
+
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
onDraftChange?.(canSave ? (value as Record<string, unknown>) : null)
|
|
127
|
+
}, [canSave, onDraftChange, value])
|
|
128
|
+
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
const previewConfig =
|
|
131
|
+
yamlError === null &&
|
|
132
|
+
typeof value === "object" &&
|
|
133
|
+
value !== null &&
|
|
134
|
+
!Array.isArray(value)
|
|
135
|
+
? (value as Record<string, unknown>)
|
|
136
|
+
: null
|
|
137
|
+
onPreviewChange?.(previewConfig)
|
|
138
|
+
}, [onPreviewChange, value, yamlError])
|
|
139
|
+
|
|
140
|
+
return (
|
|
141
|
+
<div className="grid gap-3">
|
|
142
|
+
{actionsInHeader && (
|
|
143
|
+
<div className="flex justify-end gap-2">
|
|
144
|
+
{onCancel !== undefined && (
|
|
145
|
+
<Button type="button" variant="tertiary" onPress={onCancel}>
|
|
146
|
+
Cancel
|
|
147
|
+
</Button>
|
|
148
|
+
)}
|
|
149
|
+
<Button
|
|
150
|
+
type="button"
|
|
151
|
+
variant="primary"
|
|
152
|
+
isDisabled={!canSave}
|
|
153
|
+
onPress={() => onSave(value as Record<string, unknown>)}
|
|
154
|
+
>
|
|
155
|
+
{saveLabel}
|
|
156
|
+
</Button>
|
|
157
|
+
</div>
|
|
158
|
+
)}
|
|
159
|
+
<Tabs aria-label="Button config editor" defaultSelectedKey="form">
|
|
160
|
+
<Tabs.ListContainer>
|
|
161
|
+
<Tabs.List>
|
|
162
|
+
<Tabs.Tab id="form">
|
|
163
|
+
Form
|
|
164
|
+
<Tabs.Indicator />
|
|
165
|
+
</Tabs.Tab>
|
|
166
|
+
<Tabs.Tab id="yaml">
|
|
167
|
+
YAML
|
|
168
|
+
<Tabs.Indicator />
|
|
169
|
+
</Tabs.Tab>
|
|
170
|
+
</Tabs.List>
|
|
171
|
+
</Tabs.ListContainer>
|
|
172
|
+
<Tabs.Panel id="form">
|
|
173
|
+
{schema === undefined ? (
|
|
174
|
+
<p className="text-sm text-amber-300">
|
|
175
|
+
No visual schema is available for this button type. Use YAML.
|
|
176
|
+
</p>
|
|
177
|
+
) : (
|
|
178
|
+
<ConfigForm
|
|
179
|
+
schema={schema}
|
|
180
|
+
value={value}
|
|
181
|
+
path=""
|
|
182
|
+
onChange={change}
|
|
183
|
+
deckOptions={deckOptions}
|
|
184
|
+
onPendingAssetChange={onPendingAssetChange}
|
|
185
|
+
/>
|
|
186
|
+
)}
|
|
187
|
+
</Tabs.Panel>
|
|
188
|
+
<Tabs.Panel id="yaml">
|
|
189
|
+
<TextArea
|
|
190
|
+
aria-label="Button config YAML"
|
|
191
|
+
value={yaml}
|
|
192
|
+
onChange={(event) => yamlChange(event.target.value)}
|
|
193
|
+
spellCheck={false}
|
|
194
|
+
className="min-h-64 w-full resize-none font-mono text-sm"
|
|
195
|
+
/>
|
|
196
|
+
</Tabs.Panel>
|
|
197
|
+
</Tabs>
|
|
198
|
+
{!actionsInHeader && !hideActions && (
|
|
199
|
+
<div className="sticky bottom-0 flex gap-2 border-t border-separator bg-surface pt-3">
|
|
200
|
+
{onCancel !== undefined && (
|
|
201
|
+
<Button type="button" variant="tertiary" onPress={onCancel}>
|
|
202
|
+
<CircleX aria-hidden="true" className="size-4" />
|
|
203
|
+
Cancel
|
|
204
|
+
</Button>
|
|
205
|
+
)}
|
|
206
|
+
<Button
|
|
207
|
+
type="button"
|
|
208
|
+
variant="primary"
|
|
209
|
+
isDisabled={!canSave}
|
|
210
|
+
onPress={() => onSave(value as Record<string, unknown>)}
|
|
211
|
+
>
|
|
212
|
+
<Check aria-hidden="true" className="size-4" />
|
|
213
|
+
{saveLabel}
|
|
214
|
+
</Button>
|
|
215
|
+
</div>
|
|
216
|
+
)}
|
|
217
|
+
{errors.length > 0 && (
|
|
218
|
+
<div role="alert" className="grid gap-1 text-sm text-danger">
|
|
219
|
+
{errors.map((error) => (
|
|
220
|
+
<p key={error}>{error}</p>
|
|
221
|
+
))}
|
|
222
|
+
</div>
|
|
223
|
+
)}
|
|
224
|
+
</div>
|
|
225
|
+
)
|
|
226
|
+
}
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { useEffect, useState } from "react"
|
|
2
|
+
import { Button, Card, ListBox, Select, Tabs, TextArea } from "@heroui/react"
|
|
3
|
+
import { CodeXml, PencilRuler } from "lucide-react"
|
|
4
|
+
|
|
5
|
+
import type { WsClient } from "../bridge"
|
|
6
|
+
import type { EditorState } from "./EditorPage"
|
|
7
|
+
|
|
8
|
+
export interface ConfigPageProps {
|
|
9
|
+
readonly configPath?: string | null
|
|
10
|
+
readonly editor?: React.ReactNode
|
|
11
|
+
readonly wsClient?: WsClient | null
|
|
12
|
+
readonly editorState?: EditorState | null
|
|
13
|
+
readonly sourceValidation?: {
|
|
14
|
+
readonly requestId: string
|
|
15
|
+
readonly valid: boolean
|
|
16
|
+
readonly errors: string[]
|
|
17
|
+
} | null
|
|
18
|
+
readonly mutationResult?: {
|
|
19
|
+
readonly requestId: string
|
|
20
|
+
readonly ok: boolean
|
|
21
|
+
readonly error?: string
|
|
22
|
+
} | null
|
|
23
|
+
readonly activeTab?: "editor" | "config"
|
|
24
|
+
readonly onTabChange?: (tab: "editor" | "config") => void
|
|
25
|
+
readonly revision?: number | null
|
|
26
|
+
readonly canUndo?: boolean
|
|
27
|
+
readonly onUndo?: () => void
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let sourceRequestNumber = 0
|
|
31
|
+
const nextSourceRequestId = (): string =>
|
|
32
|
+
`source-${Date.now()}-${sourceRequestNumber++}`
|
|
33
|
+
|
|
34
|
+
export const ConfigPage = ({
|
|
35
|
+
configPath,
|
|
36
|
+
editor,
|
|
37
|
+
wsClient = null,
|
|
38
|
+
editorState = null,
|
|
39
|
+
sourceValidation = null,
|
|
40
|
+
mutationResult = null,
|
|
41
|
+
activeTab,
|
|
42
|
+
onTabChange,
|
|
43
|
+
revision = null,
|
|
44
|
+
canUndo = false,
|
|
45
|
+
onUndo,
|
|
46
|
+
}: ConfigPageProps) => {
|
|
47
|
+
const [internalTab, setInternalTab] = useState<"editor" | "config">(
|
|
48
|
+
editor === undefined ? "config" : "editor",
|
|
49
|
+
)
|
|
50
|
+
const tab = activeTab ?? internalTab
|
|
51
|
+
const sources = editorState?.sources ?? []
|
|
52
|
+
const [selectedSource, setSelectedSource] = useState<string | null>(
|
|
53
|
+
sources[0] ?? configPath ?? null,
|
|
54
|
+
)
|
|
55
|
+
const [draft, setDraft] = useState<string | null>(null)
|
|
56
|
+
const [requestId, setRequestId] = useState<string | null>(null)
|
|
57
|
+
const [saveRequestId, setSaveRequestId] = useState<string | null>(null)
|
|
58
|
+
const [themeRequestId, setThemeRequestId] = useState<string | null>(null)
|
|
59
|
+
const [themeMessage, setThemeMessage] = useState<string | null>(null)
|
|
60
|
+
const [selectedTheme, setSelectedTheme] = useState<string | null>(null)
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (selectedSource === null && sources[0] !== undefined) {
|
|
64
|
+
setSelectedSource(sources[0])
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
if (selectedSource === null) return
|
|
68
|
+
setDraft(editorState?.sourceContents?.[selectedSource] ?? "")
|
|
69
|
+
setRequestId(null)
|
|
70
|
+
setSaveRequestId(null)
|
|
71
|
+
}, [editorState?.revision, selectedSource])
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (mutationResult?.requestId !== saveRequestId) return
|
|
75
|
+
if (mutationResult.ok) setSaveRequestId(null)
|
|
76
|
+
}, [mutationResult, saveRequestId])
|
|
77
|
+
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (mutationResult?.requestId !== themeRequestId) return
|
|
80
|
+
setThemeMessage(
|
|
81
|
+
mutationResult.ok
|
|
82
|
+
? "Theme saved"
|
|
83
|
+
: (mutationResult.error ?? "Theme update failed"),
|
|
84
|
+
)
|
|
85
|
+
if (!mutationResult.ok) setSelectedTheme(null)
|
|
86
|
+
setThemeRequestId(null)
|
|
87
|
+
}, [mutationResult, themeRequestId])
|
|
88
|
+
|
|
89
|
+
const original =
|
|
90
|
+
selectedSource === null
|
|
91
|
+
? ""
|
|
92
|
+
: (editorState?.sourceContents?.[selectedSource] ?? "")
|
|
93
|
+
const isDirty = draft !== null && draft !== original
|
|
94
|
+
const validationMatches =
|
|
95
|
+
requestId !== null && sourceValidation?.requestId === requestId
|
|
96
|
+
const canSave =
|
|
97
|
+
isDirty && validationMatches && sourceValidation?.valid === true
|
|
98
|
+
const theme = (
|
|
99
|
+
editorState?.config as { theme?: string | { src?: string } } | undefined
|
|
100
|
+
)?.theme
|
|
101
|
+
const activeTheme =
|
|
102
|
+
typeof theme === "string" ? theme : (theme?.src ?? "default")
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
if (selectedTheme === activeTheme) setSelectedTheme(null)
|
|
106
|
+
}, [activeTheme, selectedTheme])
|
|
107
|
+
|
|
108
|
+
const changeDraft = (content: string): void => {
|
|
109
|
+
setDraft(content)
|
|
110
|
+
const nextRequestId = nextSourceRequestId()
|
|
111
|
+
setRequestId(nextRequestId)
|
|
112
|
+
wsClient?.send(
|
|
113
|
+
JSON.stringify({
|
|
114
|
+
type: "editor-source-validation-request",
|
|
115
|
+
requestId: nextRequestId,
|
|
116
|
+
revision: editorState?.revision ?? 0,
|
|
117
|
+
path: selectedSource,
|
|
118
|
+
content,
|
|
119
|
+
}),
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const save = (): void => {
|
|
124
|
+
if (!canSave || selectedSource === null || draft === null) return
|
|
125
|
+
const nextRequestId = nextSourceRequestId()
|
|
126
|
+
setSaveRequestId(nextRequestId)
|
|
127
|
+
wsClient?.send(
|
|
128
|
+
JSON.stringify({
|
|
129
|
+
type: "editor-mutate",
|
|
130
|
+
requestId: nextRequestId,
|
|
131
|
+
revision: editorState?.revision ?? 0,
|
|
132
|
+
mutation: { kind: "edit-source", path: selectedSource, content: draft },
|
|
133
|
+
}),
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return (
|
|
138
|
+
<div className="flex h-full min-h-0 flex-col">
|
|
139
|
+
<Tabs
|
|
140
|
+
selectedKey={tab}
|
|
141
|
+
onSelectionChange={(key) => {
|
|
142
|
+
const next = String(key) as "editor" | "config"
|
|
143
|
+
setInternalTab(next)
|
|
144
|
+
onTabChange?.(next)
|
|
145
|
+
}}
|
|
146
|
+
className="min-h-0 flex-1"
|
|
147
|
+
>
|
|
148
|
+
<div className="flex items-center justify-between gap-3 px-4 pt-4">
|
|
149
|
+
<Tabs.ListContainer>
|
|
150
|
+
<Tabs.List
|
|
151
|
+
aria-label="Configuration views"
|
|
152
|
+
className="w-fit rounded-full bg-[var(--surface-secondary)] p-1"
|
|
153
|
+
>
|
|
154
|
+
{editor !== undefined && (
|
|
155
|
+
<Tabs.Tab
|
|
156
|
+
id="editor"
|
|
157
|
+
className="flex items-center gap-2 rounded-full px-4 py-1.5 text-xs"
|
|
158
|
+
>
|
|
159
|
+
<PencilRuler size={14} aria-hidden="true" />
|
|
160
|
+
Editor
|
|
161
|
+
<Tabs.Indicator />
|
|
162
|
+
</Tabs.Tab>
|
|
163
|
+
)}
|
|
164
|
+
<Tabs.Tab
|
|
165
|
+
id="config"
|
|
166
|
+
className="flex items-center gap-2 rounded-full px-4 py-1.5 text-xs"
|
|
167
|
+
>
|
|
168
|
+
<CodeXml size={14} aria-hidden="true" />
|
|
169
|
+
Config
|
|
170
|
+
<Tabs.Indicator />
|
|
171
|
+
</Tabs.Tab>
|
|
172
|
+
</Tabs.List>
|
|
173
|
+
</Tabs.ListContainer>
|
|
174
|
+
<div className="flex items-center gap-2">
|
|
175
|
+
<span
|
|
176
|
+
role="status"
|
|
177
|
+
aria-live="polite"
|
|
178
|
+
className="text-sm text-muted"
|
|
179
|
+
>
|
|
180
|
+
{revision === null
|
|
181
|
+
? "Waiting for editor state"
|
|
182
|
+
: `Revision ${revision}`}
|
|
183
|
+
</span>
|
|
184
|
+
<Button
|
|
185
|
+
type="button"
|
|
186
|
+
variant="tertiary"
|
|
187
|
+
isDisabled={!canUndo}
|
|
188
|
+
onPress={onUndo}
|
|
189
|
+
>
|
|
190
|
+
Undo
|
|
191
|
+
</Button>
|
|
192
|
+
<span className="text-sm text-muted">Theme</span>
|
|
193
|
+
<Select
|
|
194
|
+
aria-label="Theme"
|
|
195
|
+
selectedKey={selectedTheme ?? activeTheme}
|
|
196
|
+
onSelectionChange={(key) => {
|
|
197
|
+
const themeName = String(key)
|
|
198
|
+
const nextRequestId = nextSourceRequestId()
|
|
199
|
+
setSelectedTheme(themeName)
|
|
200
|
+
setThemeRequestId(nextRequestId)
|
|
201
|
+
setThemeMessage("Saving theme...")
|
|
202
|
+
wsClient?.send(
|
|
203
|
+
JSON.stringify({
|
|
204
|
+
type: "editor-mutate",
|
|
205
|
+
requestId: nextRequestId,
|
|
206
|
+
revision: editorState?.revision ?? 0,
|
|
207
|
+
mutation: { kind: "set-theme", theme: themeName },
|
|
208
|
+
}),
|
|
209
|
+
)
|
|
210
|
+
}}
|
|
211
|
+
>
|
|
212
|
+
<Select.Trigger className="min-w-32">
|
|
213
|
+
<Select.Value />
|
|
214
|
+
<Select.Indicator />
|
|
215
|
+
</Select.Trigger>
|
|
216
|
+
<Select.Popover>
|
|
217
|
+
<ListBox>
|
|
218
|
+
{(editorState?.themes?.length === 0
|
|
219
|
+
? [{ name: activeTheme }]
|
|
220
|
+
: (editorState?.themes ?? [{ name: activeTheme }])
|
|
221
|
+
).map((theme) => (
|
|
222
|
+
<ListBox.Item
|
|
223
|
+
key={theme.name}
|
|
224
|
+
id={theme.name}
|
|
225
|
+
textValue={theme.name}
|
|
226
|
+
>
|
|
227
|
+
{theme.name}
|
|
228
|
+
<ListBox.ItemIndicator />
|
|
229
|
+
</ListBox.Item>
|
|
230
|
+
))}
|
|
231
|
+
</ListBox>
|
|
232
|
+
</Select.Popover>
|
|
233
|
+
</Select>
|
|
234
|
+
{themeMessage !== null && (
|
|
235
|
+
<span role="status" aria-live="polite" className="sr-only">
|
|
236
|
+
{themeMessage}
|
|
237
|
+
</span>
|
|
238
|
+
)}
|
|
239
|
+
</div>
|
|
240
|
+
</div>
|
|
241
|
+
{editor !== undefined && (
|
|
242
|
+
<Tabs.Panel
|
|
243
|
+
id="editor"
|
|
244
|
+
className="min-h-0 flex-1 overflow-hidden p-4"
|
|
245
|
+
>
|
|
246
|
+
{editor}
|
|
247
|
+
</Tabs.Panel>
|
|
248
|
+
)}
|
|
249
|
+
<Tabs.Panel id="config" className="min-h-0 flex-1 overflow-hidden p-4">
|
|
250
|
+
<div className="flex h-full min-h-0 flex-col gap-4 lg:flex-row">
|
|
251
|
+
<Card className="w-full shrink-0 lg:w-64">
|
|
252
|
+
<Card.Header>
|
|
253
|
+
<Card.Title>Configuration files</Card.Title>
|
|
254
|
+
</Card.Header>
|
|
255
|
+
<Card.Content className="grid gap-1">
|
|
256
|
+
{sources.map((source, index) => (
|
|
257
|
+
<button
|
|
258
|
+
key={source}
|
|
259
|
+
type="button"
|
|
260
|
+
onClick={() => setSelectedSource(source)}
|
|
261
|
+
aria-pressed={selectedSource === source}
|
|
262
|
+
className="rounded-lg px-3 py-2 text-left text-xs aria-pressed:bg-[var(--default)] aria-pressed:text-[var(--foreground)]"
|
|
263
|
+
>
|
|
264
|
+
<span className="block truncate font-mono">{source}</span>
|
|
265
|
+
<span className="text-[10px] text-[var(--muted)]">
|
|
266
|
+
{index === 0 ? "Main config" : "Included file"}
|
|
267
|
+
</span>
|
|
268
|
+
</button>
|
|
269
|
+
))}
|
|
270
|
+
</Card.Content>
|
|
271
|
+
</Card>
|
|
272
|
+
<Card className="flex min-h-0 min-w-0 flex-1 flex-col">
|
|
273
|
+
<Card.Header className="flex flex-row flex-wrap items-center justify-between gap-3">
|
|
274
|
+
<div className="min-w-0">
|
|
275
|
+
<h2 className="truncate text-base font-semibold">
|
|
276
|
+
{selectedSource ?? "Configuration"}
|
|
277
|
+
</h2>
|
|
278
|
+
<p className="text-xs text-[var(--muted)]">
|
|
279
|
+
The complete configuration must validate before saving.
|
|
280
|
+
</p>
|
|
281
|
+
</div>
|
|
282
|
+
<div className="flex gap-2">
|
|
283
|
+
<Button
|
|
284
|
+
type="button"
|
|
285
|
+
variant="tertiary"
|
|
286
|
+
onPress={() => setDraft(original)}
|
|
287
|
+
isDisabled={!isDirty}
|
|
288
|
+
>
|
|
289
|
+
Cancel
|
|
290
|
+
</Button>
|
|
291
|
+
<Button
|
|
292
|
+
type="button"
|
|
293
|
+
variant="primary"
|
|
294
|
+
onPress={save}
|
|
295
|
+
isDisabled={!canSave || saveRequestId !== null}
|
|
296
|
+
>
|
|
297
|
+
{saveRequestId === null ? "Save" : "Saving..."}
|
|
298
|
+
</Button>
|
|
299
|
+
</div>
|
|
300
|
+
</Card.Header>
|
|
301
|
+
<Card.Content className="flex min-h-0 flex-1 flex-col">
|
|
302
|
+
<TextArea
|
|
303
|
+
aria-label="Configuration source YAML"
|
|
304
|
+
value={draft ?? ""}
|
|
305
|
+
onChange={(event) => changeDraft(event.target.value)}
|
|
306
|
+
spellCheck={false}
|
|
307
|
+
className="min-h-0 w-full flex-1 font-mono"
|
|
308
|
+
/>
|
|
309
|
+
{validationMatches && sourceValidation?.errors.length !== 0 && (
|
|
310
|
+
<div
|
|
311
|
+
role="alert"
|
|
312
|
+
className="mt-3 grid gap-1 text-sm text-[var(--danger)]"
|
|
313
|
+
>
|
|
314
|
+
{sourceValidation?.errors.map((error) => (
|
|
315
|
+
<p key={error}>{error}</p>
|
|
316
|
+
))}
|
|
317
|
+
</div>
|
|
318
|
+
)}
|
|
319
|
+
</Card.Content>
|
|
320
|
+
</Card>
|
|
321
|
+
</div>
|
|
322
|
+
</Tabs.Panel>
|
|
323
|
+
</Tabs>
|
|
324
|
+
</div>
|
|
325
|
+
)
|
|
326
|
+
}
|