Skip to content

Commit 7385c2b

Browse files
committed
add missing modal
1 parent 726f32b commit 7385c2b

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

ui/src/components/ActionBar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import WakeOnLanModal from "@/components/popovers/WakeOnLan/Index";
1919
import MountPopopover from "@/components/popovers/MountPopover";
2020
import ExtensionPopover from "@/components/popovers/ExtensionPopover";
2121
import { useDeviceUiNavigation } from "@/hooks/useAppNavigation";
22+
2223
import OCRModal from "./popovers/OCRModal";
2324

2425
export default function Actionbar({
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import { useCallback, useMemo, useState } from "react";
2+
import { LuCornerDownLeft } from "react-icons/lu";
3+
import { ExclamationCircleIcon } from "@heroicons/react/16/solid";
4+
import { type LoggerMessage } from "tesseract.js";
5+
6+
import { Button } from "@components/Button";
7+
import { GridCard } from "@components/Card";
8+
import { TextAreaWithLabel } from "@components/TextArea";
9+
import { SettingsPageHeader } from "@components/SettingsPageheader";
10+
import { useUiStore } from "@/hooks/stores";
11+
import useOCR from "@/hooks/useOCR";
12+
import { cx } from "@/cva.config";
13+
14+
export default function OCRModal({ videoElmRef }: { videoElmRef: React.RefObject<HTMLVideoElement> }) {
15+
const { setDisableVideoFocusTrap } = useUiStore();
16+
const [ocrStatus, setOcrStatus] = useState<LoggerMessage>();
17+
const [ocrError, setOcrError] = useState<string | null>(null);
18+
const handleOcrError = useCallback((error: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any
19+
if (typeof error === "string") {
20+
setOcrError(error);
21+
} else {
22+
setOcrError(error.message);
23+
}
24+
}, [setOcrError]);
25+
const [ocrText, setOcrText] = useState<string | null>(null);
26+
27+
const { ocrImage } = useOCR();
28+
29+
const onConfirmOCR = useCallback(async () => {
30+
setDisableVideoFocusTrap(true);
31+
32+
setOcrText(null);
33+
setOcrError(null);
34+
setOcrStatus(undefined);
35+
36+
if (!videoElmRef.current) {
37+
setOcrError("Video element not found");
38+
return;
39+
}
40+
41+
setOcrStatus({
42+
status: "Capturing image",
43+
progress: 0,
44+
jobId: "",
45+
userJobId: "",
46+
workerId: "",
47+
});
48+
49+
// create a canvas from the video element then capture the image from the canvas
50+
const video = videoElmRef.current;
51+
const canvas = document.createElement("canvas");
52+
canvas.width = video.videoWidth;
53+
canvas.height = video.videoHeight;
54+
55+
console.log(video.width, video.height, video)
56+
57+
const ctx = canvas.getContext("2d");
58+
ctx?.drawImage(video, 0, 0, canvas.width, canvas.height);
59+
60+
const text = await ocrImage(["eng"], canvas, { logger: setOcrStatus, errorHandler: handleOcrError });
61+
setOcrText(text);
62+
63+
setOcrStatus(undefined);
64+
setOcrError(null);
65+
}, [
66+
videoElmRef,
67+
setDisableVideoFocusTrap,
68+
ocrImage,
69+
setOcrStatus,
70+
setOcrError,
71+
handleOcrError,
72+
]);
73+
74+
const ocrProgress = useMemo(() => {
75+
if (!ocrStatus?.progress) return 0;
76+
return Math.round(ocrStatus?.progress * 100);
77+
}, [ocrStatus]);
78+
79+
return (
80+
<GridCard>
81+
<div className="space-y-4 p-4 py-3">
82+
<div className="grid h-full grid-rows-(--grid-headerBody)">
83+
<div className="h-full space-y-4">
84+
<div className="space-y-4">
85+
<SettingsPageHeader
86+
title="OCR"
87+
description="OCR text from the video"
88+
/>
89+
90+
<div
91+
className={cx("animate-fadeIn space-y-2 opacity-0", ocrText === null ? "hidden" : "")}
92+
style={{
93+
animationDuration: "0.7s",
94+
animationDelay: "0.1s",
95+
}}
96+
>
97+
<div>
98+
<div
99+
className="w-full"
100+
onKeyUp={e => e.stopPropagation()}
101+
onKeyDown={e => e.stopPropagation()}
102+
>
103+
<TextAreaWithLabel
104+
value={ocrText || ""}
105+
label="Text"
106+
rows={4}
107+
readOnly
108+
spellCheck={false}
109+
data-lt="false"
110+
data-gram="false"
111+
className="font-mono"
112+
/>
113+
</div>
114+
</div>
115+
</div>
116+
117+
{ocrStatus && <div className={cx("animate-fadeIn space-y-2 opacity-0")}
118+
style={{
119+
animationDuration: "0.7s",
120+
animationDelay: "0.1s",
121+
}}
122+
>
123+
<div className="space-y-1 flex justify-between">
124+
<p className="text-xs text-slate-600 dark:text-slate-400 capitalize">
125+
{ocrStatus?.status}
126+
</p>
127+
<span className="text-xs text-slate-600 dark:text-slate-400">{ocrProgress}%</span>
128+
</div>
129+
<div className="h-2.5 w-full overflow-hidden rounded-full bg-slate-300">
130+
<div
131+
style={{ width: ocrProgress + "%" }}
132+
className="h-2.5 bg-blue-700 transition-all duration-1000 ease-in-out"
133+
></div>
134+
</div>
135+
</div>}
136+
137+
{ocrError && (
138+
<div className="flex items-center gap-x-2">
139+
<ExclamationCircleIcon className="h-4 w-4 text-red-500 dark:text-red-400" />
140+
<span className="text-xs text-red-500 dark:text-red-400">
141+
{ocrError}
142+
</span>
143+
</div>
144+
)}
145+
</div>
146+
</div>
147+
</div>
148+
<div className="gap-y-4">
149+
<p className="text-xs text-slate-600 dark:text-slate-400">
150+
Internet connectivity might be required to download Tesseract OCR trained data.
151+
</p>
152+
</div>
153+
<div className="flex animate-fadeIn items-center justify-end gap-x-2 opacity-0"
154+
style={{
155+
animationDuration: "0.7s",
156+
animationDelay: "0.2s",
157+
}}
158+
>
159+
<Button
160+
size="SM"
161+
theme="primary"
162+
text="Start OCR"
163+
onClick={onConfirmOCR}
164+
LeadingIcon={LuCornerDownLeft}
165+
/>
166+
</div>
167+
</div>
168+
</GridCard>
169+
);
170+
}

0 commit comments

Comments
 (0)