|
| 1 | +import { Popover } from "@base-ui-components/react/popover"; |
| 2 | +import { LayoutGridIcon } from "lucide-react"; |
| 3 | +import type { ComponentProps } from "react"; |
| 4 | +import { usePreloadImages } from "../../lib/preload.js"; |
| 5 | +import { Button } from "../button/button.js"; |
| 6 | +import { useClients } from "./usher.js"; |
| 7 | + |
| 8 | +type RenderProp = ComponentProps<typeof Popover.Trigger>["render"]; |
| 9 | + |
| 10 | +type Props = { |
| 11 | + children?: RenderProp; |
| 12 | + referrer: string; |
| 13 | + usherUrl: string; |
| 14 | +}; |
| 15 | + |
| 16 | +const FALLBACK_LOGO_URL = |
| 17 | + "https://cdn.scouterna.net/jamboree26/images/app-placeholder.png"; |
| 18 | + |
| 19 | +function AppWaffle({ children, referrer, usherUrl }: Props) { |
| 20 | + const { clients, error } = useClients({ usherUrl }); |
| 21 | + |
| 22 | + const imageUrls = clients.map( |
| 23 | + (client) => client.logoUrl ?? FALLBACK_LOGO_URL, |
| 24 | + ); |
| 25 | + usePreloadImages(imageUrls); |
| 26 | + |
| 27 | + const clientsWithReferrer = clients.map((client) => { |
| 28 | + const url = new URL(client.url); |
| 29 | + url.searchParams.set("referrer", referrer); |
| 30 | + |
| 31 | + return { |
| 32 | + ...client, |
| 33 | + url: url.toString(), |
| 34 | + }; |
| 35 | + }); |
| 36 | + |
| 37 | + children = children ?? ( |
| 38 | + <Button size="medium-icon" variant="text" aria-label="Change app"> |
| 39 | + <LayoutGridIcon /> |
| 40 | + </Button> |
| 41 | + ); |
| 42 | + |
| 43 | + return ( |
| 44 | + <Popover.Root> |
| 45 | + <Popover.Trigger render={children} /> |
| 46 | + <Popover.Portal> |
| 47 | + <Popover.Positioner sideOffset={8}> |
| 48 | + <Popover.Popup className="origin-[var(--transform-origin)] rounded-lg bg-[canvas] p-4 text-gray-900 shadow outline outline-gray-200 transition-[transform,scale,opacity] data-[ending-style]:scale-90 data-[ending-style]:opacity-0 data-[starting-style]:scale-90 data-[starting-style]:opacity-0"> |
| 49 | + <Popover.Arrow className="data-[side=bottom]:top-[-8px] data-[side=left]:right-[-13px] data-[side=left]:rotate-90 data-[side=right]:left-[-13px] data-[side=right]:-rotate-90 data-[side=top]:bottom-[-8px] data-[side=top]:rotate-180"> |
| 50 | + <ArrowSvg /> |
| 51 | + </Popover.Arrow> |
| 52 | + {error ? ( |
| 53 | + <div>Something went wrong while loading your apps.</div> |
| 54 | + ) : ( |
| 55 | + <div className="grid grid-cols-3 gap-2 grid-flow-row-dense"> |
| 56 | + {clientsWithReferrer.map((client) => ( |
| 57 | + <a |
| 58 | + href={client.url} |
| 59 | + target="_blank" |
| 60 | + rel="noopener noreferrer" |
| 61 | + key={client.id} |
| 62 | + className="relative size-20" |
| 63 | + > |
| 64 | + <div |
| 65 | + className={` |
| 66 | + group/app-card |
| 67 | + absolute w-full h-full min-h-full rounded-lg pt-3 pb-1 px-1 flex flex-col items-center |
| 68 | + hover:h-auto hover:bg-gray-100/80 |
| 69 | + select-none |
| 70 | + `} |
| 71 | + > |
| 72 | + <img |
| 73 | + src={client.logoUrl ?? FALLBACK_LOGO_URL} |
| 74 | + alt={client.name} |
| 75 | + className="size-9 aspect-square object-cover rounded-sm" |
| 76 | + /> |
| 77 | + <span |
| 78 | + className={` |
| 79 | + text-sm text-center mt-1 overflow-ellipsis line-clamp-1 |
| 80 | + group-hover/app-card:line-clamp-none |
| 81 | + `} |
| 82 | + > |
| 83 | + {client.name} |
| 84 | + </span> |
| 85 | + </div> |
| 86 | + </a> |
| 87 | + ))} |
| 88 | + </div> |
| 89 | + )} |
| 90 | + </Popover.Popup> |
| 91 | + </Popover.Positioner> |
| 92 | + </Popover.Portal> |
| 93 | + </Popover.Root> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +function ArrowSvg(props: React.ComponentProps<"svg">) { |
| 98 | + return ( |
| 99 | + // biome-ignore lint/a11y/noSvgWithoutTitle: This is only for display |
| 100 | + <svg |
| 101 | + width="20" |
| 102 | + height="10" |
| 103 | + viewBox="0 0 20 10" |
| 104 | + fill="none" |
| 105 | + {...props} |
| 106 | + aria-hidden |
| 107 | + > |
| 108 | + <path |
| 109 | + d="M9.66437 2.60207L4.80758 6.97318C4.07308 7.63423 3.11989 8 2.13172 8H0V10H20V8H18.5349C17.5468 8 16.5936 7.63423 15.8591 6.97318L11.0023 2.60207C10.622 2.2598 10.0447 2.25979 9.66437 2.60207Z" |
| 110 | + className="fill-[canvas]" |
| 111 | + /> |
| 112 | + <path |
| 113 | + d="M8.99542 1.85876C9.75604 1.17425 10.9106 1.17422 11.6713 1.85878L16.5281 6.22989C17.0789 6.72568 17.7938 7.00001 18.5349 7.00001L15.89 7L11.0023 2.60207C10.622 2.2598 10.0447 2.2598 9.66436 2.60207L4.77734 7L2.13171 7.00001C2.87284 7.00001 3.58774 6.72568 4.13861 6.22989L8.99542 1.85876Z" |
| 114 | + className="fill-gray-200" |
| 115 | + /> |
| 116 | + </svg> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +export { AppWaffle }; |
0 commit comments