-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web socket interface supports 'load-subtitles'
- Loading branch information
1 parent
3f60909
commit cd662f0
Showing
17 changed files
with
586 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,25 @@ | ||
import { AnkiSettings, WebSocketClientSettings, ankiSettingsKeys } from '../../settings'; | ||
import { WebSocketClientSettings } from '../../settings'; | ||
import { CardTextFieldValues, PostMineAction } from '../../src/model'; | ||
import { MineSubtitleCommand, WebSocketClient } from '../../web-socket-client'; | ||
import { WebSocketClient } from '../../web-socket-client'; | ||
import { useEffect, useState } from 'react'; | ||
import { useDocumentHasFocus } from './use-document-has-focus'; | ||
|
||
export interface MineSubtitleParams extends CardTextFieldValues { | ||
postMineAction: PostMineAction; | ||
} | ||
|
||
export const useAppWebSocketClient = ({ | ||
onMineSubtitle, | ||
settings, | ||
enabled, | ||
}: { | ||
onMineSubtitle: (params: MineSubtitleParams) => boolean; | ||
settings: WebSocketClientSettings & AnkiSettings; | ||
enabled: boolean; | ||
}) => { | ||
export const useAppWebSocketClient = ({ settings }: { settings: WebSocketClientSettings }) => { | ||
const [client, setClient] = useState<WebSocketClient>(); | ||
const documentHasFocus = useDocumentHasFocus(); | ||
|
||
useEffect(() => { | ||
if (enabled && settings.webSocketClientEnabled && settings.webSocketServerUrl && documentHasFocus) { | ||
if (settings.webSocketClientEnabled && settings.webSocketServerUrl) { | ||
const client = new WebSocketClient(); | ||
client.bind(settings.webSocketServerUrl).catch(console.error); | ||
setClient(client); | ||
return () => client.unbind(); | ||
} | ||
|
||
setClient(undefined); | ||
}, [documentHasFocus, settings.webSocketServerUrl, settings.webSocketClientEnabled, enabled]); | ||
}, [settings.webSocketServerUrl, settings.webSocketClientEnabled]); | ||
|
||
useEffect(() => { | ||
if (!client) { | ||
return; | ||
} | ||
|
||
client.onMineSubtitle = async ({ | ||
body: { fields: receivedFields, postMineAction: receivedPostMineAction }, | ||
}: MineSubtitleCommand) => { | ||
const fields = receivedFields ?? {}; | ||
const word = fields[settings.wordField] || undefined; | ||
const definition = fields[settings.definitionField] || undefined; | ||
const text = fields[settings.sentenceField] || undefined; | ||
const customFieldValues = Object.fromEntries( | ||
Object.entries(settings.customAnkiFields) | ||
.map(([asbplayerFieldName, ankiFieldName]) => { | ||
const fieldValue = fields[ankiFieldName]; | ||
|
||
if (fieldValue === undefined) { | ||
return undefined; | ||
} | ||
|
||
return [asbplayerFieldName, fieldValue]; | ||
}) | ||
.filter((entry) => entry !== undefined) as string[][] | ||
); | ||
const postMineAction = receivedPostMineAction ?? PostMineAction.showAnkiDialog; | ||
return onMineSubtitle({ postMineAction, text, word, definition, customFieldValues }); | ||
}; | ||
}, [ | ||
client, | ||
settings.wordField, | ||
settings.definitionField, | ||
settings.sentenceField, | ||
settings.customAnkiFields, | ||
onMineSubtitle, | ||
]); | ||
return client; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.