forked from nassan/sefaria-llm-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.tsx
113 lines (100 loc) · 3.35 KB
/
plugin.tsx
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
110
111
112
113
import React, { useState, useEffect, useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SoferAI, SoferAIClient } from 'soferai';
enum Status {
Idle,
Loading,
Finished,
Error
}
export default function SefariaPlugin({ sref }: { sref?: string }) {
const [status, setStatus] = useState<Status>(Status.Idle);
const [displayText, setDisplayText] = useState('');
const [apiKey, setApiKey] = useState('');
const [transcriptionId, setTranscriptionId] = useState('');
const [soferAiClient, setSoferAiClient] = useState<SoferAIClient | null>(null);
useEffect(() => {
const newClient = apiKey ? new SoferAIClient({ apiKey }) : new SoferAIClient();
setSoferAiClient(newClient);
}, [apiKey]);
useEffect(() => {
if (soferAiClient && transcriptionId) {
fetchTranscript();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [soferAiClient, transcriptionId]);
async function fetchTranscript() {
setStatus(Status.Loading);
setDisplayText('');
try {
const transcripton = (await soferAiClient?.transcribe.getTranscription(transcriptionId))?.text
setDisplayText(transcripton || '');
setStatus(Status.Finished);
} catch (error) {
setStatus(Status.Error);
}
}
return (
<div style={{ fontFamily: 'Arial, sans-serif', fontSize: '18px' }}>
<div style={{
marginTop: '1em',
backgroundColor: 'rgba(0,0,0,0.05)',
padding: '1em',
borderRadius: '1em'
}}>
{status === Status.Loading && <h3>🤖 is loading...</h3>}
{status === Status.Error && (
<>
<h3>🤖 🤒</h3>
<small>Whoops! Something went wrong.</small>
</>
)}
{(status === Status.Finished) && (
<>
<p>Ref: {displayText}</p>
</>
)}
</div>
<form onSubmit={(e) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const key = formData.get('apiKey') as string;
const url = formData.get('transcriptionId') as string;
setApiKey(key);
setTranscriptionId(url);
}}>
<h4>Sofer.Ai Settings</h4>
<div>
<label>API Key:<input name="apiKey" type="text" defaultValue={apiKey} /></label>
</div>
<div>
<label>Transcription Id:<input name="transcriptionId" type="text" defaultValue={transcriptionId} /></label>
</div>
<button type="submit">Update</button>
</form>
<div style={{ fontSize: '60%' }}>Ref: {sref}</div>
</div>
);
}
// Register as a web component
class SefariaPluginElement extends HTMLElement {
static get observedAttributes() {
return ['sref'];
}
private _root: any; // store the persistent React root
connectedCallback() {
const sref = this.getAttribute('sref') || undefined;
// Create shadow DOM and persistent React root if needed
if (!this.shadowRoot) this.attachShadow({ mode: 'open' });
if (!this._root) {
this._root = createRoot(this.shadowRoot!);
this._root.render(<SefariaPlugin sref={sref} />);
}
}
attributeChangedCallback(name: string, oldValue: string, newValue: string) {
if (name === 'sref' && newValue !== oldValue && this._root) {
this._root.render(<SefariaPlugin sref={newValue} />);
}
}
}
customElements.define('sefaria-plugin', SefariaPluginElement);