Skip to content

Commit aee51a0

Browse files
committed
fix: prevent full body re-render when selecting a sample from Load Sample dropdown
Signed-off-by: surya4419 <[email protected]>
1 parent bd2f8da commit aee51a0

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/AgreementHtml.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { Spin } from "antd";
44
import useAppStore from "./store/store";
55
import FullScreenModal from "./components/FullScreenModal";
66

7-
function AgreementHtml({ loading, isModal }: { loading: boolean; isModal?: boolean }) {
7+
function AgreementHtml({ isModal }: { isModal?: boolean; loading?: boolean }) {
88
const agreementHtml = useAppStore((state) => state.agreementHtml);
99
const backgroundColor = useAppStore((state) => state.backgroundColor);
1010
const textColor = useAppStore((state) => state.textColor);
11+
const isLoading = useAppStore((state) => state.isLoading);
1112

1213
return (
1314
<div
@@ -38,9 +39,9 @@ function AgreementHtml({ loading, isModal }: { loading: boolean; isModal?: boole
3839
<p style={{ textAlign: "center", color: textColor }}>
3940
The result of merging the JSON data with the template.
4041
</p>
41-
{loading ? (
42-
<div style={{ flex: 1, display: "flex", justifyContent: "center", alignItems: "center" }}>
43-
<Spin indicator={<LoadingOutlined style={{ fontSize: 42, color: "#19c6c7" }} spin />} />
42+
{isLoading ? (
43+
<div style={{ flex: 1, display: 'flex', justifyContent: 'center', alignItems: 'center', position: 'relative' }}>
44+
<Spin indicator={<LoadingOutlined style={{ fontSize: 42, color: '#19c6c7' }} spin />} />
4445
</div>
4546
) : (
4647
<div

src/store/store.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface AppState {
2323
sampleName: string;
2424
backgroundColor: string;
2525
textColor: string;
26+
isLoading: boolean;
2627
setTemplateMarkdown: (template: string) => Promise<void>;
2728
setEditorValue: (value: string) => void;
2829
setModelCto: (model: string) => Promise<void>;
@@ -44,8 +45,6 @@ export interface DecompressedData {
4445
agreementHtml: string;
4546
}
4647

47-
const rebuildDeBounce = debounce(rebuild, 500);
48-
4948
async function rebuild(template: string, model: string, dataString: string) {
5049
const modelManager = new ModelManager({ strict: true });
5150
modelManager.addCTOModel(model, undefined, true);
@@ -69,6 +68,11 @@ async function rebuild(template: string, model: string, dataString: string) {
6968
);
7069
}
7170

71+
const rebuildDeBounce = debounce(async (template: string, model: string, data: string) => {
72+
const result = await rebuild(template, model, data);
73+
return result;
74+
}, 500);
75+
7276
const useAppStore = create<AppState>()(
7377
immer(
7478
devtools((set, get) => ({
@@ -83,6 +87,7 @@ const useAppStore = create<AppState>()(
8387
editorAgreementData: JSON.stringify(playground.DATA, null, 2),
8488
agreementHtml: "",
8589
error: undefined,
90+
isLoading: false,
8691
samples: SAMPLES,
8792
init: async () => {
8893
const params = new URLSearchParams(window.location.search);
@@ -108,16 +113,21 @@ const useAppStore = create<AppState>()(
108113
editorAgreementData: JSON.stringify(sample.DATA, null, 2),
109114
}));
110115
get().rebuild();
111-
112116
}
113117
},
114118
rebuild: async () => {
115-
const { templateMarkdown, modelCto, data } = get();
119+
set(() => ({ isLoading: true }));
116120
try {
117-
const result = await rebuildDeBounce(templateMarkdown, modelCto, data);
121+
const result = await rebuildDeBounce(
122+
get().templateMarkdown,
123+
get().modelCto,
124+
get().data
125+
);
118126
set(() => ({ agreementHtml: result, error: undefined }));
119127
} catch (error: any) {
120128
set(() => ({ error: error instanceof Error ? error.message : 'Unknown error' }));
129+
} finally {
130+
set(() => ({ isLoading: false }));
121131
}
122132
},
123133
setTemplateMarkdown: async (template: string) => {

0 commit comments

Comments
 (0)