Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove unnecessary debouncing in editor components #316

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/editors/editorsContainer/AgreementData.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import JSONEditor from "../JSONEditor";
import useAppStore from "../../store/store";
import useUndoRedo from "../../components/useUndoRedo";
import { useCallback } from "react";
import { debounce } from "ts-debounce";
import { FaUndo, FaRedo } from "react-icons/fa";

function AgreementData() {
const textColor = useAppStore((state) => state.textColor);
const data = useAppStore((state) => state.data);
const setData = useAppStore((state) => state.setData);
const { value, setValue, undo, redo } = useUndoRedo(data, setData);

const debouncedSetData = useCallback(
debounce((value: string) => {
setData(value);
}, 500),
[setData]
const { value, setValue, undo, redo } = useUndoRedo(
useAppStore((state) => state.data),
setData
);

const handleChange = (value: string | undefined) => {
if (value !== undefined) {
setValue(value);
debouncedSetData(value);
setData(value);
}
};

return (
<div className="column">
<div className="column" >
<div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
<h3 style={{ color: textColor }}>Data</h3>
<div>
Expand All @@ -35,7 +28,7 @@ function AgreementData() {
</div>
</div>
<p style={{ color: textColor }}>
JSON data (an instance of the Concerto model) used to preview output from the template.
JSON data (an instance of the Concerto model) used to preview output from the template.
</p>
<JSONEditor value={value} onChange={handleChange} />
</div>
Expand Down
19 changes: 6 additions & 13 deletions src/editors/editorsContainer/TemplateMarkdown.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
import MarkdownEditor from "../MarkdownEditor";
import useAppStore from "../../store/store";
import useUndoRedo from "../../components/useUndoRedo";
import { useCallback } from "react";
import { debounce } from "ts-debounce";
import { FaUndo, FaRedo } from "react-icons/fa";

function TemplateMarkdown() {
const textColor = useAppStore((state) => state.textColor);
const backgroundColor = useAppStore((state) => state.backgroundColor);
const templateMarkdown = useAppStore((state) => state.templateMarkdown);
const setTemplateMarkdown = useAppStore((state) => state.setTemplateMarkdown);
const { value, setValue, undo, redo } = useUndoRedo(templateMarkdown, setTemplateMarkdown);

const debouncedSetTemplateMarkdown = useCallback(
debounce((value: string) => {
setTemplateMarkdown(value);
}, 500),
[setTemplateMarkdown]
const { value, setValue, undo, redo } = useUndoRedo(
useAppStore((state) => state.templateMarkdown),
setTemplateMarkdown
);

const handleChange = useCallback((value: string | undefined) => {
const handleChange = (value: string | undefined) => {
if (value !== undefined) {
setValue(value);
debouncedSetTemplateMarkdown(value);
setTemplateMarkdown(value);
}
}, [setValue, debouncedSetTemplateMarkdown]);
};

return (
<div className="column" style={{ backgroundColor }}>
Expand Down
15 changes: 4 additions & 11 deletions src/editors/editorsContainer/TemplateModel.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import ConcertoEditor from "../ConcertoEditor";
import useAppStore from "../../store/store";
import useUndoRedo from "../../components/useUndoRedo";
import { useCallback } from "react";
import { debounce } from "ts-debounce";
import { FaUndo, FaRedo } from "react-icons/fa";

function TemplateModel() {
const textColor = useAppStore((state) => state.textColor);
const modelCto = useAppStore((state) => state.modelCto);
const setModelCto = useAppStore((state) => state.setModelCto);
const { value, setValue, undo, redo } = useUndoRedo(modelCto, setModelCto);

const debouncedSetModelCto = useCallback(
debounce((value: string) => {
setModelCto(value);
}, 500),
[setModelCto]
const { value, setValue, undo, redo } = useUndoRedo(
useAppStore((state) => state.modelCto),
setModelCto
);

const handleChange = (value: string | undefined) => {
if (value !== undefined) {
setValue(value);
debouncedSetModelCto(value);
setModelCto(value);
}
};

Expand Down