Skip to content

Commit 3fe253e

Browse files
committed
Debounce qrcode generator
1 parent 63d4c20 commit 3fe253e

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/components/md-to-html/MarkdownToHtml.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ipcRenderer } from 'electron';
55

66
const Md2Html = () => {
77
const [md, setMd] = useState('# Hello\n> This is a quote');
8-
const [preview, setPreview] = useState(false);
8+
const [preview, setPreview] = useState(true);
99
const [opening, setOpening] = useState(false);
1010

1111
const handleChange = (evt: { target: { value: string } }) =>

src/components/qrcode/QrCode.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import { ipcRenderer } from 'electron';
2-
import React, { useEffect, useState } from 'react';
2+
import React, { useState } from 'react';
3+
import { useDebouncedEffect } from '../../helpers/effectHooks';
34

45
const HtmlPreview = () => {
56
const [content, setContent] = useState('https://plainbelt.github.io');
67
const [qrCode, setQrCode] = useState();
78
const [opening, setOpening] = useState(false);
89
const [saving, setSaving] = useState(false);
910

10-
useEffect(() => {
11-
ipcRenderer
12-
.invoke('generate-qrcode', { content })
13-
.then((qr) => setQrCode(qr))
14-
.catch(() => {});
15-
});
11+
useDebouncedEffect(
12+
() => {
13+
ipcRenderer
14+
.invoke('generate-qrcode', { content })
15+
.then((qr) => setQrCode(qr))
16+
.catch(() => {});
17+
},
18+
[content],
19+
500
20+
);
1621

1722
const handleChange = async (evt: { target: { value: string } }) => {
1823
setContent(evt.target.value);
19-
const qr = await ipcRenderer.invoke('generate-qrcode', { content });
20-
setQrCode(qr);
2124
};
2225

2326
const handleOpen = async () => {
@@ -57,16 +60,16 @@ const HtmlPreview = () => {
5760
Save...
5861
</button>
5962
</div>
60-
<div className="flex min-h-full">
63+
<div className="flex flex-1 min-h-full">
6164
<textarea
6265
onChange={handleChange}
63-
className="flex-1 min-h-full bg-white p-4"
66+
className="flex-1 min-h-full bg-white p-4 rounded-md"
6467
value={content}
6568
/>
6669
<div className="mx-1" />
6770
{qrCode && (
68-
<section className="flex-1 min-h-full flex items-center p-4 prose bg-white">
69-
<img src={qrCode} alt={content} className="w-full h-full" />
71+
<section className="flex-1 min-h-full flex items-center p-4 prose bg-white rounded-md">
72+
<img src={qrCode} alt={content} />
7073
</section>
7174
)}
7275
</div>

src/helpers/effectHooks.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useEffect } from 'react';
2+
3+
export const useDebouncedEffect = (
4+
effect: any,
5+
deps: Array<any>,
6+
delay: number
7+
) => {
8+
useEffect(() => {
9+
const handler = setTimeout(() => effect(), delay);
10+
11+
return () => clearTimeout(handler);
12+
// eslint-disable-next-line react-hooks/exhaustive-deps
13+
}, [...(deps || []), delay]);
14+
};
15+
16+
export default { useDebouncedEffect };

0 commit comments

Comments
 (0)