@@ -4,11 +4,13 @@ import { titleCase } from 'title-case'
4
4
import { useMemo } from 'react'
5
5
import { disabledSettings , options , qsOptions } from '../optionsStorage'
6
6
import { hideAllModals , miscUiState } from '../globalState'
7
+ import { reloadChunksAction } from '../controls'
7
8
import Button from './Button'
8
9
import Slider from './Slider'
9
10
import Screen from './Screen'
10
11
import { showOptionsModal } from './SelectOption'
11
12
import PixelartIcon , { pixelartIcons } from './PixelartIcon'
13
+ import { reconnectReload } from './AppStatusProvider'
12
14
13
15
type GeneralItem < T extends string | number | boolean > = {
14
16
id ?: string
@@ -18,7 +20,8 @@ type GeneralItem<T extends string | number | boolean> = {
18
20
tooltip ?: string
19
21
// description?: string
20
22
enableWarning ?: string
21
- willHaveNoEffect ?: boolean
23
+ requiresRestart ?: boolean
24
+ requiresChunksReload ?: boolean
22
25
values ?: Array < T | [ T , string ] >
23
26
disableIf ?: [ option : keyof typeof options , value : any ]
24
27
}
@@ -56,7 +59,14 @@ const useCommonComponentsProps = (item: OptionMeta) => {
56
59
}
57
60
}
58
61
59
- export const OptionButton = ( { item } : { item : Extract < OptionMeta , { type : 'toggle' } > } ) => {
62
+ const ignoreReloadWarningsCache = new Set < string > ( )
63
+
64
+ export const OptionButton = ( { item, onClick, valueText, cacheKey } : {
65
+ item : Extract < OptionMeta , { type : 'toggle' } > ,
66
+ onClick ?: ( ) => void ,
67
+ valueText ?: string ,
68
+ cacheKey ?: string ,
69
+ } ) => {
60
70
const { disabledBecauseOfSetting } = useCommonComponentsProps ( item )
61
71
62
72
const optionValue = useSnapshot ( options ) [ item . id ! ]
@@ -84,40 +94,63 @@ export const OptionButton = ({ item }: { item: Extract<OptionMeta, { type: 'togg
84
94
85
95
return < Button
86
96
data-setting = { item . id }
87
- label = { `${ item . text } : ${ valuesTitlesMap [ optionValue ] } ` }
88
- // label={`${item.text}:`}
89
- // postLabel={valuesTitlesMap[optionValue]}
97
+ label = { `${ translate ( item . text ) } : ${ translate ( valueText ?? valuesTitlesMap [ optionValue ] ) } ` }
90
98
onClick = { async ( event ) => {
91
99
if ( disabledReason ) {
92
- await showOptionsModal ( `The option is unavailable. ${ disabledReason } ` , [ ] )
100
+ await showOptionsModal ( `${ translate ( ' The option is not available' ) } : ${ disabledReason } ` , [ ] )
93
101
return
94
102
}
95
103
if ( item . enableWarning && ! options [ item . id ! ] ) {
96
104
const result = await showOptionsModal ( item . enableWarning , [ 'Enable' ] )
97
105
if ( ! result ) return
98
106
}
99
- const { values } = item
100
- if ( values ) {
101
- const getOptionValue = ( arrItem ) => {
102
- if ( typeof arrItem === 'string' ) {
103
- return arrItem
107
+ onClick ?.( )
108
+ if ( item . id ) {
109
+ const { values } = item
110
+ if ( values ) {
111
+ const getOptionValue = ( arrItem ) => {
112
+ if ( typeof arrItem === 'string' ) {
113
+ return arrItem
114
+ } else {
115
+ return arrItem [ 0 ]
116
+ }
117
+ }
118
+ const currentIndex = values . findIndex ( ( value ) => {
119
+ return getOptionValue ( value ) === optionValue
120
+ } )
121
+ if ( currentIndex === - 1 ) {
122
+ options [ item . id ] = getOptionValue ( values [ 0 ] )
104
123
} else {
105
- return arrItem [ 0 ]
124
+ const nextIndex = event . shiftKey
125
+ ? ( currentIndex - 1 + values . length ) % values . length
126
+ : ( currentIndex + 1 ) % values . length
127
+ options [ item . id ] = getOptionValue ( values [ nextIndex ] )
106
128
}
107
- }
108
- const currentIndex = values . findIndex ( ( value ) => {
109
- return getOptionValue ( value ) === optionValue
110
- } )
111
- if ( currentIndex === - 1 ) {
112
- options [ item . id ! ] = getOptionValue ( values [ 0 ] )
113
129
} else {
114
- const nextIndex = event . shiftKey
115
- ? ( currentIndex - 1 + values . length ) % values . length
116
- : ( currentIndex + 1 ) % values . length
117
- options [ item . id ! ] = getOptionValue ( values [ nextIndex ] )
130
+ options [ item . id ] = ! options [ item . id ]
131
+ }
132
+ }
133
+
134
+ const toCacheKey = cacheKey ?? item . id ?? ''
135
+ if ( toCacheKey && ! ignoreReloadWarningsCache . has ( toCacheKey ) ) {
136
+ ignoreReloadWarningsCache . add ( toCacheKey )
137
+
138
+ if ( item . requiresRestart ) {
139
+ const result = await showOptionsModal ( translate ( 'The option requires a restart to take effect' ) , [ 'Restart' , 'I will do it later' ] , {
140
+ cancel : false ,
141
+ } )
142
+ if ( result ) {
143
+ reconnectReload ( )
144
+ }
145
+ }
146
+ if ( item . requiresChunksReload ) {
147
+ const result = await showOptionsModal ( translate ( 'The option requires a chunks reload to take effect' ) , [ 'Reload' , 'I will do it later' ] , {
148
+ cancel : false ,
149
+ } )
150
+ if ( result ) {
151
+ reloadChunksAction ( )
152
+ }
118
153
}
119
- } else {
120
- options [ item . id ! ] = ! options [ item . id ! ]
121
154
}
122
155
} }
123
156
title = { disabledReason ? `${ disabledReason } | ${ item . tooltip } ` : item . tooltip }
0 commit comments