Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap usePlatforms for usePresets/usePreset #1139

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ?
mkst marked this conversation as resolved.
Show resolved Hide resolved
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`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized the site will cease to show any more than 100 presets with this change. I think what we eventually want is a searchable thing with auto-populated results, and then an "Add new..." for creating a new preset

For now, instead of usePresets, we might want to keep what we had before, so we're sure not to omit any presets from the dropdown

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 100 presets per platform, which I think will be fine until we get around to revamping it (i.e. when we let users create presets)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also bump up 100 to 250 here

max_page_size = 100
if we ever reached 101 presets for a given platform

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, right... yeah it's probably fine then

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add an assert for this on the backend perhaps? Or implement pagination here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert that the length of presets is < 100? Currently we have 43 presets for N64 (which I'm assuming is the most popular platform). I'll create an issue to make the Presets a searchable drop-down - I feel it's likely we'll resolve that before we hit 100 presets for any individual platform.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #1152.

}

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