@sirenodeck/sirenodeck 0.2.7 → 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.
Files changed (70) hide show
  1. package/config-ui/index.html +13 -0
  2. package/config-ui/src/App.tsx +675 -0
  3. package/config-ui/src/DeckFrame.tsx +445 -0
  4. package/config-ui/src/Shell.tsx +108 -0
  5. package/config-ui/src/SidePanel.tsx +96 -0
  6. package/config-ui/src/bridge-log-store.ts +115 -0
  7. package/config-ui/src/bridge.ts +303 -0
  8. package/config-ui/src/components/ActionValueEditor.tsx +209 -0
  9. package/config-ui/src/components/ButtonActionsEditor.tsx +84 -0
  10. package/config-ui/src/components/DisconnectedOverlay.tsx +77 -0
  11. package/config-ui/src/components/EmojiPicker.tsx +65 -0
  12. package/config-ui/src/components/IconPicker.tsx +264 -0
  13. package/config-ui/src/components/MacroPreview.tsx +22 -0
  14. package/config-ui/src/components/ReconnectingBanner.tsx +35 -0
  15. package/config-ui/src/gesture.ts +45 -0
  16. package/config-ui/src/index.css +8 -0
  17. package/config-ui/src/main.tsx +20 -0
  18. package/config-ui/src/pages/AboutPage.tsx +65 -0
  19. package/config-ui/src/pages/AddonsPage.tsx +232 -0
  20. package/config-ui/src/pages/BridgeLogsPage.tsx +94 -0
  21. package/config-ui/src/pages/ButtonConfigEditor/ButtonAppearanceFields.tsx +77 -0
  22. package/config-ui/src/pages/ButtonConfigEditor/ButtonTypePicker.tsx +80 -0
  23. package/config-ui/src/pages/ButtonConfigEditor/ConfigForm.tsx +50 -0
  24. package/config-ui/src/pages/ButtonConfigEditor/FormFieldParts.tsx +23 -0
  25. package/config-ui/src/pages/ButtonConfigEditor/PositionPicker.tsx +68 -0
  26. package/config-ui/src/pages/ButtonConfigEditor/controls/ActionControl.tsx +27 -0
  27. package/config-ui/src/pages/ButtonConfigEditor/controls/ArrayControl.tsx +77 -0
  28. package/config-ui/src/pages/ButtonConfigEditor/controls/BooleanControl.tsx +34 -0
  29. package/config-ui/src/pages/ButtonConfigEditor/controls/DeckSelectControl.tsx +47 -0
  30. package/config-ui/src/pages/ButtonConfigEditor/controls/EmojiControl.tsx +70 -0
  31. package/config-ui/src/pages/ButtonConfigEditor/controls/IconControl.tsx +31 -0
  32. package/config-ui/src/pages/ButtonConfigEditor/controls/ObjectControl.tsx +52 -0
  33. package/config-ui/src/pages/ButtonConfigEditor/controls/RecordControl.tsx +104 -0
  34. package/config-ui/src/pages/ButtonConfigEditor/controls/SelectControl.tsx +46 -0
  35. package/config-ui/src/pages/ButtonConfigEditor/controls/ShellControl.tsx +29 -0
  36. package/config-ui/src/pages/ButtonConfigEditor/controls/TextControl.tsx +36 -0
  37. package/config-ui/src/pages/ButtonConfigEditor/controls/UnionControl.tsx +96 -0
  38. package/config-ui/src/pages/ButtonConfigEditor/controls/registry.ts +17 -0
  39. package/config-ui/src/pages/ButtonConfigEditor/types.ts +67 -0
  40. package/config-ui/src/pages/ButtonConfigEditor/utils.ts +56 -0
  41. package/config-ui/src/pages/ButtonConfigEditor.tsx +226 -0
  42. package/config-ui/src/pages/ConfigPage.tsx +326 -0
  43. package/config-ui/src/pages/DecksPage.tsx +104 -0
  44. package/config-ui/src/pages/EditorPage.tsx +1697 -0
  45. package/config-ui/src/pages/ServiceLogsPage.tsx +130 -0
  46. package/config-ui/vite.config.ts +105 -0
  47. package/dist/main.mjs +299 -41557
  48. package/frontend/index.html +12 -0
  49. package/frontend/src/App.tsx +536 -0
  50. package/frontend/src/__mocks__/addons-registry.ts +2 -0
  51. package/frontend/src/__mocks__/theme.ts +1 -0
  52. package/frontend/src/__mocks__/themes-manifest.tsx +78 -0
  53. package/frontend/src/__mocks__/token.ts +2 -0
  54. package/frontend/src/bridge/client.ts +186 -0
  55. package/frontend/src/bridge/use-button-action.ts +31 -0
  56. package/frontend/src/bridge/ws-context.tsx +24 -0
  57. package/frontend/src/components/Deck.tsx +486 -0
  58. package/frontend/src/components/DisconnectedOverlay.tsx +82 -0
  59. package/frontend/src/components/ErrorBoundary.tsx +92 -0
  60. package/frontend/src/components/ReconnectingBanner.tsx +40 -0
  61. package/frontend/src/index.css +40 -0
  62. package/frontend/src/main.tsx +23 -0
  63. package/frontend/vite.config.ts +132 -0
  64. package/package.json +20 -6
  65. package/src/builtin-addons/internal-settings/index.ts +0 -1
  66. package/src/builtin-addons/internal-settings/logo72x72.png +0 -0
  67. package/src/cli/commands/emulator-mode.ts +26 -4
  68. package/src/cli/commands/service-manager.ts +53 -1
  69. package/src/device/black-frame.ts +3 -7
  70. package/src/vite/virtual-modules.ts +16 -1
