Skip to content

Commit 030cdce

Browse files
ConorBobbleHatConorBobbleHat
andauthored
Fix switching diff algorithms (#769)
* Fix diff mode option * (almost) all the types! --------- Co-authored-by: ConorBobbleHat <[email protected]>
1 parent d36b363 commit 030cdce

File tree

1 file changed

+63
-40
lines changed

1 file changed

+63
-40
lines changed

frontend/src/components/compiler/CompilerOpts.tsx

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, useContext } from "react"
1+
import { createContext, useContext, ReactElement } from "react"
22

33
import * as api from "@/lib/api"
44
import useTranslation from "@/lib/i18n/translate"
@@ -15,11 +15,14 @@ const NO_TRANSLATION = "NO_TRANSLATION"
1515
interface IOptsContext {
1616
checkFlag(flag: string): boolean
1717
setFlag(flag: string, value: boolean): void
18+
setFlags(edits: {flag: string, value: boolean}[]): void
1819
}
1920

2021
const OptsContext = createContext<IOptsContext>(undefined)
2122

22-
function Checkbox({ flag, description }) {
23+
type CheckboxProps = {flag: string, description: string}
24+
25+
function Checkbox({ flag, description }: CheckboxProps) {
2326
const { checkFlag, setFlag } = useContext(OptsContext)
2427

2528
const isChecked = checkFlag(flag)
@@ -31,7 +34,7 @@ function Checkbox({ flag, description }) {
3134
</div>
3235
}
3336

34-
function DiffCheckbox({ flag, description }) {
37+
function DiffCheckbox({ flag, description }: CheckboxProps) {
3538
const { checkFlag, setFlag } = useContext(OptsContext)
3639

3740
const isChecked = checkFlag(flag)
@@ -42,7 +45,9 @@ function DiffCheckbox({ flag, description }) {
4245
</div>
4346
}
4447

45-
function FlagSet({ name, children, value }) {
48+
type FlagSetProps = {name: string, children: ReactElement<FlagOptionProps>[], value: string};
49+
50+
function FlagSet({ name, children, value }: FlagSetProps) {
4651
const { setFlag } = useContext(OptsContext)
4752

4853
return <div className={styles.flagSet}>
@@ -62,18 +67,20 @@ function FlagSet({ name, children, value }) {
6267
</div>
6368
}
6469

65-
function DiffFlagSet({ name, children, value }) {
66-
const { setFlag } = useContext(OptsContext)
70+
function DiffFlagSet({ name, children, value }: FlagSetProps) {
71+
const { setFlags } = useContext(OptsContext)
6772

6873
return <div className={styles.flagSet}>
6974
<div className={styles.flagSetName}>{name}</div>
7075
<Select
7176
onChange={event => {
72-
for (const child of children) {
73-
setFlag(child.props.flag, false)
74-
}
77+
const trueFlag = (event.target as HTMLSelectElement).value
7578

76-
setFlag((event.target as HTMLSelectElement).value, true)
79+
const edits = children.map(child => {
80+
return { flag: child.props.flag, value: child.props.flag == trueFlag }
81+
})
82+
83+
setFlags(edits)
7784
}}
7885
value={value}
7986
>
@@ -82,13 +89,15 @@ function DiffFlagSet({ name, children, value }) {
8289
</div>
8390
}
8491

85-
function FlagOption({ flag, description }: { flag: string, description?: string }) {
92+
type FlagOptionProps = { flag: string, description?: string };
93+
94+
function FlagOption({ flag, description }: FlagOptionProps) {
8695
return <option value={flag}>
8796
{flag} {description && description !== NO_TRANSLATION && `(${description})`}
8897
</option>
8998
}
9099

91-
function DiffFlagOption({ flag, description }: { flag: string, description?: string }) {
100+
function DiffFlagOption({ flag, description }: FlagOptionProps) {
92101
return <option value={flag}>
93102
{description || flag}
94103
</option>
@@ -195,6 +204,46 @@ export default function CompilerOpts({ platform, value, onChange, diffLabel, onD
195204
})
196205
}
197206

207+
const optsEditorProvider = {
208+
checkFlag(flag: string) {
209+
return (" " + opts + " ").includes(" " + flag + " ")
210+
},
211+
212+
setFlag(flag: string, enable: boolean) {
213+
if (enable) {
214+
opts = opts + " " + flag
215+
} else {
216+
opts = (" " + opts + " ").replace(" " + flag + " ", " ")
217+
}
218+
opts = opts.trim()
219+
setOpts(opts)
220+
},
221+
222+
setFlags(edits: {flag: string, value: boolean}[]) {
223+
edits.forEach(({ flag, value }) => optsEditorProvider.setFlag(flag, value))
224+
},
225+
226+
}
227+
228+
const diffOptsEditorProvider = {
229+
checkFlag(flag: string) {
230+
return diff_opts.includes(flag)
231+
},
232+
233+
setFlag(flag: string, enable: boolean) {
234+
diffOptsEditorProvider.setFlags([{ flag, value: enable }])
235+
},
236+
237+
setFlags(edits: {flag: string, value: boolean}[]) {
238+
const positiveEdits = edits.filter(o => o.value).map(o => o.flag)
239+
const negativeEdits = edits.filter(o => !o.value).map(o => o.flag)
240+
241+
const negativeState = diff_opts.filter(o => !negativeEdits.includes(o))
242+
243+
setDiffOpts([...negativeState, ...positiveEdits])
244+
},
245+
}
246+
198247
return <div>
199248
<section className={styles.header}>
200249
<PlatformIcon platform={platform} size={32} />
@@ -203,39 +252,13 @@ export default function CompilerOpts({ platform, value, onChange, diffLabel, onD
203252
<PresetSelect platform={platform} presetName={value.preset} setPreset={setPreset} />
204253
</div>
205254
</section>
206-
<OptsContext.Provider value={{
207-
checkFlag(flag: string) {
208-
return (" " + opts + " ").includes(" " + flag + " ")
209-
},
210-
211-
setFlag(flag: string, enable: boolean) {
212-
if (enable) {
213-
opts = opts + " " + flag
214-
} else {
215-
opts = (" " + opts + " ").replace(" " + flag + " ", " ")
216-
}
217-
opts = opts.trim()
218-
setOpts(opts)
219-
},
220-
}}>
255+
<OptsContext.Provider value={optsEditorProvider}>
221256
<section className={styles.section}>
222257
<h3 className={styles.heading}>Compiler options</h3>
223258
<OptsEditor platform={platform} compiler={compiler} setCompiler={setCompiler} opts={opts} setOpts={setOpts} />
224259
</section>
225260
</OptsContext.Provider>
226-
<OptsContext.Provider value={{
227-
checkFlag(flag: string) {
228-
return diff_opts.includes(flag)
229-
},
230-
231-
setFlag(flag: string, enable: boolean) {
232-
if (enable && !diff_opts.includes(flag)) {
233-
setDiffOpts([...diff_opts, flag])
234-
} else if (!enable && diff_opts.includes(flag)) {
235-
setDiffOpts(diff_opts.filter(o => o != flag))
236-
}
237-
},
238-
}}>
261+
<OptsContext.Provider value={diffOptsEditorProvider}>
239262
<section className={styles.section}>
240263
<h3 className={styles.heading}>Diff options</h3>
241264
<DiffOptsEditor platform={platform} compiler={compiler} diffLabel={diffLabel} onDiffLabelChange={onDiffLabelChange} />

0 commit comments

Comments
 (0)