|
| 1 | +import { useQuery } from "@apollo/client/react" |
| 2 | +import { ExternalLink, Infinity as InfinityIcon, RotateCw } from "lucide-react" |
| 3 | + |
| 4 | +import { CardImage, EmptyState } from "../../components/card-image" |
| 5 | +import { Badge } from "../../components/ui/badge" |
| 6 | +import { Button } from "../../components/ui/button" |
| 7 | +import { |
| 8 | + Dialog, |
| 9 | + DialogClose, |
| 10 | + DialogContent, |
| 11 | + DialogHeader, |
| 12 | + DialogTitle, |
| 13 | +} from "../../components/ui/dialog" |
| 14 | +import type { DeckCombosQuery } from "../../gql/graphql" |
| 15 | +import { ManaText } from "../cards/card-text" |
| 16 | +import { DeckCombosDocument } from "./queries" |
| 17 | + |
| 18 | +type Combo = DeckCombosQuery["deckCombos"][number] |
| 19 | +type ComboDeck = { id: string; name: string } |
| 20 | + |
| 21 | +export function DeckCombosDialog({ |
| 22 | + deck, |
| 23 | + onOpenChange, |
| 24 | + open, |
| 25 | +}: { |
| 26 | + deck: ComboDeck | null |
| 27 | + onOpenChange: (open: boolean) => void |
| 28 | + open: boolean |
| 29 | +}) { |
| 30 | + const comboQuery = useQuery(DeckCombosDocument, { |
| 31 | + variables: { id: deck?.id || "" }, |
| 32 | + skip: !open || !deck?.id, |
| 33 | + fetchPolicy: "network-only", |
| 34 | + }) |
| 35 | + const combos = comboQuery.data?.deckCombos || [] |
| 36 | + const isLoading = comboQuery.loading && !comboQuery.data |
| 37 | + |
| 38 | + return ( |
| 39 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 40 | + <DialogContent className="max-w-6xl" labelledBy="deck-combos-title"> |
| 41 | + <DialogHeader> |
| 42 | + <div className="min-w-0"> |
| 43 | + <DialogTitle id="deck-combos-title" className="flex items-center gap-2"> |
| 44 | + <InfinityIcon aria-hidden="true" className="h-5 w-5 text-primary" /> |
| 45 | + Infinite combos |
| 46 | + </DialogTitle> |
| 47 | + <p className="mt-1 truncate text-sm text-base-content/60">{deck?.name}</p> |
| 48 | + </div> |
| 49 | + <DialogClose onClose={() => onOpenChange(false)} /> |
| 50 | + </DialogHeader> |
| 51 | + |
| 52 | + <div className="min-h-0 flex-1 overflow-y-auto p-4 sm:p-5"> |
| 53 | + {isLoading ? <ComboLoadingState /> : null} |
| 54 | + |
| 55 | + {comboQuery.error ? ( |
| 56 | + <div |
| 57 | + className="rounded-box border border-error/30 bg-error/10 p-4 text-sm text-error" |
| 58 | + role="alert" |
| 59 | + > |
| 60 | + <p className="font-bold">Commander Spellbook could not check this deck.</p> |
| 61 | + <p className="mt-1 text-error/85">Try the request again in a moment.</p> |
| 62 | + <Button |
| 63 | + type="button" |
| 64 | + variant="outline" |
| 65 | + size="sm" |
| 66 | + className="mt-3" |
| 67 | + onClick={() => void comboQuery.refetch()} |
| 68 | + > |
| 69 | + <RotateCw className="h-4 w-4" /> |
| 70 | + Retry |
| 71 | + </Button> |
| 72 | + </div> |
| 73 | + ) : null} |
| 74 | + |
| 75 | + {!isLoading && !comboQuery.error && combos.length === 0 ? ( |
| 76 | + <EmptyState |
| 77 | + title="No infinite combos found" |
| 78 | + description="Commander Spellbook did not find a complete combo among this deck's commander and mainboard cards." |
| 79 | + action={<CommanderSpellbookLink />} |
| 80 | + /> |
| 81 | + ) : null} |
| 82 | + |
| 83 | + {combos.length > 0 ? ( |
| 84 | + <div> |
| 85 | + <div className="flex flex-wrap items-center justify-between gap-3 border-b border-base-300 pb-4"> |
| 86 | + <p className="text-sm text-base-content/70"> |
| 87 | + <span className="font-mono font-black tabular-nums text-base-content"> |
| 88 | + {combos.length} |
| 89 | + </span>{" "} |
| 90 | + {combos.length === 1 ? "combo" : "combos"} found in the current decklist |
| 91 | + </p> |
| 92 | + <CommanderSpellbookLink /> |
| 93 | + </div> |
| 94 | + <div className="divide-y divide-base-300"> |
| 95 | + {combos.map((combo) => ( |
| 96 | + <ComboResult key={combo.id} combo={combo} /> |
| 97 | + ))} |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ) : null} |
| 101 | + </div> |
| 102 | + </DialogContent> |
| 103 | + </Dialog> |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +function ComboLoadingState() { |
| 108 | + return ( |
| 109 | + <div |
| 110 | + className="space-y-5" |
| 111 | + aria-busy="true" |
| 112 | + aria-label="Checking Commander Spellbook" |
| 113 | + role="status" |
| 114 | + > |
| 115 | + <p className="text-sm font-bold text-base-content/70">Checking the current decklist…</p> |
| 116 | + {[0, 1].map((index) => ( |
| 117 | + <div key={index} className="space-y-3 border-t border-base-300 pt-5 first:border-t-0"> |
| 118 | + <div className="flex gap-3"> |
| 119 | + <div className="h-20 w-14 animate-pulse rounded-box bg-base-200" /> |
| 120 | + <div className="h-20 w-14 animate-pulse rounded-box bg-base-200" /> |
| 121 | + </div> |
| 122 | + <div className="h-5 w-64 max-w-full animate-pulse rounded bg-base-200" /> |
| 123 | + <div className="h-4 w-full animate-pulse rounded bg-base-200" /> |
| 124 | + </div> |
| 125 | + ))} |
| 126 | + </div> |
| 127 | + ) |
| 128 | +} |
| 129 | + |
| 130 | +function ComboResult({ combo }: { combo: Combo }) { |
| 131 | + const steps = combo.description |
| 132 | + .split(/\r?\n/u) |
| 133 | + .map((step) => step.trim()) |
| 134 | + .filter(Boolean) |
| 135 | + |
| 136 | + return ( |
| 137 | + <article className="py-5 first:pt-4 last:pb-0"> |
| 138 | + <div className="flex flex-col gap-5 lg:grid lg:grid-cols-[minmax(15rem,0.8fr)_minmax(0,1.2fr)]"> |
| 139 | + <div className="space-y-4"> |
| 140 | + <ul className="flex flex-wrap gap-3" aria-label="Combo cards"> |
| 141 | + {combo.cards.map((card, index) => ( |
| 142 | + <li key={`${combo.id}-${card.name}-${index}`} className="flex items-center gap-3"> |
| 143 | + {index > 0 ? ( |
| 144 | + <span aria-hidden="true" className="text-lg font-black text-base-content/35"> |
| 145 | + + |
| 146 | + </span> |
| 147 | + ) : null} |
| 148 | + <div className="flex items-center gap-2"> |
| 149 | + <CardImage |
| 150 | + printing={{ imageUrl: card.imageUrl, card: { name: card.name } }} |
| 151 | + className="h-20 w-14 shrink-0 rounded-box" |
| 152 | + /> |
| 153 | + <span className="max-w-36 text-sm font-bold leading-snug"> |
| 154 | + {card.quantity > 1 ? `${card.quantity}× ` : ""} |
| 155 | + {card.name} |
| 156 | + </span> |
| 157 | + </div> |
| 158 | + </li> |
| 159 | + ))} |
| 160 | + </ul> |
| 161 | + |
| 162 | + <div> |
| 163 | + <h3 className="text-xs font-bold uppercase text-base-content/55">Produces</h3> |
| 164 | + <div className="mt-2 flex flex-wrap gap-2"> |
| 165 | + {combo.produces.map((result) => ( |
| 166 | + <Badge key={result} tone="success"> |
| 167 | + {result} |
| 168 | + </Badge> |
| 169 | + ))} |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + |
| 174 | + <div className="space-y-4"> |
| 175 | + <div className="flex flex-wrap items-center justify-between gap-3"> |
| 176 | + <h3 className="font-black">How it works</h3> |
| 177 | + <Button asChild variant="outline" size="sm"> |
| 178 | + <a href={combo.url} target="_blank" rel="noreferrer"> |
| 179 | + Open combo |
| 180 | + <ExternalLink className="h-3.5 w-3.5" /> |
| 181 | + </a> |
| 182 | + </Button> |
| 183 | + </div> |
| 184 | + |
| 185 | + {steps.length ? ( |
| 186 | + <ol className="space-y-2 text-sm leading-6 text-base-content/80"> |
| 187 | + {steps.map((step, index) => ( |
| 188 | + <li key={`${combo.id}-step-${index}`} className="flex items-baseline gap-3"> |
| 189 | + <span className="w-5 shrink-0 text-right font-mono text-xs font-black leading-6 tabular-nums text-primary"> |
| 190 | + {index + 1}. |
| 191 | + </span> |
| 192 | + <span>{step}</span> |
| 193 | + </li> |
| 194 | + ))} |
| 195 | + </ol> |
| 196 | + ) : null} |
| 197 | + |
| 198 | + {combo.manaNeeded || combo.prerequisites.length > 0 || combo.notes ? ( |
| 199 | + <dl className="grid gap-3 border-t border-base-300 pt-4 text-sm sm:grid-cols-2"> |
| 200 | + {combo.manaNeeded ? ( |
| 201 | + <div> |
| 202 | + <dt className="font-bold text-base-content/60">Mana needed</dt> |
| 203 | + <dd className="mt-1 font-medium"> |
| 204 | + <ManaText text={combo.manaNeeded} /> |
| 205 | + </dd> |
| 206 | + </div> |
| 207 | + ) : null} |
| 208 | + {combo.prerequisites.length > 0 ? ( |
| 209 | + <div className={combo.manaNeeded ? undefined : "sm:col-span-2"}> |
| 210 | + <dt className="font-bold text-base-content/60">Prerequisites</dt> |
| 211 | + <dd className="mt-1"> |
| 212 | + <ul className="space-y-1"> |
| 213 | + {combo.prerequisites.map((prerequisite) => ( |
| 214 | + <li key={prerequisite}>• {prerequisite}</li> |
| 215 | + ))} |
| 216 | + </ul> |
| 217 | + </dd> |
| 218 | + </div> |
| 219 | + ) : null} |
| 220 | + {combo.notes ? ( |
| 221 | + <div className="sm:col-span-2"> |
| 222 | + <dt className="font-bold text-base-content/60">Notes</dt> |
| 223 | + <dd className="mt-1 text-base-content/75">{combo.notes}</dd> |
| 224 | + </div> |
| 225 | + ) : null} |
| 226 | + </dl> |
| 227 | + ) : null} |
| 228 | + </div> |
| 229 | + </div> |
| 230 | + </article> |
| 231 | + ) |
| 232 | +} |
| 233 | + |
| 234 | +function CommanderSpellbookLink() { |
| 235 | + return ( |
| 236 | + <a |
| 237 | + href="https://commanderspellbook.com/" |
| 238 | + target="_blank" |
| 239 | + rel="noreferrer" |
| 240 | + className="inline-flex items-center gap-1.5 text-sm font-bold text-primary underline-offset-4 hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary/35" |
| 241 | + > |
| 242 | + Commander Spellbook |
| 243 | + <ExternalLink className="h-3.5 w-3.5" /> |
| 244 | + </a> |
| 245 | + ) |
| 246 | +} |
0 commit comments