Skip to content

Commit 27db1e5

Browse files
committed
Refactor web configuration editing logic into the use-web-configuration-editing hook
- in settings, handlers should live in `apps/editor/src/views/settings/frontend/hooks/use-web-configuration-editing.ts`, similarly to panel
1 parent e344df8 commit 27db1e5

2 files changed

Lines changed: 63 additions & 52 deletions

File tree

‎apps/editor/src/views/settings/frontend/Settings.tsx‎

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ import { Home, NavItem } from './Home/Home'
66
import { use_web_configuration_editing } from './hooks/use-web-configuration-editing'
77
import { Modal as UiModal } from '@ui/components/editor/settings/Modal'
88
import { EditWebConfigurationForm } from '@/views/shared/components/EditWebConfigurationForm'
9-
import { WebConfiguration } from '@shared/types/web-configuration'
109

1110
const vscode = acquireVsCodeApi()
1211

1312
export const Settings = () => {
1413
const settings_hook = use_settings(vscode)
15-
const { updating_web_configuration, set_updating_web_configuration } =
16-
use_web_configuration_editing()
14+
const {
15+
updating_web_configuration,
16+
set_updating_web_configuration,
17+
set_updated_web_configuration,
18+
edit_web_configuration_cancel_handler,
19+
edit_web_configuration_save_handler
20+
} = use_web_configuration_editing(vscode)
1721
const [scroll_to_section_on_load, set_scroll_to_section_on_load] =
1822
useState<NavItem>()
19-
const [updated_web_configuration, set_updated_web_configuration] = useState<
20-
WebConfiguration | undefined
21-
>()
2223

2324
const all_data_loaded = useMemo(() => {
2425
return (
@@ -46,18 +47,12 @@ export const Settings = () => {
4647
)
4748
}, [settings_hook])
4849

49-
useEffect(() => {
50-
set_updated_web_configuration(updating_web_configuration)
51-
}, [updating_web_configuration])
52-
5350
useEffect(() => {
5451
if (!all_data_loaded) return
5552
post_message(vscode, { command: 'SETTINGS_UI_READY' })
5653
const handle_message = (event: MessageEvent<BackendMessage>) => {
5754
if (event.data.command == 'SHOW_SECTION') {
5855
set_scroll_to_section_on_load(event.data.section as NavItem)
59-
} else if (event.data.command == 'WEB_CONFIGURATION_UPDATED') {
60-
set_updating_web_configuration(undefined)
6156
}
6257
}
6358
window.addEventListener('message', handle_message)
@@ -201,44 +196,11 @@ export const Settings = () => {
201196
scroll_to_section_on_load={scroll_to_section_on_load}
202197
/>
203198
{updating_web_configuration && (
204-
<UiModal
205-
on_close={() => {
206-
if (updated_web_configuration) {
207-
post_message(vscode, {
208-
command: 'UPDATE_WEB_CONFIGURATION',
209-
updating_web_configuration,
210-
updated_web_configuration,
211-
origin: 'cancel'
212-
})
213-
} else {
214-
set_updating_web_configuration(undefined)
215-
}
216-
}}
217-
>
199+
<UiModal on_close={edit_web_configuration_cancel_handler}>
218200
<UiModal.Form
219201
title="Edit Configuration"
220-
on_save={() => {
221-
if (updated_web_configuration) {
222-
post_message(vscode, {
223-
command: 'UPDATE_WEB_CONFIGURATION',
224-
updating_web_configuration,
225-
updated_web_configuration,
226-
origin: 'save'
227-
})
228-
}
229-
}}
230-
on_cancel={() => {
231-
if (updated_web_configuration) {
232-
post_message(vscode, {
233-
command: 'UPDATE_WEB_CONFIGURATION',
234-
updating_web_configuration,
235-
updated_web_configuration,
236-
origin: 'cancel'
237-
})
238-
} else {
239-
set_updating_web_configuration(undefined)
240-
}
241-
}}
202+
on_save={edit_web_configuration_save_handler}
203+
on_cancel={edit_web_configuration_cancel_handler}
242204
>
243205
<EditWebConfigurationForm
244206
web_configuration={updating_web_configuration}
Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
1-
import { useState } from 'react'
1+
import { useState, useEffect } from 'react'
22
import { WebConfiguration } from '@shared/types/web-configuration'
3+
import { BackendMessage } from '../../types/messages'
4+
import { post_message } from '../utils/post-message'
35

4-
export const use_web_configuration_editing = () => {
5-
const [updating_web_configuration, set_updating_web_configuration] = useState<WebConfiguration>()
6+
export const use_web_configuration_editing = (vscode: any) => {
7+
const [updating_web_configuration, set_updating_web_configuration] =
8+
useState<WebConfiguration>()
9+
const [updated_web_configuration, set_updated_web_configuration] =
10+
useState<WebConfiguration>()
11+
12+
useEffect(() => {
13+
set_updated_web_configuration(updating_web_configuration)
14+
}, [updating_web_configuration])
15+
16+
const edit_web_configuration_cancel_handler = () => {
17+
if (updated_web_configuration) {
18+
post_message(vscode, {
19+
command: 'UPDATE_WEB_CONFIGURATION',
20+
updating_web_configuration: updating_web_configuration!,
21+
updated_web_configuration: updated_web_configuration,
22+
origin: 'cancel'
23+
})
24+
} else {
25+
set_updating_web_configuration(undefined)
26+
}
27+
}
28+
29+
const edit_web_configuration_save_handler = () => {
30+
if (updated_web_configuration) {
31+
post_message(vscode, {
32+
command: 'UPDATE_WEB_CONFIGURATION',
33+
updating_web_configuration: updating_web_configuration!,
34+
updated_web_configuration: updated_web_configuration,
35+
origin: 'save'
36+
})
37+
}
38+
}
39+
40+
useEffect(() => {
41+
const handle_message = (event: MessageEvent<BackendMessage>) => {
42+
const message = event.data
43+
if (message.command == 'WEB_CONFIGURATION_UPDATED') {
44+
set_updating_web_configuration(undefined)
45+
set_updated_web_configuration(undefined)
46+
}
47+
}
48+
49+
window.addEventListener('message', handle_message)
50+
return () => window.removeEventListener('message', handle_message)
51+
}, [])
652

753
return {
854
updating_web_configuration,
9-
set_updating_web_configuration
55+
set_updating_web_configuration,
56+
set_updated_web_configuration,
57+
edit_web_configuration_cancel_handler,
58+
edit_web_configuration_save_handler
1059
}
1160
}

0 commit comments

Comments
 (0)