@@ -3,11 +3,10 @@ import { AppHeader } from "./AppHeader";
33import {
44 call_rpc ,
55 create_rpc_connection ,
6- RpcConnection ,
76} from "@zmkfirmware/zmk-studio-ts-client" ;
87import type { Notification } from "@zmkfirmware/zmk-studio-ts-client/studio" ;
9- import { ConnectionContext } from "./rpc/ConnectionContext" ;
10- import { Dispatch , useEffect , useState } from "react" ;
8+ import { ConnectionState , ConnectionContext } from "./rpc/ConnectionContext" ;
9+ import { Dispatch , useCallback , useEffect , useState } from "react" ;
1110import { ConnectModal , TransportFactory } from "./ConnectModal" ;
1211
1312import type { RpcTransport } from "@zmkfirmware/zmk-studio-ts-client/transport/index" ;
@@ -115,13 +114,13 @@ async function listen_for_notifications(
115114
116115async function connect (
117116 transport : RpcTransport ,
118- setConn : Dispatch < RpcConnection | null > ,
117+ setConn : Dispatch < ConnectionState > ,
119118 setConnectedDeviceName : Dispatch < string | undefined >
120119) {
121- let rpc_conn = await create_rpc_connection ( transport ) ;
120+ let conn = await create_rpc_connection ( transport ) ;
122121
123122 let details = await Promise . race ( [
124- call_rpc ( rpc_conn , { core : { getDeviceInfo : true } } )
123+ call_rpc ( conn , { core : { getDeviceInfo : true } } )
125124 . then ( ( r ) => r ?. core ?. getDeviceInfo )
126125 . catch ( ( e ) => {
127126 console . error ( "Failed first RPC call" , e ) ;
@@ -136,22 +135,22 @@ async function connect(
136135 return ;
137136 }
138137
139- listen_for_notifications ( rpc_conn . notification_readable )
138+ listen_for_notifications ( conn . notification_readable )
140139 . then ( ( ) => {
141140 setConnectedDeviceName ( undefined ) ;
142- setConn ( null ) ;
141+ setConn ( { conn : null } ) ;
143142 } )
144143 . catch ( ( _e ) => {
145144 setConnectedDeviceName ( undefined ) ;
146- setConn ( null ) ;
145+ setConn ( { conn : null } ) ;
147146 } ) ;
148147
149148 setConnectedDeviceName ( details . name ) ;
150- setConn ( rpc_conn ) ;
149+ setConn ( { conn } ) ;
151150}
152151
153152function App ( ) {
154- const [ conn , setConn ] = useState < RpcConnection | null > ( null ) ;
153+ const [ conn , setConn ] = useState < ConnectionState > ( { conn : null } ) ;
155154 const [ connectedDeviceName , setConnectedDeviceName ] = useState <
156155 string | undefined
157156 > ( undefined ) ;
@@ -174,11 +173,13 @@ function App() {
174173 }
175174
176175 async function updateLockState ( ) {
177- if ( ! conn ) {
176+ if ( ! conn . conn ) {
178177 return ;
179178 }
180179
181- let locked_resp = await call_rpc ( conn , { core : { getLockState : true } } ) ;
180+ let locked_resp = await call_rpc ( conn . conn , {
181+ core : { getLockState : true } ,
182+ } ) ;
182183
183184 setLockState (
184185 locked_resp . core ?. getLockState ||
@@ -189,13 +190,48 @@ function App() {
189190 updateLockState ( ) ;
190191 } , [ conn , setLockState ] ) ;
191192
193+ const save = useCallback ( ( ) => {
194+ async function doSave ( ) {
195+ if ( ! conn . conn ) {
196+ return ;
197+ }
198+
199+ let resp = await call_rpc ( conn . conn , { keymap : { saveChanges : true } } ) ;
200+ if ( ! resp . keymap ?. saveChanges ) {
201+ console . error ( "Failed to save changes" , resp ) ;
202+ }
203+ }
204+
205+ doSave ( ) ;
206+ } , [ conn ] ) ;
207+
208+ const discard = useCallback ( ( ) => {
209+ async function doDiscard ( ) {
210+ if ( ! conn . conn ) {
211+ return ;
212+ }
213+
214+ let resp = await call_rpc ( conn . conn , {
215+ keymap : { discardChanges : true } ,
216+ } ) ;
217+ if ( ! resp . keymap ?. discardChanges ) {
218+ console . error ( "Failed to discard changes" , resp ) ;
219+ }
220+
221+ reset ( ) ;
222+ setConn ( { conn : conn . conn } ) ;
223+ }
224+
225+ doDiscard ( ) ;
226+ } , [ conn ] ) ;
227+
192228 return (
193229 < ConnectionContext . Provider value = { conn } >
194230 < LockStateContext . Provider value = { lockState } >
195231 < UndoRedoContext . Provider value = { doIt } >
196232 < UnlockModal />
197233 < ConnectModal
198- open = { ! conn }
234+ open = { ! conn . conn }
199235 transports = { TRANSPORTS }
200236 onTransportCreated = { ( t ) =>
201237 connect ( t , setConn , setConnectedDeviceName )
@@ -213,6 +249,8 @@ function App() {
213249 canRedo = { canRedo }
214250 onUndo = { undo }
215251 onRedo = { redo }
252+ onSave = { save }
253+ onDiscard = { discard }
216254 />
217255 < Keyboard />
218256 < AppFooter
0 commit comments