@@ -4,11 +4,13 @@ import { titleCase } from 'title-case'
44import { useMemo } from 'react'
55import { disabledSettings , options , qsOptions } from '../optionsStorage'
66import { hideAllModals , miscUiState } from '../globalState'
7+ import { reloadChunksAction } from '../controls'
78import Button from './Button'
89import Slider from './Slider'
910import Screen from './Screen'
1011import { showOptionsModal } from './SelectOption'
1112import PixelartIcon , { pixelartIcons } from './PixelartIcon'
13+ import { reconnectReload } from './AppStatusProvider'
1214
1315type GeneralItem < T extends string | number | boolean > = {
1416 id ?: string
@@ -18,7 +20,8 @@ type GeneralItem<T extends string | number | boolean> = {
1820 tooltip ?: string
1921 // description?: string
2022 enableWarning ?: string
21- willHaveNoEffect ?: boolean
23+ requiresRestart ?: boolean
24+ requiresChunksReload ?: boolean
2225 values ?: Array < T | [ T , string ] >
2326 disableIf ?: [ option : keyof typeof options , value : any ]
2427}
@@ -56,7 +59,14 @@ const useCommonComponentsProps = (item: OptionMeta) => {
5659 }
5760}
5861
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+ } ) => {
6070 const { disabledBecauseOfSetting } = useCommonComponentsProps ( item )
6171
6272 const optionValue = useSnapshot ( options ) [ item . id ! ]
@@ -84,40 +94,63 @@ export const OptionButton = ({ item }: { item: Extract<OptionMeta, { type: 'togg
8494
8595 return < Button
8696 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 ] ) } ` }
9098 onClick = { async ( event ) => {
9199 if ( disabledReason ) {
92- await showOptionsModal ( `The option is unavailable. ${ disabledReason } ` , [ ] )
100+ await showOptionsModal ( `${ translate ( ' The option is not available' ) } : ${ disabledReason } ` , [ ] )
93101 return
94102 }
95103 if ( item . enableWarning && ! options [ item . id ! ] ) {
96104 const result = await showOptionsModal ( item . enableWarning , [ 'Enable' ] )
97105 if ( ! result ) return
98106 }
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 ] )
104123 } 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 ] )
106128 }
107- }
108- const currentIndex = values . findIndex ( ( value ) => {
109- return getOptionValue ( value ) === optionValue
110- } )
111- if ( currentIndex === - 1 ) {
112- options [ item . id ! ] = getOptionValue ( values [ 0 ] )
113129 } 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+ }
118153 }
119- } else {
120- options [ item . id ! ] = ! options [ item . id ! ]
121154 }
122155 } }
123156 title = { disabledReason ? `${ disabledReason } | ${ item . tooltip } ` : item . tooltip }
0 commit comments