-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): allow presets to be explored in dedicated page (#1232)
* feat(backend): allowing auth users to create presents * fix: linter&preset behaviour * fix: remove unused import * feat: improve presets exploration * fix: remove platform presets list * fix: export name field * fix: platform icon * fix: PR comments * fix: tailwindcss classes
- Loading branch information
Showing
5 changed files
with
140 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Presets } from "@/app/(navfooter)/preset/presets" | ||
import { get } from "@/lib/api/request" | ||
|
||
export default async function Page() { | ||
const compilers = await get("/compiler") | ||
|
||
return ( | ||
<main className="mx-auto w-full max-w-3xl p-4"> | ||
<Presets serverCompilers={compilers}/> | ||
</main> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use client" | ||
|
||
import { useState } from "react" | ||
|
||
import PlatformSelect from "@/components/PlatformSelect" | ||
import { PresetList } from "@/components/PresetList" | ||
import * as api from "@/lib/api" | ||
|
||
export function Presets({ serverCompilers }: { | ||
serverCompilers: { | ||
platforms: { | ||
[id: string]: api.Platform | ||
} | ||
compilers: { | ||
[id: string]: api.Compiler | ||
} | ||
} | ||
}) { | ||
|
||
const platforms = Object.keys(serverCompilers.platforms) | ||
|
||
const [platform, setPlatform] = useState<string>(platforms.length > 0 ? platforms[0] : "") | ||
|
||
return ( | ||
<section> | ||
<h2 className="pb-2 text-lg font-medium tracking-tight">Platforms</h2> | ||
<PlatformSelect | ||
platforms={serverCompilers.platforms} | ||
value={platform} | ||
onChange={setPlatform} | ||
/> | ||
<h2 className="py-2 text-lg font-medium tracking-tight">Presets</h2> | ||
<PresetList url={`/preset?platform=${platform}`}/> | ||
</section> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"use client" | ||
|
||
import type { ReactNode, JSX } from "react" | ||
|
||
import Link from "next/link" | ||
|
||
import classNames from "classnames" | ||
|
||
import AsyncButton from "@/components/AsyncButton" | ||
import Button from "@/components/Button" | ||
import LoadingSpinner from "@/components/loading.svg" | ||
import { PlatformIcon } from "@/components/PlatformSelect/PlatformIcon" | ||
import { type Preset, usePaginated } from "@/lib/api" | ||
import { presetUrl } from "@/lib/api/urls" | ||
import useTranslation from "@/lib/i18n/translate" | ||
|
||
export interface Props { | ||
url?: string | ||
className?: string | ||
item?: ({ preset }: { preset: Preset }) => JSX.Element | ||
emptyButtonLabel?: ReactNode | ||
} | ||
|
||
export function PresetList({ url, className, item, emptyButtonLabel }: Props): JSX.Element { | ||
const { results, isLoading, hasNext, loadNext } = usePaginated<Preset>(url || "/preset") | ||
if (results.length === 0 && isLoading) { | ||
return <div className={classNames("flex justify-center items-center gap-[0.5em] p-[1em] opacity-50", className)}> | ||
<LoadingSpinner width="1.5em" height="1.5em" /> | ||
Just a moment... | ||
</div> | ||
} | ||
|
||
const Item = item ?? PresetItem | ||
|
||
return ( | ||
<ul className={classNames("flex flex-col justify-center gap-[0.5em] overflow-hidden rounded-md border-gray-6 text-sm", className)}> | ||
{results.map(preset => ( | ||
<Item hideIcon key={preset.id} preset={preset} /> | ||
))} | ||
{results.length === 0 && emptyButtonLabel && <li className={"col-[span_var(--num-columns,_1)] mt-[0.5em] flex items-center justify-center opacity-70"}> | ||
<Link href="/new"> | ||
|
||
<Button> | ||
{emptyButtonLabel} | ||
</Button> | ||
|
||
</Link> | ||
</li>} | ||
{hasNext && <li className={"col-[span_var(--num-columns,_1)] mt-[0.5em] flex items-center justify-center opacity-70"}> | ||
<AsyncButton onClick={loadNext}> | ||
Show more | ||
</AsyncButton> | ||
</li>} | ||
</ul> | ||
) | ||
} | ||
|
||
export function PresetItem({ preset, hideIcon }: { preset: Preset, hideIcon?: boolean }): JSX.Element { | ||
const compilersTranslation = useTranslation("compilers") | ||
const compilerName = compilersTranslation.t(preset.compiler) | ||
|
||
return ( | ||
<div className="rounded-md border border-gray-6 p-[1em] text-sm"> | ||
<div className="flex items-center gap-2"> | ||
{hideIcon && <PlatformIcon platform={preset.platform} className="w-[1.2em]"/>} | ||
<a className="font-semibold hover:text-[var(--link)]" href={presetUrl(preset)}> | ||
{preset.name} | ||
</a> | ||
</div> | ||
<p className="text-gray-11">{compilerName}</p> | ||
</div> | ||
) | ||
} |