feat(squads): show each athlete face in the squad panel
The squad list can show who has a face and what it looks like.
This commit was merged in pull request #223.
This commit is contained in:
1 parent
256473e3a7
commit
c125d6ed5c
5 files changed
+81
-5
No files matched your search
@@ -1388,11 +1388,15 @@ export class SportsService {
|
||||
const season = event.fixture?.season ?? seasonOf(event.startsAt);
|
||||
const players = await this.squad(team.id, season);
|
||||
const personOf = await this.peopleOf(event.organizationId, players);
|
||||
const faceOf = await this.facesOf([...personOf.values()]);
|
||||
sides.push({
|
||||
side,
|
||||
team: { id: team.id, name: team.name, abbreviation: team.abbreviation, logoUrl: team.logoUrl },
|
||||
players: players.map((p) => {
|
||||
const tally = counts(personOf.get(p.id) ?? null);
|
||||
const personId = personOf.get(p.id) ?? null;
|
||||
const tally = counts(personId);
|
||||
const enrolled = personId ? faceOf.get(personId) ?? null : null;
|
||||
const official = p.photoUrl && /^https?:\/\//i.test(p.photoUrl) ? p.photoUrl : null;
|
||||
return {
|
||||
id: p.id,
|
||||
number: p.number,
|
||||
@@ -1400,7 +1404,9 @@ export class SportsService {
|
||||
fullName: p.fullName,
|
||||
position: p.position,
|
||||
hasPhoto: !!p.photoUrl,
|
||||
personId: personOf.get(p.id) ?? null,
|
||||
faceUrl: enrolled ?? official,
|
||||
hasFace: !!enrolled,
|
||||
personId,
|
||||
photos: { confirmed: tally.confirmed, likely: tally.likely },
|
||||
photoIds: tally.photoIds,
|
||||
};
|
||||
@@ -1765,6 +1771,19 @@ export class SportsService {
|
||||
return linked?.season ?? null;
|
||||
}
|
||||
|
||||
/** Signed URL of each person's enrolled face. People without a reference photo are absent. */
|
||||
private async facesOf(personIds: string[]): Promise<Map<string, string>> {
|
||||
const ids = [...new Set(personIds)];
|
||||
const out = new Map<string, string>();
|
||||
if (!ids.length) return out;
|
||||
const people = await this.deps.db.person.findMany({
|
||||
where: { id: { in: ids }, referenceKey: { not: null } },
|
||||
select: { id: true },
|
||||
});
|
||||
for (const person of people) out.set(person.id, this.deps.storage.personUrl(person.id));
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Which person is each squad row: the row itself, or the same human at the club they play for now. */
|
||||
private async peopleOf(orgId: string, players: SportsPlayer[]): Promise<Map<string, string>> {
|
||||
const out = new Map<string, string>();
|
||||
|
||||
@@ -846,6 +846,10 @@ export const t = {
|
||||
never: "ainda não lido",
|
||||
addFpf: "Seguir competição FPF (link do resultados.fpf.pt)",
|
||||
add: "Adicionar",
|
||||
showFaces: "Caras",
|
||||
faceKnown: "Cara registada",
|
||||
faceSquadOnly: "Foto do plantel, ainda sem cara registada",
|
||||
faceNone: "Sem cara",
|
||||
squadsSync: "Atualizar pessoas a partir dos plantéis",
|
||||
squadsSynced: "Pessoas atualizadas",
|
||||
},
|
||||
|
||||
@@ -12,7 +12,7 @@ const squads: EventSquads = {
|
||||
side: "home",
|
||||
team: { id: "t1", name: "ÁGUAS SANTAS MILANEZA", abbreviation: "ASM", logoUrl: null },
|
||||
players: [
|
||||
{ id: "pl1", number: "5", name: "Miguel Baptista", fullName: null, position: null, hasPhoto: false, personId: "pe1", photos: { confirmed: 0, likely: 2 }, photoIds: ["a", "b"] },
|
||||
{ id: "pl1", number: "5", name: "Miguel Baptista", fullName: null, position: null, hasPhoto: true, faceUrl: "/v1/files/persons/pe1/reference", hasFace: true, personId: "pe1", photos: { confirmed: 0, likely: 2 }, photoIds: ["a", "b"] },
|
||||
{ id: "pl2", number: "7", name: "Gabriel", fullName: null, position: null, hasPhoto: false, personId: null, photos: { confirmed: 0, likely: 0 }, photoIds: [] },
|
||||
],
|
||||
},
|
||||
@@ -32,7 +32,7 @@ function Harness() {
|
||||
return (
|
||||
<>
|
||||
<SquadsButton
|
||||
api={{ eventSquads: vi.fn().mockResolvedValue(squads) } as unknown as ApiClient}
|
||||
api={{ eventSquads: vi.fn().mockResolvedValue(squads), absolute: (path: string) => path } as unknown as ApiClient}
|
||||
event={{ id: "e1", homeTeamId: "t1", awayTeamId: "t2" } as EventSummary}
|
||||
canEdit={false}
|
||||
onSaved={() => {}}
|
||||
@@ -59,4 +59,15 @@ describe("squad panel", () => {
|
||||
await waitFor(() => expect(screen.getByTestId("picked").textContent).toBe("b+c"));
|
||||
expect(miguel.getAttribute("aria-pressed")).toBe("false");
|
||||
});
|
||||
|
||||
it("shows each athlete's face only when asked", async () => {
|
||||
window.localStorage.clear();
|
||||
render(<Harness />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Plantéis" }));
|
||||
await screen.findByRole("button", { name: /Miguel Baptista/ });
|
||||
expect(document.querySelector("img")).toBeNull();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Caras" }));
|
||||
expect(document.querySelector("img")?.getAttribute("src")).toContain("/v1/files/persons/pe1/reference");
|
||||
expect(document.querySelectorAll("span[title$='Sem cara']").length).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,12 @@
|
||||
import { ApiError, type ApiClient, type EventSquads, type EventSummary } from "@photoreport/sdk";
|
||||
import { useEffect, useState } from "react";
|
||||
import { t } from "../i18n.js";
|
||||
import { prefs } from "../platform.js";
|
||||
import { FixturePicker } from "./EventForm.js";
|
||||
import { type PlayerPick, picksFrom } from "./squadFilter.js";
|
||||
|
||||
const FACES_PREF = "squad-faces";
|
||||
|
||||
/**
|
||||
* The open event's squads (from the federations): who is who by number, and whether each player is already a person
|
||||
* of the organisation (faces and jersey numbers name people). An older event can be linked to its match here.
|
||||
@@ -11,6 +14,7 @@ import { type PlayerPick, picksFrom } from "./squadFilter.js";
|
||||
*/
|
||||
export function SquadsButton({ api, event, canEdit, onSaved, facesTick = 0, selectedIds = [], onPlayers }: { api: ApiClient; event: EventSummary; canEdit: boolean; onSaved: (e: EventSummary) => void; facesTick?: number; selectedIds?: readonly string[]; onPlayers?: (picks: PlayerPick[]) => void }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [showFaces, setShowFaces] = useState(() => prefs.get(FACES_PREF) === "1");
|
||||
const [squads, setSquads] = useState<EventSquads | null>(null);
|
||||
const [status, setStatus] = useState<string | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
@@ -86,6 +90,20 @@ export function SquadsButton({ api, event, canEdit, onSaved, facesTick = 0, sele
|
||||
)
|
||||
) : (
|
||||
<div className="grid grid-cols-2 gap-3 p-3 max-sm:grid-cols-1">
|
||||
<div className="col-span-full flex justify-end">
|
||||
<button
|
||||
type="button"
|
||||
aria-pressed={showFaces}
|
||||
onClick={() => {
|
||||
const next = !showFaces;
|
||||
setShowFaces(next);
|
||||
prefs.set(FACES_PREF, next ? "1" : "0");
|
||||
}}
|
||||
className={`h-6 border border-black px-2 ${showFaces ? "bg-accent text-white" : "bg-cell text-ink hover:bg-cell-on"}`}
|
||||
>
|
||||
{t.sports.showFaces}
|
||||
</button>
|
||||
</div>
|
||||
{squads?.sides.map((s) => (
|
||||
<div key={s.side}>
|
||||
<p className="mb-1 font-semibold text-ink">{s.team.name}</p>
|
||||
@@ -100,8 +118,9 @@ export function SquadsButton({ api, event, canEdit, onSaved, facesTick = 0, sele
|
||||
aria-pressed={on}
|
||||
title={p.personId ? t.sports.photosHelp(p.photos?.confirmed ?? 0, p.photos?.likely ?? 0) : t.sports.notPerson}
|
||||
onClick={() => toggle(p.id)}
|
||||
className={`flex w-full gap-2 px-1 py-0.5 text-left ${on ? "bg-cell-on" : "hover:bg-cell"} disabled:cursor-default disabled:hover:bg-transparent`}
|
||||
className={`flex w-full items-center gap-2 px-1 py-0.5 text-left ${on ? "bg-cell-on" : "hover:bg-cell"} disabled:cursor-default disabled:hover:bg-transparent`}
|
||||
>
|
||||
{showFaces && <FaceThumb name={p.name} url={p.faceUrl} hasFace={p.hasFace === true} absolute={(path) => api.absolute(path)} />}
|
||||
<span className="w-7 shrink-0 text-right font-mono text-accent">{p.number ?? "–"}</span>
|
||||
<span className="truncate text-ink">{p.name}</span>
|
||||
<span className="ml-auto shrink-0 font-mono text-[11px]">
|
||||
@@ -140,3 +159,22 @@ export function SquadsButton({ api, event, canEdit, onSaved, facesTick = 0, sele
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** The enrolled face, the squad photo, or an empty slot when this athlete has neither. */
|
||||
function FaceThumb(props: { name: string; url?: string | null; hasFace: boolean; absolute: (path: string) => string }) {
|
||||
const [failed, setFailed] = useState(false);
|
||||
if (!props.url || failed) {
|
||||
return <span title={`${props.name}: ${t.sports.faceNone}`} className="inline-block h-8 w-8 shrink-0 border border-dashed border-[#555] bg-raised" />;
|
||||
}
|
||||
const title = props.hasFace ? t.sports.faceKnown : t.sports.faceSquadOnly;
|
||||
return (
|
||||
<img
|
||||
src={props.absolute(props.url)}
|
||||
alt=""
|
||||
title={`${props.name}: ${title}`}
|
||||
loading="lazy"
|
||||
onError={() => setFailed(true)}
|
||||
className={`h-8 w-8 shrink-0 object-cover ${props.hasFace ? "outline outline-1 outline-offset-[-1px] outline-white" : "opacity-70"}`}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -219,6 +219,10 @@ export interface EventSquads {
|
||||
fullName: string | null;
|
||||
position: string | null;
|
||||
hasPhoto: boolean;
|
||||
/** The enrolled face, or the squad photo when there is no face yet. */
|
||||
faceUrl?: string | null;
|
||||
/** An enrolled reference face, the one recognition uses. */
|
||||
hasFace?: boolean;
|
||||
personId: string | null;
|
||||
/** Photos of them in this event: confirmed, and taken for them by the faces (70% or more) not confirmed yet. */
|
||||
photos?: { confirmed: number; likely: number };
|
||||
|
||||
Reference in new issue
Block a user