Skip to content

Commit

Permalink
Simplify UsePresets
Browse files Browse the repository at this point in the history
  • Loading branch information
mkst committed Jul 12, 2024
1 parent e303202 commit 424f09e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
12 changes: 5 additions & 7 deletions frontend/src/components/compiler/PresetSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export default function PresetSelect({ className, platform, presetId, setPreset,
if (typeof serverPresets === "undefined")
serverPresets = api.usePresets(platform)

const sortedPresets = typeof serverPresets === "undefined" ? null : serverPresets.slice().sort((a, b) => a.name.localeCompare(b.name))

if (sortedPresets === null) {
if (typeof serverPresets === "undefined") {
return <Select
className={className}
options={{ "Loading": "Loading..." }}
Expand All @@ -35,17 +33,17 @@ export default function PresetSelect({ className, platform, presetId, setPreset,
/>
}

const selectedPreset = sortedPresets.find((p: api.Preset) => p.id === presetId)
const selectedPreset = serverPresets.find((p: api.Preset) => p.id === presetId)

if (sortedPresets.length > 0 && typeof presetId === "number" && !selectedPreset)
if (serverPresets.length > 0 && typeof presetId === "number" && !selectedPreset)
console.warn(`Scratch.preset == '${presetId}' but no preset with that id was found.`)

return <Select
className={className}
options={presetsToOptions(sortedPresets)}
options={presetsToOptions(serverPresets)}
value={selectedPreset?.name || "Custom"}
onChange={name => {
setPreset(name === "Custom" ? null : sortedPresets.find((p: api.Preset) => p.name === name))
setPreset(name === "Custom" ? null : serverPresets.find((p: api.Preset) => p.name === name))
}}
/>
}
2 changes: 1 addition & 1 deletion frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export function useLibraries(platform: string): LibraryVersions[] {

export function usePresets(platform: string): Preset[] {
const getByPlatform = ([url, platform]: [string | null, string]) => {
return get(url && platform && `${url}?platform=${platform}&page_size=100`)
return get(url && platform && `${url}?platform=${platform}&ordering=name&page_size=100`)
}

const url = typeof platform === "string" ? "/preset" : null
Expand Down

0 comments on commit 424f09e

Please sign in to comment.