Skip to content

Commit

Permalink
Swap usePlatforms for usePresets/usePreset (#1139)
Browse files Browse the repository at this point in the history
* Swap usePlatforms for usePresets/usePreset

* Don't emit warning if serverPresets is empty
  • Loading branch information
mkst authored Mar 20, 2024
1 parent 3fc0f43 commit 2fb9f75
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
9 changes: 3 additions & 6 deletions frontend/src/components/ScratchList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ export function getMatchPercentString(scratch: api.TerseScratch) {
export function ScratchItem({ scratch, children } : { scratch: api.TerseScratch, children?: ReactNode }) {
const compilersTranslation = useTranslation("compilers")
const compilerName = compilersTranslation.t(scratch.compiler as any)
const serverPresets = api.usePlatforms()[scratch.platform].presets
const matchPercentString = getMatchPercentString(scratch)
const preset = serverPresets.find(p => p.id === scratch.preset)
const preset = api.usePreset(scratch.preset)
const presetName = preset?.name

const presetOrCompiler = presetName ?
Expand Down Expand Up @@ -124,9 +123,8 @@ export function ScratchItem({ scratch, children } : { scratch: api.TerseScratch,
export function ScratchItemNoOwner({ scratch }: { scratch: api.TerseScratch }) {
const compilersTranslation = useTranslation("compilers")
const compilerName = compilersTranslation.t(scratch.compiler)
const serverPresets = api.usePlatforms()[scratch.platform].presets
const matchPercentString = getMatchPercentString(scratch)
const preset = serverPresets.find(p => p.id === scratch.preset)
const preset = api.usePreset(scratch.preset)
const presetName = preset?.name

const presetOrCompiler = presetName ?
Expand Down Expand Up @@ -159,9 +157,8 @@ export function ScratchItemNoOwner({ scratch }: { scratch: api.TerseScratch }) {
export function ScratchItemPlatformList({ scratch }: { scratch: api.TerseScratch }) {
const compilersTranslation = useTranslation("compilers")
const compilerName = compilersTranslation.t(scratch.compiler)
const serverPresets = api.usePlatforms()[scratch.platform].presets
const matchPercentString = getMatchPercentString(scratch)
const preset = serverPresets.find(p => p.id === scratch.preset)
const preset = api.usePreset(scratch.preset)
const presetName = preset?.name

const presetOrCompiler = presetName ?
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/compiler/PresetSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export default function PresetSelect({ className, platform, presetId, setPreset,
serverPresets?: api.Preset[]
}) {
if (!serverPresets)
serverPresets = api.usePlatforms()[platform].presets
serverPresets = api.usePresets(platform)

const selectedPreset = serverPresets.find(p => p.id === presetId)

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

return <Select
Expand Down
29 changes: 18 additions & 11 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,6 @@ export function useCompilation(scratch: Scratch | null, autoRecompile = true, au
}
}

export function usePlatforms(): Record<string, Platform> {
const { data } = useSWR<{ "platforms": Record<string, Platform> }>("/compiler", get, {
refreshInterval: 0,
revalidateOnFocus: false,
suspense: true, // TODO: remove
onErrorRetry,
})

return data?.platforms
}

export function usePlatform(id: string | undefined): Platform | undefined {
const url = typeof id === "string" ? `/platform/${id}` : null
const { data } = useSWR(url, get, {
Expand Down Expand Up @@ -288,6 +277,24 @@ export function useLibraries(): LibraryVersions[] {
return data.libraries
}

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

const url = typeof platform === "string" ? "/preset" : null
const { data } = useSWR([url, platform], getByPlatform, {
refreshInterval: 0,
onErrorRetry,
})

if (!data) {
return []
}

return data.results
}

export function usePreset(id: number | undefined): Preset | undefined {
const url = typeof id === "number" ? `/preset/${id}` : null
const { data } = useSWR(url, get, {
Expand Down

0 comments on commit 2fb9f75

Please sign in to comment.