@@ -0,0 +1,186 @@
1
+ import { ChannelRegistry } from "sirenodeck/react"
2
+ import {
3
+ helloMessageSchema,
4
+ wsMessageSchema,
5
+ type WsMessage,
6
+ } from "@/api/protocol"
7
+
8
+ export type ConnectionStatus =
9
+ | "connecting"
10
+ | "open"
11
+ | "closed"
12
+ | "failed"
13
+ | "authorization-failed"
14
+
15
+ export interface WsClientOptions {
16
+ url: string
17
+ token?: string
18
+ protocolVersion?: number
19
+ onMessage?: (message: WsMessage) => void
20
+ onStatus?: (status: ConnectionStatus) => void
21
+ backoffMs?: ReadonlyArray<number>
22
+ maxAttempts?: number
23
+ }
24
+
25
+ const DEFAULT_BACKOFF = [1000, 2000, 4000, 8000, 16000, 30000]
26
+ const DEFAULT_MAX_ATTEMPTS = 10
27
+
28
+ export interface WsClient {
29
+ connect(): void
30
+ close(): void
31
+ send(message: WsMessage): void
32
+ subscribeChannels(channels: string[]): void
33
+ getAttempt(): number
34
+ getLastError(): string | null
35
+ }
36
+
37
+ export const createWsClient = (options: WsClientOptions): WsClient => {
38
+ const {
39
+ url,
40
+ token,
41
+ protocolVersion = 1,
42
+ onMessage,
43
+ onStatus,
44
+ backoffMs = DEFAULT_BACKOFF,
45
+ maxAttempts = DEFAULT_MAX_ATTEMPTS,
46
+ } = options
47
+
48
+ let socket: WebSocket | null = null
49
+ let attempt = 0
50
+ let manuallyClosed = false
51
+ let timer: ReturnType<typeof setTimeout> | null = null
52
+ let connected = false
53
+ let lastError: string | null = null
54
+ const pendingMessages: WsMessage[] = []
55
+ const subscribedChannels = new Set<string>()
56
+
57
+ const setStatus = (status: ConnectionStatus): void => onStatus?.(status)
58
+
59
+ const flushPending = (): void => {
60
+ while (
61
+ pendingMessages.length > 0 &&
62
+ socket?.readyState === WebSocket.OPEN
63
+ ) {
64
+ const msg = pendingMessages.shift()!
65
+ socket.send(JSON.stringify(msg))
66
+ }
67
+ }
68
+
69
+ const scheduleReconnect = (): void => {
70
+ if (manuallyClosed) return
71
+ if (attempt >= maxAttempts) {
72
+ setStatus("failed")
73
+ return
74
+ }
75
+ const delay = backoffMs[Math.min(attempt, backoffMs.length - 1)] ?? 30000
76
+ attempt += 1
77
+ timer = setTimeout(connect, delay)
78
+ }
79
+
80
+ const connect = (): void => {
81
+ if (manuallyClosed) return
82
+ setStatus("connecting")
83
+ socket = new WebSocket(url)
84
+ socket.addEventListener("open", () => {
85
+ attempt = 0
86
+ connected = true
87
+ setStatus("open")
88
+ const hello = helloMessageSchema.parse({
89
+ type: "hello",
90
+ version: protocolVersion,
91
+ ...(token !== undefined ? { token } : {}),
92
+ })
93
+ socket?.send(JSON.stringify(hello))
94
+ flushPending()
95
+ if (subscribedChannels.size > 0) {
96
+ const msg: WsMessage = {
97
+ type: "subscribe-channels",
98
+ channels: [...subscribedChannels],
99
+ }
100
+ socket?.send(JSON.stringify(msg))
101
+ }
102
+ })
103
+ socket.addEventListener("message", (event: MessageEvent) => {
104
+ try {
105
+ const parsed = wsMessageSchema.safeParse(JSON.parse(String(event.data)))
106
+ if (parsed.success) {
107
+ onMessage?.(parsed.data)
108
+ if (parsed.data.type === "state") {
109
+ for (const [channel, payload] of Object.entries(
110
+ parsed.data.channels,
111
+ )) {
112
+ ChannelRegistry.instance().publish(channel, payload)
113
+ }
114
+ }
115
+ }
116
+ } catch {
117
+ // ignore invalid frames
118
+ }
119
+ })
120
+ socket.addEventListener("close", (event: CloseEvent) => {
121
+ connected = false
122
+ if (event.code === 4001) {
123
+ lastError =
124
+ "Token mismatch — check that SIRENO_TOKEN matches the daemon"
125
+ setStatus("authorization-failed")
126
+ return
127
+ } else if (event.code !== 1000 && event.code !== 0) {
128
+ lastError =
129
+ event.reason?.length > 0
130
+ ? event.reason
131
+ : `WS closed with code ${event.code}`
132
+ }
133
+ setStatus("closed")
134
+ scheduleReconnect()
135
+ })
136
+ socket.addEventListener("error", () => {
137
+ lastError = "WebSocket connection error"
138
+ socket?.close()
139
+ })
140
+ }
141
+
142
+ const close = (): void => {
143
+ manuallyClosed = true
144
+ connected = false
145
+ pendingMessages.length = 0
146
+ subscribedChannels.clear()
147
+ if (timer !== null) clearTimeout(timer)
148
+ if (socket === null) return
149
+ if (
150
+ socket.readyState === WebSocket.CONNECTING ||
151
+ socket.readyState === WebSocket.OPEN
152
+ ) {
153
+ socket.close()
154
+ }
155
+ }
156
+
157
+ const send = (message: WsMessage): void => {
158
+ if (socket?.readyState === WebSocket.OPEN) {
159
+ socket.send(JSON.stringify(message))
160
+ } else {
161
+ pendingMessages.push(message)
162
+ }
163
+ }
164
+
165
+ const subscribeChannels = (channels: string[]): void => {
166
+ const newChannels = channels.filter((ch) => !subscribedChannels.has(ch))
167
+ if (newChannels.length === 0) return
168
+ for (const ch of newChannels) subscribedChannels.add(ch)
169
+ if (socket?.readyState === WebSocket.OPEN) {
170
+ const msg: WsMessage = {
171
+ type: "subscribe-channels",
172
+ channels: newChannels,
173
+ }
174
+ socket.send(JSON.stringify(msg))
175
+ }
176
+ }
177
+
178
+ return {
179
+ connect,
180
+ close,
181
+ send,
182
+ subscribeChannels,
183
+ getAttempt: () => attempt,
184
+ getLastError: () => lastError,
185
+ }
186
+ }
@@ -0,0 +1,31 @@
1
+ import { useCallback } from "react"
2
+
3
+ import type { ButtonActionMessage } from "@/api/protocol-internal"
4
+
5
+ import { useWebSocketSend } from "./ws-context"
6
+
7
+ export interface UseButtonActionReturn {
8
+ readonly fire: (gesture?: ButtonActionMessage["gesture"]) => void
9
+ }
10
+
11
+ export const useButtonAction = (
12
+ deckId: string,
13
+ position: number,
14
+ ): UseButtonActionReturn => {
15
+ const send = useWebSocketSend()
16
+
17
+ const fire = useCallback(
18
+ (gesture: ButtonActionMessage["gesture"] = "tap") => {
19
+ if (send === null) return
20
+ send({
21
+ type: "button-action",
22
+ deckId,
23
+ position,
24
+ gesture,
25
+ })
26
+ },
27
+ [send, deckId, position],
28
+ )
29
+
30
+ return { fire }
31
+ }
@@ -0,0 +1,24 @@
1
+ import { createContext, useContext, type ReactNode } from "react"
2
+
3
+ import type { WsMessage } from "@/api/protocol-internal"
4
+
5
+ export type WebSocketSend = (message: WsMessage) => void
6
+
7
+ const WebSocketContext = createContext<WebSocketSend | null>(null)
8
+
9
+ export interface WebSocketProviderProps {
10
+ readonly value: WebSocketSend | null
11
+ readonly children: ReactNode
12
+ }
13
+
14
+ export const WebSocketProvider = ({
15
+ value,
16
+ children,
17
+ }: WebSocketProviderProps) => (
18
+ <WebSocketContext.Provider value={value}>
19
+ {children}
20
+ </WebSocketContext.Provider>
21
+ )
22
+
23
+ export const useWebSocketSend = (): WebSocketSend | null =>
24
+ useContext(WebSocketContext)