mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-11-18 07:22:06 +00:00
fix dead code with knip upgrade TS to v6 and fix linting errors auto apply oxfmt rules fix linting errors - unused deps etc ignore file remove all ignored files in knip add knip - dead code analysis tool swap prettier and eslint of oxfmt and oxlint fix: fix map rendering in react strict mode fix: headlessUI float replace fix: disable Matomo if not URL and SITE ID exist upgrade: 1st pass upgrade to Next.js v16 deps: upgrade react to v19
106 lines
3.4 KiB
TypeScript
106 lines
3.4 KiB
TypeScript
import { useState } from "react";
|
|
|
|
import { useTranslations } from "next-intl";
|
|
import { FieldPath, FieldValues } from "react-hook-form";
|
|
import { FaExternalLinkAlt } from "react-icons/fa";
|
|
|
|
import { TimeControlColours } from "@/constants";
|
|
import { getTournamentDetails } from "@/server/getTournamentDetails";
|
|
import { SearchedTournament, searchTournaments } from "@/server/searchTournaments";
|
|
|
|
import { AsyncSelectField, AsyncSelectFieldProps } from "./AsyncSelectField";
|
|
|
|
type TournamentSelectFieldProps<
|
|
TFieldValues extends FieldValues = FieldValues,
|
|
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
> = Omit<
|
|
AsyncSelectFieldProps<TFieldValues, TFieldName, false, string, SearchedTournament>,
|
|
"loadOptions" | "loadOption" | "separators"
|
|
>;
|
|
|
|
export const TournamentSelectField = <
|
|
TFieldValues extends FieldValues = FieldValues,
|
|
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
>({
|
|
onInformChange,
|
|
...rest
|
|
}: TournamentSelectFieldProps<TFieldValues, TFieldName>) => {
|
|
const at = useTranslations("App");
|
|
const [selectedTournament, setSelectedTournament] = useState<SearchedTournament | null>(null);
|
|
|
|
const loadOption = async (ffeId: string) => {
|
|
const result = await getTournamentDetails({ ffeId });
|
|
const tournament = result?.data;
|
|
|
|
return !tournament
|
|
? undefined
|
|
: {
|
|
value: tournament.ffeId,
|
|
label: tournament.tournament,
|
|
data: tournament,
|
|
};
|
|
};
|
|
|
|
const loadOptions = async (searchValue?: string) => {
|
|
const result = await searchTournaments({
|
|
searchValue: searchValue ?? "",
|
|
});
|
|
|
|
const tournaments = result?.data ?? [];
|
|
|
|
return (tournaments ?? []).map((tournament) => ({
|
|
value: tournament.ffeId,
|
|
label: tournament.tournament,
|
|
data: tournament,
|
|
}));
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col gap-2">
|
|
<AsyncSelectField
|
|
{...rest}
|
|
isSearchable
|
|
separators
|
|
loadOption={loadOption}
|
|
loadOptions={loadOptions}
|
|
isClearable={true}
|
|
onInformChange={(data) => {
|
|
setSelectedTournament(data ? (data[0].data ?? null) : null);
|
|
onInformChange?.(data);
|
|
}}
|
|
formatOptionLabel={(option, context) => {
|
|
if (context.context === "value") return option.label;
|
|
return (
|
|
<div className="flex flex-col gap-1">
|
|
<div className="flex items-center gap-2 text-xs uppercase">
|
|
<div className="flex-1">{option.data?.town}</div>
|
|
<div
|
|
className="ml-2 rounded-sm px-1 py-0.5 align-middle text-[9px]/[11px] uppercase text-white"
|
|
style={{
|
|
background: `${TimeControlColours[option.data!.timeControl]}`,
|
|
}}
|
|
>
|
|
{at("timeControlEnum", { tc: option.data!.timeControl })}
|
|
</div>
|
|
<div className="text-xs">{option.data?.date}</div>
|
|
</div>
|
|
|
|
<div>{option.data?.tournament}</div>
|
|
</div>
|
|
);
|
|
}}
|
|
/>
|
|
{selectedTournament && (
|
|
<a
|
|
href={selectedTournament.url}
|
|
target="_blank"
|
|
className="inline-flex items-center justify-end gap-1 text-xs text-primary hover:text-primary-800"
|
|
>
|
|
{selectedTournament.url}
|
|
<FaExternalLinkAlt />
|
|
</a>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|