|
| 1 | +import Link from "next/link" |
| 2 | +import { useRouter } from "next/router" |
| 3 | +import { useTranslation } from "next-i18next" |
| 4 | +import styled from "styled-components" |
| 5 | +import { Card, Badge } from "../../bootstrap" |
| 6 | +import { Highlight } from "react-instantsearch" |
| 7 | +import { HearingHitData } from "./HearingSearch" |
| 8 | +import { |
| 9 | + useState, |
| 10 | + useMemo, |
| 11 | + KeyboardEvent, |
| 12 | + MouseEvent, |
| 13 | + useCallback |
| 14 | +} from "react" |
| 15 | + |
| 16 | +const StyledCard = styled(Card)` |
| 17 | + border: none; |
| 18 | + border-radius: 4px; |
| 19 | + margin-bottom: 0.75rem; |
| 20 | + overflow: hidden; |
| 21 | + cursor: pointer; |
| 22 | + outline-color: var(--bs-blue); |
| 23 | + outline-style: solid; |
| 24 | + outline-width: 0; |
| 25 | + transition: outline-width 0.1s; |
| 26 | +
|
| 27 | + font-size: 0.85rem; |
| 28 | +
|
| 29 | + &:hover { |
| 30 | + outline-width: 2px; |
| 31 | + } |
| 32 | +
|
| 33 | + &:active { |
| 34 | + outline-width: 4px; |
| 35 | + } |
| 36 | +
|
| 37 | + .card-body { |
| 38 | + padding: 0.85rem 1rem; |
| 39 | + } |
| 40 | +` |
| 41 | + |
| 42 | +const SectionLabel = styled.span` |
| 43 | + color: var(--bs-blue); |
| 44 | + font-weight: 600; |
| 45 | + margin-right: 0.5rem; |
| 46 | +` |
| 47 | + |
| 48 | +export const HearingHit = ({ hit }: { hit: HearingHitData }) => { |
| 49 | + const { t } = useTranslation(["search", "hearing"]) |
| 50 | + const router = useRouter() |
| 51 | + const startsAt = new Date(hit.startsAt) |
| 52 | + const scheduleDate = t("schedule_date", { ns: "hearing", date: startsAt }) |
| 53 | + const scheduleTime = t("schedule_time", { ns: "hearing", date: startsAt }) |
| 54 | + const chairNames = hit.chairNames ?? [] |
| 55 | + const topics = hit.agendaTopics ?? [] |
| 56 | + const bills = useMemo(() => { |
| 57 | + const numbers = hit.billNumbers ?? [] |
| 58 | + const slugs = hit.billSlugs ?? [] |
| 59 | + return numbers.map((number, index) => ({ |
| 60 | + number, |
| 61 | + slug: slugs[index]! |
| 62 | + })) |
| 63 | + }, [hit.billNumbers, hit.billSlugs]) |
| 64 | + |
| 65 | + const navigateToHearing = useCallback(() => { |
| 66 | + void router.push(`/hearing/${hit.eventId}`) |
| 67 | + }, [hit.eventId, router]) |
| 68 | + |
| 69 | + const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => { |
| 70 | + if (event.key === "Enter" || event.key === " ") { |
| 71 | + event.preventDefault() |
| 72 | + navigateToHearing() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const handleClick = (event: MouseEvent<HTMLDivElement>) => { |
| 77 | + event.preventDefault() |
| 78 | + navigateToHearing() |
| 79 | + } |
| 80 | + |
| 81 | + return ( |
| 82 | + <StyledCard |
| 83 | + role="link" |
| 84 | + tabIndex={0} |
| 85 | + className="w-100" |
| 86 | + onClick={handleClick} |
| 87 | + onKeyDown={handleKeyDown} |
| 88 | + aria-label={hit.title} |
| 89 | + > |
| 90 | + <Card.Body className="bg-white"> |
| 91 | + <div className="d-flex flex-column gap-2"> |
| 92 | + <div className="d-flex flex-wrap gap-2 align-items-center justify-content-between"> |
| 93 | + <div className="d-flex flex-column"> |
| 94 | + <span className="text-uppercase fw-semibold text-secondary"> |
| 95 | + {scheduleDate} |
| 96 | + </span> |
| 97 | + <span className="text-secondary">{scheduleTime}</span> |
| 98 | + </div> |
| 99 | + {hit.hasVideo ? ( |
| 100 | + <Badge bg="success" pill> |
| 101 | + {t("video_available", { ns: "search" })} |
| 102 | + </Badge> |
| 103 | + ) : null} |
| 104 | + </div> |
| 105 | + |
| 106 | + <div> |
| 107 | + <Card.Title as="h6" className="mb-1"> |
| 108 | + <Highlight attribute="title" hit={hit} /> |
| 109 | + </Card.Title> |
| 110 | + {hit.description ? ( |
| 111 | + <p className="mb-0 text-muted"> |
| 112 | + <Highlight attribute="description" hit={hit} /> |
| 113 | + </p> |
| 114 | + ) : null} |
| 115 | + </div> |
| 116 | + |
| 117 | + {hit.locationName || hit.locationCity ? ( |
| 118 | + <div> |
| 119 | + <SectionLabel> |
| 120 | + {t("location_label", { ns: "search" })} |
| 121 | + </SectionLabel> |
| 122 | + <span> |
| 123 | + {hit.locationName ?? hit.locationCity} |
| 124 | + {hit.locationName && hit.locationCity |
| 125 | + ? ` · ${hit.locationCity}` |
| 126 | + : ""} |
| 127 | + </span> |
| 128 | + </div> |
| 129 | + ) : null} |
| 130 | + |
| 131 | + {chairNames.length ? ( |
| 132 | + <div className="d-flex align-items-center gap-2"> |
| 133 | + <SectionLabel>{t("chairs", { ns: "hearing" })}</SectionLabel> |
| 134 | + {<span>{chairNames.join(", ")}</span>} |
| 135 | + </div> |
| 136 | + ) : null} |
| 137 | + |
| 138 | + {topics.length ? ( |
| 139 | + <div> |
| 140 | + <SectionLabel>{t("agenda_label", { ns: "search" })}</SectionLabel> |
| 141 | + <span>{topics.join(", ")}</span> |
| 142 | + </div> |
| 143 | + ) : null} |
| 144 | + |
| 145 | + <BillsSection bills={bills} /> |
| 146 | + </div> |
| 147 | + </Card.Body> |
| 148 | + </StyledCard> |
| 149 | + ) |
| 150 | +} |
| 151 | + |
| 152 | +const BillsSection = ({ |
| 153 | + bills |
| 154 | +}: { |
| 155 | + bills: { number: string; slug: string }[] |
| 156 | +}) => { |
| 157 | + const { t } = useTranslation("search") |
| 158 | + const [showAllBills, setShowAllBills] = useState(false) |
| 159 | + |
| 160 | + if (!bills.length) return null |
| 161 | + |
| 162 | + const visibleCount = showAllBills ? bills.length : Math.min(7, bills.length) |
| 163 | + const visibleBills = bills.slice(0, visibleCount) |
| 164 | + const remaining = bills.length - visibleCount |
| 165 | + |
| 166 | + return ( |
| 167 | + <div> |
| 168 | + <SectionLabel>{t("bills_label")}</SectionLabel> |
| 169 | + <span> |
| 170 | + {visibleBills.map((bill, index) => { |
| 171 | + const isLastVisible = index === visibleBills.length - 1 |
| 172 | + const shouldShowComma = |
| 173 | + !isLastVisible || (!showAllBills && remaining > 0) |
| 174 | + |
| 175 | + return ( |
| 176 | + <span key={`${bill.slug}-${bill.number}-${index}`}> |
| 177 | + <Link href={`/bills/${bill.slug}`} legacyBehavior> |
| 178 | + <a |
| 179 | + className="text-decoration-none" |
| 180 | + onClick={event => event.stopPropagation()} |
| 181 | + onKeyDown={event => { |
| 182 | + if (event.key === "Enter" || event.key === " ") { |
| 183 | + event.stopPropagation() |
| 184 | + } |
| 185 | + }} |
| 186 | + > |
| 187 | + {bill.number} |
| 188 | + </a> |
| 189 | + </Link> |
| 190 | + {shouldShowComma ? ", " : ""} |
| 191 | + </span> |
| 192 | + ) |
| 193 | + })} |
| 194 | + {!showAllBills && remaining > 0 ? ( |
| 195 | + <button |
| 196 | + type="button" |
| 197 | + className="btn btn-link p-0 align-baseline" |
| 198 | + onClick={event => { |
| 199 | + event.stopPropagation() |
| 200 | + setShowAllBills(true) |
| 201 | + }} |
| 202 | + > |
| 203 | + {t("more_bills", { count: remaining })} |
| 204 | + </button> |
| 205 | + ) : null} |
| 206 | + </span> |
| 207 | + </div> |
| 208 | + ) |
| 209 | +} |
0 commit comments