Skip to content

Commit f8009ae

Browse files
authored
chore: improve DocEditor hooks (#537)
1 parent 51690bc commit f8009ae

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

packages/root-cms/ui/components/DocEditor/DocEditor.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
useReducer,
3333
useRef,
3434
useState,
35+
useCallback,
3536
} from 'preact/hooks';
3637
import {route} from 'preact-router';
3738
import * as schema from '../../../core/schema.js';
@@ -106,22 +107,24 @@ export function DocEditor(props: DocEditorProps) {
106107
const collection = useCollectionSchema(props.collection.id);
107108
const draft = props.draft;
108109
const {controller, saveState, data} = draft;
109-
const ref = useRef<HTMLDivElement>(null);
110110
const [deeplink, setDeeplink] = useState('');
111111
const [clipboardValue, setClipboardValue] = useState(null);
112112
const loading = collection.loading || draft.loading;
113113
const fields = collection.schema?.fields || [];
114114

115-
function goBack() {
115+
const goBack = useCallback(() => {
116116
const collectionId = props.docId.split('/')[0];
117117
route(`/cms/content/${collectionId}`);
118-
}
118+
}, [props.docId]);
119119

120-
function onDocAction(event: DocActionEvent) {
121-
if (event.action === 'delete') {
122-
goBack();
123-
}
124-
}
120+
const onDocAction = useCallback(
121+
(event: DocActionEvent) => {
122+
if (event.action === 'delete') {
123+
goBack();
124+
}
125+
},
126+
[goBack]
127+
);
125128

126129
const publishDocModal = usePublishDocModal({docId: props.docId});
127130
const localizationModal = useLocalizationModal({
@@ -147,7 +150,7 @@ export function DocEditor(props: DocEditorProps) {
147150
>
148151
<DOC_DATA_CONTEXT.Provider value={data}>
149152
<DEEPLINK_CONTEXT.Provider value={deeplink}>
150-
<div className="DocEditor" ref={ref}>
153+
<div className="DocEditor">
151154
<LoadingOverlay
152155
visible={loading}
153156
loaderProps={{color: 'gray', size: 'xl'}}
@@ -285,7 +288,7 @@ DocEditor.Field = (props: FieldProps) => {
285288
}
286289
);
287290
return unsubscribe;
288-
}, []);
291+
}, [props.draft, props.deepKey]);
289292

290293
useEffect(() => {
291294
const translate = (field as any).translate;
@@ -821,7 +824,7 @@ DocEditor.ArrayField = (props: FieldProps) => {
821824
}
822825
);
823826
return unsubscribe;
824-
}, []);
827+
}, [props.draft, props.deepKey]);
825828

826829
// Focus the field that was just moved (for hotkey support).
827830
useEffect(() => {
@@ -1159,7 +1162,7 @@ DocEditor.OneOfField = (props: FieldProps) => {
11591162
}
11601163
);
11611164
return unsubscribe;
1162-
}, []);
1165+
}, [props.draft, props.deepKey]);
11631166

11641167
return (
11651168
<div className="DocEditor__OneOfField">

0 commit comments

Comments
 (0)