Skip to content

Commit

Permalink
refactor: safer types
Browse files Browse the repository at this point in the history
To avoid issues like #3990

This ensures that the type and the array always have the same keys

However, simply adding an extra property on `SettingsValues`
will not cause errors, it will simply keep
the `SettingsStoreState` type unchanged.
But any runtime changes (e.g. adding or accessing a property
without also adding it to the array, or vice versa) will be caught.
  • Loading branch information
WofWca committed Jul 1, 2024
1 parent 3f0bd33 commit 22526ae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- use 'Info' and 'Message Info' consistently #3961
- consolidate 'Profile' wording #3963
- Update local help (2024-06-19)
- refactor: safer types #3993

### Fixed
- Fix crash on "Settings" click when not on main screen (e.g. no account selected): hide the "settings" button
Expand Down
44 changes: 23 additions & 21 deletions src/renderer/stores/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,32 @@ export interface SettingsStoreState {
accountId: number
selfContact: Type.Contact
settings: {
sentbox_watch: string
mvbox_move: string
e2ee_enabled: string
addr: string
displayname: string
selfstatus: string
mdns_enabled: string
show_emails: string
bcc_self: string
delete_device_after: string
delete_server_after: string
webrtc_instance: string
download_limit: string
only_fetch_mvbox: string
disable_idle: string
media_quality: string
is_chatmail: '0' | '1'
webxdc_realtime_enabled: string
[P in (typeof settingsKeys)[number]]: {
sentbox_watch: string
mvbox_move: string
e2ee_enabled: string
addr: string
displayname: string
selfstatus: string
mdns_enabled: string
show_emails: string
bcc_self: string
delete_device_after: string
delete_server_after: string
webrtc_instance: string
download_limit: string
only_fetch_mvbox: string
disable_idle: string
media_quality: string
is_chatmail: '0' | '1'
webxdc_realtime_enabled: string
}[P]
}
desktopSettings: DesktopSettingsType
rc: RC_Config
}

const settingsKeys: Array<keyof SettingsStoreState['settings']> = [
const settingsKeys = [
'sentbox_watch',
'mvbox_move',
'e2ee_enabled',
Expand All @@ -52,7 +54,7 @@ const settingsKeys: Array<keyof SettingsStoreState['settings']> = [
'media_quality',
'is_chatmail',
'webxdc_realtime_enabled',
]
] as const

class SettingsStore extends Store<SettingsStoreState | null> {
reducer = {
Expand Down Expand Up @@ -123,7 +125,7 @@ class SettingsStore extends Store<SettingsStoreState | null> {
}
const settings = (await BackendRemote.rpc.batchGetConfig(
accountId,
settingsKeys
settingsKeys as unknown as Array<(typeof settingsKeys)[number]>
)) as SettingsStoreState['settings']
const selfContact = await BackendRemote.rpc.getContact(
accountId,
Expand Down

0 comments on commit 22526ae

Please sign in to comment.