@backgold/template-kit 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/dist/chunk-RJAVFRC6.js +67 -0
- package/dist/constants-BAIo_Q5U.d-lQbj1e1-.d.ts +48 -0
- package/dist/content-api-Cox1-wz5.d.ts +1096 -0
- package/dist/contract/content-api-examples.d.ts +72 -0
- package/dist/contract/content-api-examples.js +926 -0
- package/dist/contract/content-api.d.ts +2 -0
- package/dist/contract/content-api.js +1 -0
- package/dist/contract/site-templates.d.ts +253 -0
- package/dist/contract/site-templates.js +185 -0
- package/package.json +70 -3
- package/src/components/EntryCard.astro +35 -0
- package/src/components/EventMap.astro +118 -0
- package/src/components/EventPage.astro +194 -0
- package/src/components/FieldValue.astro +41 -0
- package/src/components/LeadingHero.astro +21 -0
- package/src/components/NotFound.astro +20 -0
- package/src/components/PageSections.astro +47 -0
- package/src/components/Prose.astro +18 -0
- package/src/components/chrome/Brand.astro +61 -0
- package/src/components/chrome/ClosingCall.astro +94 -0
- package/src/components/chrome/DonateStrip.astro +61 -0
- package/src/components/chrome/FooterColumns.astro +32 -0
- package/src/components/chrome/FooterContact.astro +28 -0
- package/src/components/chrome/MenuList.astro +45 -0
- package/src/components/chrome/ModeToggle.astro +125 -0
- package/src/components/chrome/SignedInNotice.astro +53 -0
- package/src/components/chrome/SignupBand.astro +12 -0
- package/src/components/chrome/TakeoverBanner.astro +56 -0
- package/src/components/chrome/VisitorAccount.astro +82 -0
- package/src/components/entries/AuthorBox.astro +47 -0
- package/src/components/entries/EntryFields.astro +29 -0
- package/src/components/entries/EntryHeader.astro +77 -0
- package/src/components/entries/ListFrame.astro +126 -0
- package/src/components/entries/ListSearch.astro +24 -0
- package/src/components/entries/Share.astro +24 -0
- package/src/components/entries/course/classroom.astro +26 -0
- package/src/components/entries/course/lesson.astro +18 -0
- package/src/components/entries/course/section.ts +20 -0
- package/src/components/entries/course/syllabus.astro +15 -0
- package/src/components/entries/index.ts +77 -0
- package/src/components/entries/newsletter/issue.astro +41 -0
- package/src/components/entries/newsletter/list.astro +26 -0
- package/src/components/entries/podcast/episode.astro +42 -0
- package/src/components/entries/podcast/list.astro +36 -0
- package/src/components/entries/post/article.astro +31 -0
- package/src/components/entries/post/list.astro +85 -0
- package/src/components/entries/props.ts +61 -0
- package/src/components/entries/video/cards.astro +39 -0
- package/src/components/entries/video/grid.astro +15 -0
- package/src/components/entries/video/watch.astro +41 -0
- package/src/components/live/LivePage.astro +215 -0
- package/src/components/live/LocalTime.astro +29 -0
- package/src/components/sections/About/books.astro +188 -0
- package/src/components/sections/About/credentials.ts +80 -0
- package/src/components/sections/About/index.astro +60 -0
- package/src/components/sections/About/left.astro +57 -0
- package/src/components/sections/About/letter.astro +111 -0
- package/src/components/sections/About/portrait.astro +308 -0
- package/src/components/sections/Advocacy/compact.astro +14 -0
- package/src/components/sections/Advocacy/index.astro +14 -0
- package/src/components/sections/Band.astro +39 -0
- package/src/components/sections/Blog/feature.astro +176 -0
- package/src/components/sections/Blog/index.astro +37 -0
- package/src/components/sections/Blog/latest.ts +80 -0
- package/src/components/sections/Blog/list.astro +186 -0
- package/src/components/sections/Booking/index.astro +139 -0
- package/src/components/sections/Booking/list.astro +144 -0
- package/src/components/sections/Booking/types.ts +60 -0
- package/src/components/sections/Calendar/agenda.astro +14 -0
- package/src/components/sections/Calendar/index.astro +14 -0
- package/src/components/sections/CandidateRoster/ballot.astro +394 -0
- package/src/components/sections/CandidateRoster/candidates.ts +69 -0
- package/src/components/sections/CandidateRoster/compact.astro +115 -0
- package/src/components/sections/CandidateRoster/index.astro +57 -0
- package/src/components/sections/CaseStudies/beforeafter.astro +222 -0
- package/src/components/sections/CaseStudies/cases.ts +111 -0
- package/src/components/sections/CaseStudies/grid.astro +261 -0
- package/src/components/sections/CaseStudies/index.astro +333 -0
- package/src/components/sections/Classroom/CourseCard.astro +181 -0
- package/src/components/sections/Classroom/Courses.astro +88 -0
- package/src/components/sections/Classroom/classroom.ts +141 -0
- package/src/components/sections/Classroom/index.astro +20 -0
- package/src/components/sections/Classroom/list.astro +15 -0
- package/src/components/sections/Collection/compact.astro +7 -0
- package/src/components/sections/Collection/entries.ts +62 -0
- package/src/components/sections/Collection/feature.astro +180 -0
- package/src/components/sections/Collection/index.astro +46 -0
- package/src/components/sections/Collection/library.astro +271 -0
- package/src/components/sections/Collection/list.astro +7 -0
- package/src/components/sections/Collection/outline.astro +134 -0
- package/src/components/sections/Collection/shelves.astro +191 -0
- package/src/components/sections/Comparison/Unlisted.astro +6 -0
- package/src/components/sections/Comparison/cards.astro +171 -0
- package/src/components/sections/Comparison/index.astro +191 -0
- package/src/components/sections/Comparison/sides.ts +98 -0
- package/src/components/sections/Comparison/spectrum.astro +261 -0
- package/src/components/sections/ContentPost/banner.astro +152 -0
- package/src/components/sections/ContentPost/index.astro +35 -0
- package/src/components/sections/ContentPost/lesson.ts +126 -0
- package/src/components/sections/ContentPost/player.astro +254 -0
- package/src/components/sections/ContentPost/prose.astro +80 -0
- package/src/components/sections/ContentPost/theater.astro +329 -0
- package/src/components/sections/Countdown/index.astro +53 -0
- package/src/components/sections/Course/Bar.astro +46 -0
- package/src/components/sections/Course/CourseHead.astro +123 -0
- package/src/components/sections/Course/Crumbs.astro +44 -0
- package/src/components/sections/Course/Icon.astro +29 -0
- package/src/components/sections/Course/Ring.astro +62 -0
- package/src/components/sections/Course/Syllabus.astro +146 -0
- package/src/components/sections/Course/course.ts +146 -0
- package/src/components/sections/Course/index.astro +143 -0
- package/src/components/sections/Course/outline.astro +39 -0
- package/src/components/sections/Cta/banner.astro +64 -0
- package/src/components/sections/Cta/card.astro +88 -0
- package/src/components/sections/Cta/index.astro +31 -0
- package/src/components/sections/Cta/photo.astro +114 -0
- package/src/components/sections/Cta/strip.astro +91 -0
- package/src/components/sections/Donate/index.astro +136 -0
- package/src/components/sections/Donate/story.astro +7 -0
- package/src/components/sections/Event/card.astro +14 -0
- package/src/components/sections/Event/index.astro +14 -0
- package/src/components/sections/EventsList/AgendaMonths.astro +105 -0
- package/src/components/sections/EventsList/CalendarLine.astro +23 -0
- package/src/components/sections/EventsList/EventRows.astro +196 -0
- package/src/components/sections/EventsList/LiveCounts.astro +75 -0
- package/src/components/sections/EventsList/TypeTabs.astro +53 -0
- package/src/components/sections/EventsList/agenda.astro +55 -0
- package/src/components/sections/EventsList/calendar.astro +392 -0
- package/src/components/sections/EventsList/cards.astro +186 -0
- package/src/components/sections/EventsList/cohorts.astro +205 -0
- package/src/components/sections/EventsList/compact.astro +163 -0
- package/src/components/sections/EventsList/events.ts +168 -0
- package/src/components/sections/EventsList/index.astro +46 -0
- package/src/components/sections/EventsList/map.astro +436 -0
- package/src/components/sections/EventsList/shifts.astro +355 -0
- package/src/components/sections/EventsList/week.astro +361 -0
- package/src/components/sections/Faq/Contact.astro +63 -0
- package/src/components/sections/Faq/Questions.astro +98 -0
- package/src/components/sections/Faq/cards.astro +74 -0
- package/src/components/sections/Faq/faq.ts +101 -0
- package/src/components/sections/Faq/index.astro +49 -0
- package/src/components/sections/Faq/topics.astro +178 -0
- package/src/components/sections/Financials/Foot.astro +38 -0
- package/src/components/sections/Financials/Head.astro +47 -0
- package/src/components/sections/Financials/donut.astro +197 -0
- package/src/components/sections/Financials/financials.ts +52 -0
- package/src/components/sections/Financials/index.astro +148 -0
- package/src/components/sections/Financials/table.astro +157 -0
- package/src/components/sections/FormBlock/index.astro +33 -0
- package/src/components/sections/FormBlock/wide.astro +7 -0
- package/src/components/sections/Hero/cover.astro +176 -0
- package/src/components/sections/Hero/donate.astro +209 -0
- package/src/components/sections/Hero/field.astro +390 -0
- package/src/components/sections/Hero/index.astro +52 -0
- package/src/components/sections/Hero/overlap.astro +151 -0
- package/src/components/sections/Hero/rotating.astro +30 -0
- package/src/components/sections/Hero/split.astro +99 -0
- package/src/components/sections/Hero/statement.astro +55 -0
- package/src/components/sections/Hero/video.astro +399 -0
- package/src/components/sections/Issues/checklist.astro +204 -0
- package/src/components/sections/Issues/drawer.astro +237 -0
- package/src/components/sections/Issues/index.astro +115 -0
- package/src/components/sections/Issues/issues.ts +37 -0
- package/src/components/sections/Issues/numbered.astro +79 -0
- package/src/components/sections/Issues/rows.astro +137 -0
- package/src/components/sections/Issues/steps.astro +81 -0
- package/src/components/sections/Issues/tabs.astro +322 -0
- package/src/components/sections/Leaderboard/index.astro +29 -0
- package/src/components/sections/Lesson/Door.astro +121 -0
- package/src/components/sections/Lesson/LessonBody.astro +416 -0
- package/src/components/sections/Lesson/Rail.astro +182 -0
- package/src/components/sections/Lesson/Tracker.astro +137 -0
- package/src/components/sections/Lesson/index.astro +27 -0
- package/src/components/sections/Lesson/lesson.ts +233 -0
- package/src/components/sections/Lesson/reading.astro +21 -0
- package/src/components/sections/Map/CoverageLine.astro +19 -0
- package/src/components/sections/Map/RegionMap.astro +109 -0
- package/src/components/sections/Map/bar.astro +160 -0
- package/src/components/sections/Map/index.astro +192 -0
- package/src/components/sections/Map/map.astro +275 -0
- package/src/components/sections/MediaKit/BioCard.astro +108 -0
- package/src/components/sections/MediaKit/Downloads.astro +88 -0
- package/src/components/sections/MediaKit/index.astro +183 -0
- package/src/components/sections/MediaKit/kit.ts +140 -0
- package/src/components/sections/MediaKit/press.astro +189 -0
- package/src/components/sections/MediaKit/speaker.astro +205 -0
- package/src/components/sections/Membership/Benefits.astro +42 -0
- package/src/components/sections/Membership/Check.astro +12 -0
- package/src/components/sections/Membership/Price.astro +61 -0
- package/src/components/sections/Membership/compare.astro +312 -0
- package/src/components/sections/Membership/index.astro +281 -0
- package/src/components/sections/Membership/offers.ts +169 -0
- package/src/components/sections/Membership/tiers.astro +217 -0
- package/src/components/sections/MovementMap/index.astro +50 -0
- package/src/components/sections/MovementTicker/index.astro +43 -0
- package/src/components/sections/Pathways/Doors.astro +147 -0
- package/src/components/sections/Pathways/Head.astro +37 -0
- package/src/components/sections/Pathways/index.astro +27 -0
- package/src/components/sections/Pathways/pathways.ts +100 -0
- package/src/components/sections/Pathways/quiz.astro +286 -0
- package/src/components/sections/Pathways/tabs.astro +271 -0
- package/src/components/sections/PdfEmbed/card.astro +99 -0
- package/src/components/sections/PdfEmbed/index.astro +26 -0
- package/src/components/sections/PdfEmbed/medium.astro +7 -0
- package/src/components/sections/PdfEmbed/tall.astro +7 -0
- package/src/components/sections/Petition/coverage.astro +14 -0
- package/src/components/sections/Petition/image.astro +14 -0
- package/src/components/sections/Petition/index.astro +14 -0
- package/src/components/sections/Petition/map.astro +14 -0
- package/src/components/sections/PlatformPlanks/anchors.ts +66 -0
- package/src/components/sections/PlatformPlanks/columns.astro +171 -0
- package/src/components/sections/PlatformPlanks/index.astro +56 -0
- package/src/components/sections/PlatformPlanks/longread.astro +250 -0
- package/src/components/sections/PollBlock/index.astro +30 -0
- package/src/components/sections/Portfolio/featured.astro +343 -0
- package/src/components/sections/Portfolio/index.astro +571 -0
- package/src/components/sections/Portfolio/strip.astro +173 -0
- package/src/components/sections/Portfolio/work.ts +100 -0
- package/src/components/sections/Progress/MatchClock.astro +166 -0
- package/src/components/sections/Progress/compact.astro +150 -0
- package/src/components/sections/Progress/index.astro +32 -0
- package/src/components/sections/Progress/match.astro +126 -0
- package/src/components/sections/Progress/ring.astro +179 -0
- package/src/components/sections/QuickDonate/DonateCard.astro +149 -0
- package/src/components/sections/QuickDonate/Picker.astro +95 -0
- package/src/components/sections/QuickDonate/band.astro +164 -0
- package/src/components/sections/QuickDonate/bar.astro +208 -0
- package/src/components/sections/QuickDonate/donate.ts +126 -0
- package/src/components/sections/QuickDonate/hero.astro +143 -0
- package/src/components/sections/QuickDonate/index.astro +73 -0
- package/src/components/sections/QuickDonate/jar.astro +225 -0
- package/src/components/sections/QuickDonate/ladder.astro +248 -0
- package/src/components/sections/QuoteStrip/carousel.astro +340 -0
- package/src/components/sections/QuoteStrip/index.astro +58 -0
- package/src/components/sections/QuoteStrip/logos.astro +226 -0
- package/src/components/sections/QuoteStrip/quotes.ts +61 -0
- package/src/components/sections/QuoteStrip/single.astro +92 -0
- package/src/components/sections/QuoteStrip/ticker.astro +319 -0
- package/src/components/sections/QuoteStrip/wall.astro +113 -0
- package/src/components/sections/Resources/band.astro +93 -0
- package/src/components/sections/Resources/featured.astro +239 -0
- package/src/components/sections/Resources/index.astro +225 -0
- package/src/components/sections/Resources/resources.ts +92 -0
- package/src/components/sections/SampleBallot/Position.astro +39 -0
- package/src/components/sections/SampleBallot/ballot.ts +92 -0
- package/src/components/sections/SampleBallot/cards.astro +299 -0
- package/src/components/sections/SampleBallot/index.astro +326 -0
- package/src/components/sections/SampleBallot/measures.astro +172 -0
- package/src/components/sections/Scorecard/grades.astro +141 -0
- package/src/components/sections/Scorecard/index.astro +294 -0
- package/src/components/sections/Scorecard/promise.astro +167 -0
- package/src/components/sections/Scorecard/votes.ts +83 -0
- package/src/components/sections/SectionHead.astro +31 -0
- package/src/components/sections/Services/Button.astro +24 -0
- package/src/components/sections/Services/Check.astro +12 -0
- package/src/components/sections/Services/Includes.astro +42 -0
- package/src/components/sections/Services/compare.astro +328 -0
- package/src/components/sections/Services/index.astro +187 -0
- package/src/components/sections/Services/list.astro +181 -0
- package/src/components/sections/Services/services.ts +80 -0
- package/src/components/sections/ShareKit/Actions.astro +71 -0
- package/src/components/sections/ShareKit/Buttons.astro +62 -0
- package/src/components/sections/ShareKit/compact.astro +123 -0
- package/src/components/sections/ShareKit/index.astro +160 -0
- package/src/components/sections/ShareKit/share.ts +60 -0
- package/src/components/sections/ShareKit/strip.astro +173 -0
- package/src/components/sections/SignupCapture/band.astro +7 -0
- package/src/components/sections/SignupCapture/index.astro +103 -0
- package/src/components/sections/SignupCapture/inline.astro +7 -0
- package/src/components/sections/SocialFeed/index.astro +34 -0
- package/src/components/sections/StatCards/feature.astro +143 -0
- package/src/components/sections/StatCards/index.astro +51 -0
- package/src/components/sections/StatCards/ribbon.astro +113 -0
- package/src/components/sections/StatCards/wall.astro +177 -0
- package/src/components/sections/Store/featured.astro +14 -0
- package/src/components/sections/Store/index.astro +14 -0
- package/src/components/sections/Stories/Status.astro +42 -0
- package/src/components/sections/Stories/StoryCards.astro +171 -0
- package/src/components/sections/Stories/cards.astro +42 -0
- package/src/components/sections/Stories/index.astro +238 -0
- package/src/components/sections/Stories/stories.ts +97 -0
- package/src/components/sections/Stories/timeline.astro +135 -0
- package/src/components/sections/TeamGrid/board.astro +280 -0
- package/src/components/sections/TeamGrid/index.astro +71 -0
- package/src/components/sections/TeamGrid/list.astro +306 -0
- package/src/components/sections/TeamGrid/people.ts +178 -0
- package/src/components/sections/VideoEmbed/index.astro +52 -0
- package/src/components/sections/VideoEmbed/landscape.astro +7 -0
- package/src/components/sections/VideoEmbed/portrait.astro +7 -0
- package/src/components/sections/VideoEmbed/square.astro +7 -0
- package/src/components/sections/VoterTools/DaysLeft.astro +33 -0
- package/src/components/sections/VoterTools/Lookup.astro +43 -0
- package/src/components/sections/VoterTools/dates.astro +153 -0
- package/src/components/sections/VoterTools/index.astro +231 -0
- package/src/components/sections/VoterTools/polling.astro +144 -0
- package/src/components/sections/VoterTools/voter-tools.ts +71 -0
- package/src/components/sections/index.ts +373 -0
- package/src/components/sections/modules/Headed.astro +39 -0
- package/src/components/sections/modules/ModuleFrame.astro +181 -0
- package/src/components/store/CollectionPage.astro +40 -0
- package/src/components/store/ProductCard.astro +32 -0
- package/src/components/store/ProductGrid.astro +16 -0
- package/src/components/store/ProductPage.astro +146 -0
- package/src/components/store/StorePage.astro +137 -0
- package/src/components/store/StoreScript.astro +572 -0
- package/src/components/volunteer/ShiftLive.astro +90 -0
- package/src/components/volunteer/detail.astro +247 -0
- package/src/components/volunteer/list.astro +298 -0
- package/src/components/volunteer/props.ts +21 -0
- package/src/env.d.ts +22 -0
- package/src/index.d.ts +10 -0
- package/src/index.js +63 -0
- package/src/layouts/Site.astro +387 -0
- package/src/lib/academy.ts +146 -0
- package/src/lib/brand.ts +534 -0
- package/src/lib/chrome.ts +121 -0
- package/src/lib/donate-frame.ts +104 -0
- package/src/lib/entries.ts +190 -0
- package/src/lib/env.ts +34 -0
- package/src/lib/event-map-draw.ts +91 -0
- package/src/lib/events.ts +476 -0
- package/src/lib/fonts.ts +116 -0
- package/src/lib/footer.ts +24 -0
- package/src/lib/format.ts +160 -0
- package/src/lib/geo.ts +131 -0
- package/src/lib/live.ts +55 -0
- package/src/lib/menu.ts +48 -0
- package/src/lib/petition-text.ts +29 -0
- package/src/lib/reading.ts +17 -0
- package/src/lib/regions.ts +215 -0
- package/src/lib/section.ts +105 -0
- package/src/lib/shifts.ts +187 -0
- package/src/lib/store.ts +521 -0
- package/src/lib/stream-time.ts +20 -0
- package/src/lib/stream.ts +141 -0
- package/src/lib/visitor.ts +54 -0
- package/src/routes/catch-all.astro +435 -0
- package/src/routes/form.astro +30 -0
- package/src/routes/live.astro +49 -0
- package/src/routes/not-found.astro +18 -0
- package/src/routes/preview.astro +60 -0
- package/src/routes/setup.astro +62 -0
- package/src/routes/volunteer-shift.astro +61 -0
- package/src/styles/entries.css +784 -0
- package/src/styles/site.css +1910 -0
- package/src/styles/store.css +634 -0
- package/src/template.ts +182 -0
package/src/lib/store.ts
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
// The storefront the kit draws for every template (board 06 of the store
|
|
2
|
+
// design): static pages built from the Content API's catalog reads, readable
|
|
3
|
+
// with scripts off, and one small live island.
|
|
4
|
+
//
|
|
5
|
+
// /store the shop: the departments rail (each with
|
|
6
|
+
// its published count, only departments with
|
|
7
|
+
// something in them; the api lists only those
|
|
8
|
+
// whose storefront page is on, each under its
|
|
9
|
+
// nearest listed ancestor), the site's collections
|
|
10
|
+
// under it, the grid of listings (each card
|
|
11
|
+
// the link, no buttons) and a collection band
|
|
12
|
+
// carrying the page's one call (GET /products).
|
|
13
|
+
// /store/departments/<slug> the same, narrowed to a department and
|
|
14
|
+
// everything under it (`?department=`); a
|
|
15
|
+
// department the api does not list has none.
|
|
16
|
+
// Its rail carries the filter panel: a
|
|
17
|
+
// group per facet the api sends with
|
|
18
|
+
// `?department=`, each value a link that
|
|
19
|
+
// toggles `?attr=<key>:<value>` (repeatable,
|
|
20
|
+
// keys AND, a key's values OR) in the page's
|
|
21
|
+
// own address, which passes to GET /products.
|
|
22
|
+
// The shop's root has no facets.
|
|
23
|
+
// /store/<slug> one listing (GET /products/<slug>): its
|
|
24
|
+
// gallery, copy, attributes, related listings
|
|
25
|
+
// by collection, and the Product + Offer
|
|
26
|
+
// JSON-LD in the HTML. Its price,
|
|
27
|
+
// availability and Add to cart are the island
|
|
28
|
+
// (components/store/StoreScript.astro), which
|
|
29
|
+
// reads GET /products/live on view.
|
|
30
|
+
// /collections/<slug> one collection (GET /collections/<slug>):
|
|
31
|
+
// its cover, intro, the ordered grid, then one
|
|
32
|
+
// call back to the shop.
|
|
33
|
+
//
|
|
34
|
+
// A grid's later pages ride `?cursor=` as a list's do. Availability is the
|
|
35
|
+
// api's word, never a count. Prices are the build's; the island shows the
|
|
36
|
+
// live one when it differs.
|
|
37
|
+
//
|
|
38
|
+
// The cart belongs to the hosted checkout (`/s/<org>/checkout` on BackGold,
|
|
39
|
+
// on the host of a listing's `buy_url`), a different origin from the site's,
|
|
40
|
+
// so the site keeps its own cart and hands it over. The contract:
|
|
41
|
+
//
|
|
42
|
+
// - The island keeps the lines in localStorage on the site's origin under
|
|
43
|
+
// `gg-cart:<org slug>`, as an array in the checkout cart's own `CartItem`
|
|
44
|
+
// shape (the platform's frontend services/store-cart.ts): `variant_id`,
|
|
45
|
+
// `product_slug`, `product_name`, `variant_name`, `unit_price_cents`,
|
|
46
|
+
// `quantity`, `photo_url`, `needs_shipping`, and `answers` as
|
|
47
|
+
// `{ order?, units? }` keyed by label when the listing asks questions. A
|
|
48
|
+
// second add of the same item adds its quantity to the line, keeps the
|
|
49
|
+
// newer once-per-order answers and appends its units' answers, as the
|
|
50
|
+
// checkout's own add does.
|
|
51
|
+
// - The header's Cart link counts the lines' quantities. It, and the
|
|
52
|
+
// island's Check out after an add, go to
|
|
53
|
+
// `<checkout>#cart=<base64url of the UTF-8 JSON of the CartItem[]>`, in
|
|
54
|
+
// the fragment, never the query, so answers reach no server log. The
|
|
55
|
+
// checkout replaces its cart for the organization with the lines handed
|
|
56
|
+
// over (the site's cart is what the buyer saw), takes at most 50 lines and
|
|
57
|
+
// 64 KB, one line per item, a quantity of 1 to 99, names of at most 255
|
|
58
|
+
// characters, `units` no longer than the quantity and answers that are
|
|
59
|
+
// strings or lists of strings (a line that breaks one is dropped), and
|
|
60
|
+
// clears the fragment; it quotes prices
|
|
61
|
+
// again itself, so a line's `unit_price_cents` is for display only. The
|
|
62
|
+
// site forgets its cart in the click that hands it over (the link's
|
|
63
|
+
// address already carries the lines, so a new tab gets them too): the
|
|
64
|
+
// checkout owns the cart from then on, and an empty site cart's link goes
|
|
65
|
+
// to the checkout as it stands. A later hand-over of new lines replaces
|
|
66
|
+
// the checkout's cart, never adds to it.
|
|
67
|
+
|
|
68
|
+
import type { BackgoldContent, CollectionResponse, ProductDepartment, ProductDetail, ProductFacet, ProductResponse, ProductSummary, ProductsResponse } from '@backgold/content'
|
|
69
|
+
import { BackgoldApiError } from '@backgold/content'
|
|
70
|
+
import { PRODUCT_ATTR_FILTERS_MAX, PRODUCT_ATTR_KEY_PATTERN, PRODUCT_ATTR_VALUE_MAX } from '#contract/content-api'
|
|
71
|
+
|
|
72
|
+
/** The api's availability word: `in_stock`, `low`, `out` or `preorder`. */
|
|
73
|
+
export type ProductAvailability = ProductSummary['availability']
|
|
74
|
+
|
|
75
|
+
export const STORE_PATH = '/store'
|
|
76
|
+
export const COLLECTIONS_PATH = '/collections'
|
|
77
|
+
/** The segment under /store that a department's own page sits under. */
|
|
78
|
+
const DEPARTMENTS_SEGMENT = 'departments'
|
|
79
|
+
/** How many listings a grid page shows. */
|
|
80
|
+
export const STORE_PAGE_SIZE = 24
|
|
81
|
+
|
|
82
|
+
export const productPath = (slug: string): string => `${STORE_PATH}/${encodeURIComponent(slug)}`
|
|
83
|
+
export const collectionPath = (slug: string): string => `${COLLECTIONS_PATH}/${encodeURIComponent(slug)}`
|
|
84
|
+
export const departmentPath = (slug: string): string => `${STORE_PATH}/${DEPARTMENTS_SEGMENT}/${encodeURIComponent(slug)}`
|
|
85
|
+
|
|
86
|
+
/** Which storefront page a path is, or null for any other path. */
|
|
87
|
+
export type StoreRoute =
|
|
88
|
+
| { kind: 'store'; department: null }
|
|
89
|
+
| { kind: 'store'; department: string }
|
|
90
|
+
| { kind: 'product'; slug: string }
|
|
91
|
+
| { kind: 'collection'; slug: string }
|
|
92
|
+
|
|
93
|
+
const decoded = (segment: string): string | null => {
|
|
94
|
+
try {
|
|
95
|
+
const value = decodeURIComponent(segment)
|
|
96
|
+
return value && !value.includes('/') ? value : null
|
|
97
|
+
} catch {
|
|
98
|
+
return null
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function storeRouteOf(pathname: string): StoreRoute | null {
|
|
103
|
+
const segments = pathname.replace(/\/+$/, '').split('/').filter(Boolean)
|
|
104
|
+
if (segments[0] === 'store') {
|
|
105
|
+
if (segments.length === 1) return { kind: 'store', department: null }
|
|
106
|
+
if (segments.length === 2) {
|
|
107
|
+
const slug = decoded(segments[1]!)
|
|
108
|
+
return slug ? { kind: 'product', slug } : null
|
|
109
|
+
}
|
|
110
|
+
if (segments.length === 3 && segments[1] === DEPARTMENTS_SEGMENT) {
|
|
111
|
+
const slug = decoded(segments[2]!)
|
|
112
|
+
return slug ? { kind: 'store', department: slug } : null
|
|
113
|
+
}
|
|
114
|
+
return null
|
|
115
|
+
}
|
|
116
|
+
if (segments[0] === 'collections' && segments.length === 2) {
|
|
117
|
+
const slug = decoded(segments[1]!)
|
|
118
|
+
return slug ? { kind: 'collection', slug } : null
|
|
119
|
+
}
|
|
120
|
+
return null
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A read the api has no answer for: an api older than the catalog (404), or a cursor or slug it refuses (400). */
|
|
124
|
+
const absent = (error: unknown): boolean => error instanceof BackgoldApiError && (error.status === 404 || error.status === 400)
|
|
125
|
+
|
|
126
|
+
/** One page of the shop as the api answered it, and the filters that page is under. */
|
|
127
|
+
export interface Storefront {
|
|
128
|
+
shop: ProductsResponse
|
|
129
|
+
filters: StoreFilters
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* One page of the shop (a department's, with one, under its filters), or
|
|
134
|
+
* null when the api serves no catalog or refuses the page. Filters the api
|
|
135
|
+
* refuses (400 `BAD_ATTR`) are dropped: the page is read again without them,
|
|
136
|
+
* and drawn unfiltered.
|
|
137
|
+
*/
|
|
138
|
+
export async function storefront(
|
|
139
|
+
client: BackgoldContent,
|
|
140
|
+
opts: { department: string | null; cursor: string | null; filters: StoreFilters },
|
|
141
|
+
): Promise<Storefront | null> {
|
|
142
|
+
const read = (filters: StoreFilters) =>
|
|
143
|
+
client.products({
|
|
144
|
+
limit: STORE_PAGE_SIZE,
|
|
145
|
+
...(opts.department ? { department: opts.department } : {}),
|
|
146
|
+
...(filters.length > 0 ? { attr: attrOption(filters) } : {}),
|
|
147
|
+
...(opts.cursor ? { cursor: opts.cursor } : {}),
|
|
148
|
+
})
|
|
149
|
+
try {
|
|
150
|
+
return { shop: await read(opts.filters), filters: opts.filters }
|
|
151
|
+
} catch (error) {
|
|
152
|
+
if (opts.filters.length > 0 && error instanceof BackgoldApiError && error.code === 'BAD_ATTR') {
|
|
153
|
+
try {
|
|
154
|
+
return { shop: await read([]), filters: [] }
|
|
155
|
+
} catch (again) {
|
|
156
|
+
if (absent(again)) return null
|
|
157
|
+
throw again
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (absent(error)) return null
|
|
161
|
+
throw error
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ── The filter panel ─────────────────────────────────────────────────────────
|
|
166
|
+
|
|
167
|
+
/** The query name a department page's filters ride in, as GET /products takes them. */
|
|
168
|
+
export const FILTER_PARAM = 'attr'
|
|
169
|
+
|
|
170
|
+
/** One chosen attribute value. */
|
|
171
|
+
export interface StoreFilter {
|
|
172
|
+
key: string
|
|
173
|
+
value: string
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** The filters a page is under, in the order its address names them. */
|
|
177
|
+
export type StoreFilters = readonly StoreFilter[]
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The page address's `?attr=<key>:<value>` filters: each split at its first
|
|
181
|
+
* colon, a key the api would refuse, an empty or overlong value, a repeat and
|
|
182
|
+
* any past the api's limit left out, so a hand-typed address never fails the
|
|
183
|
+
* page.
|
|
184
|
+
*/
|
|
185
|
+
export function storeFiltersOf(params: URLSearchParams): StoreFilters {
|
|
186
|
+
const out: StoreFilter[] = []
|
|
187
|
+
for (const raw of params.getAll(FILTER_PARAM)) {
|
|
188
|
+
const colon = raw.indexOf(':')
|
|
189
|
+
if (colon === -1) continue
|
|
190
|
+
const key = raw.slice(0, colon)
|
|
191
|
+
const value = raw.slice(colon + 1)
|
|
192
|
+
if (!PRODUCT_ATTR_KEY_PATTERN.test(key) || value === '' || value.length > PRODUCT_ATTR_VALUE_MAX) continue
|
|
193
|
+
if (out.some((f) => f.key === key && f.value === value)) continue
|
|
194
|
+
out.push({ key, value })
|
|
195
|
+
if (out.length === PRODUCT_ATTR_FILTERS_MAX) break
|
|
196
|
+
}
|
|
197
|
+
return out
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The filters as `products({ attr })` takes them: a key's values together. */
|
|
201
|
+
function attrOption(filters: StoreFilters): Record<string, string[]> {
|
|
202
|
+
const attr: Record<string, string[]> = {}
|
|
203
|
+
for (const { key, value } of filters) (attr[key] ??= []).push(value)
|
|
204
|
+
return attr
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** A department page's address under these filters, and at a later page with a cursor. */
|
|
208
|
+
export function filteredPath(path: string, filters: StoreFilters, cursor: string | null = null): string {
|
|
209
|
+
const query = [
|
|
210
|
+
...filters.map((f) => `${FILTER_PARAM}=${encodeURIComponent(f.key)}:${encodeURIComponent(f.value)}`),
|
|
211
|
+
...(cursor ? [`cursor=${encodeURIComponent(cursor)}`] : []),
|
|
212
|
+
]
|
|
213
|
+
return query.length > 0 ? `${path}?${query.join('&')}` : path
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export interface FacetLink {
|
|
217
|
+
label: string
|
|
218
|
+
href: string
|
|
219
|
+
count: number
|
|
220
|
+
chosen: boolean
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export interface FacetGroup {
|
|
224
|
+
key: string
|
|
225
|
+
label: string
|
|
226
|
+
links: FacetLink[]
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** A facet value as the panel names it: the api's text, `true` and `false` as Yes and No. */
|
|
230
|
+
export function facetValueText(facet: Pick<ProductFacet, 'kind'>, value: string): string {
|
|
231
|
+
if (facet.kind === 'boolean' && (value === 'true' || value === 'false')) return value === 'true' ? 'Yes' : 'No'
|
|
232
|
+
return value
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* The filter panel: one group per facet, in the api's order, each value a
|
|
237
|
+
* link to the page with that value toggled (the other chosen values kept,
|
|
238
|
+
* the cursor dropped). A chosen value the api did not list (nothing matches
|
|
239
|
+
* it under the other filters) is kept at the end with a count of 0, so it
|
|
240
|
+
* can still be turned off.
|
|
241
|
+
*/
|
|
242
|
+
export function facetGroups(facets: readonly ProductFacet[], filters: StoreFilters, path: string): FacetGroup[] {
|
|
243
|
+
const isChosen = (key: string, value: string) => filters.some((f) => f.key === key && f.value === value)
|
|
244
|
+
const toggled = (key: string, value: string): StoreFilters =>
|
|
245
|
+
isChosen(key, value) ? filters.filter((f) => !(f.key === key && f.value === value)) : [...filters, { key, value }]
|
|
246
|
+
return facets.map((facet) => {
|
|
247
|
+
const values = [...facet.values]
|
|
248
|
+
for (const f of filters) if (f.key === facet.key && !values.some((v) => v.value === f.value)) values.push({ value: f.value, count: 0 })
|
|
249
|
+
return {
|
|
250
|
+
key: facet.key,
|
|
251
|
+
label: facet.label,
|
|
252
|
+
links: values.map((v) => ({
|
|
253
|
+
label: facetValueText(facet, v.value),
|
|
254
|
+
href: filteredPath(path, toggled(facet.key, v.value)),
|
|
255
|
+
count: v.count,
|
|
256
|
+
chosen: isChosen(facet.key, v.value),
|
|
257
|
+
})),
|
|
258
|
+
}
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** A listing for its page, or null (unknown, unpublished, or an api older than the catalog). */
|
|
263
|
+
export async function productPage(client: BackgoldContent, slug: string): Promise<ProductResponse | null> {
|
|
264
|
+
try {
|
|
265
|
+
return await client.product(slug)
|
|
266
|
+
} catch (error) {
|
|
267
|
+
if (absent(error)) return null
|
|
268
|
+
throw error
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** A collection for its page, or null (unknown, unpublished, or an api older than the catalog). */
|
|
273
|
+
export async function collectionPage(client: BackgoldContent, slug: string): Promise<CollectionResponse | null> {
|
|
274
|
+
try {
|
|
275
|
+
return await client.collection(slug)
|
|
276
|
+
} catch (error) {
|
|
277
|
+
if (absent(error)) return null
|
|
278
|
+
throw error
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// ── Words and numbers ────────────────────────────────────────────────────────
|
|
283
|
+
|
|
284
|
+
/** The availability words a card and the island show. */
|
|
285
|
+
export const AVAILABILITY_WORDS: Record<ProductAvailability, string> = {
|
|
286
|
+
in_stock: 'In stock',
|
|
287
|
+
low: 'Only a few left',
|
|
288
|
+
out: 'Sold out',
|
|
289
|
+
preorder: 'Preorder',
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** The chip class an availability word wears: quiet when in stock, the accent when few are left. */
|
|
293
|
+
export const AVAILABILITY_CHIPS: Record<ProductAvailability, string> = {
|
|
294
|
+
in_stock: 'chip-wash',
|
|
295
|
+
low: 'chip-accent',
|
|
296
|
+
out: '',
|
|
297
|
+
preorder: 'chip-wash',
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const WHOLE = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })
|
|
301
|
+
const CENTS = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 2 })
|
|
302
|
+
|
|
303
|
+
/** A price from cents: "$30" for whole dollars, "$24.50" otherwise. */
|
|
304
|
+
export function money(cents: number): string {
|
|
305
|
+
return cents % 100 === 0 ? WHOLE.format(cents / 100) : CENTS.format(cents / 100)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** A listing's price on a card: one price, or "From $X" when its items differ. */
|
|
309
|
+
export function priceLabel(product: Pick<ProductSummary, 'price_from_cents' | 'price_to_cents'>): string {
|
|
310
|
+
return product.price_from_cents === product.price_to_cents ? money(product.price_from_cents) : `From ${money(product.price_from_cents)}`
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ── The rail ─────────────────────────────────────────────────────────────────
|
|
314
|
+
|
|
315
|
+
export interface RailLink {
|
|
316
|
+
label: string
|
|
317
|
+
href: string
|
|
318
|
+
count: number
|
|
319
|
+
current: boolean
|
|
320
|
+
/** 0 for a top-level department, 1 for one under the department being shown. */
|
|
321
|
+
depth: 0 | 1
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const byPosition = (a: ProductDepartment, b: ProductDepartment) => a.position - b.position || a.label.localeCompare(b.label)
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* The departments rail: every top-level department with a published listing
|
|
328
|
+
* in it or under it, in the tree's order; on a department's page, its path
|
|
329
|
+
* is current and the departments under it with something in them follow it.
|
|
330
|
+
*/
|
|
331
|
+
export function departmentRail(departments: readonly ProductDepartment[], current: string | null): RailLink[] {
|
|
332
|
+
const live = departments.filter((d) => d.count > 0)
|
|
333
|
+
const bySlug = new Map(live.map((d) => [d.slug, d]))
|
|
334
|
+
const shown = current ? bySlug.get(current) ?? null : null
|
|
335
|
+
// The current department's root, so its top-level row is marked current too.
|
|
336
|
+
const path = new Set<string>()
|
|
337
|
+
for (let node = shown; node; node = node.parent_id ? live.find((d) => d.id === node!.parent_id) ?? null : null) path.add(node.id)
|
|
338
|
+
const link = (d: ProductDepartment, depth: 0 | 1): RailLink => ({ label: d.label, href: departmentPath(d.slug), count: d.count, current: path.has(d.id), depth })
|
|
339
|
+
const out: RailLink[] = []
|
|
340
|
+
for (const root of live.filter((d) => d.parent_id === null).sort(byPosition)) {
|
|
341
|
+
out.push(link(root, 0))
|
|
342
|
+
if (shown && path.has(root.id)) {
|
|
343
|
+
for (const child of live.filter((d) => d.parent_id === shown.id).sort(byPosition)) out.push(link(child, 1))
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return out
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** The department a page narrows to, by its slug; null when the tree has no such live department. */
|
|
350
|
+
export function departmentOf(departments: readonly ProductDepartment[], slug: string | null): ProductDepartment | null {
|
|
351
|
+
return slug ? departments.find((d) => d.slug === slug && d.count > 0) ?? null : null
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* How many published listings the shop has: the top-level departments'
|
|
356
|
+
* counts, or the page's own count when nothing is classified. GET /products
|
|
357
|
+
* carries no total, so when a top-level department's storefront page is off
|
|
358
|
+
* the api does not list it: its children come up as top-level departments
|
|
359
|
+
* and still count, but the listings filed on it directly are left out of the
|
|
360
|
+
* sum.
|
|
361
|
+
*/
|
|
362
|
+
export function shopCount(response: ProductsResponse): number {
|
|
363
|
+
const roots = response.departments.filter((d) => d.parent_id === null).reduce((n, d) => n + d.count, 0)
|
|
364
|
+
return Math.max(roots, response.products.length)
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// ── The product page ─────────────────────────────────────────────────────────
|
|
368
|
+
|
|
369
|
+
/** An attribute's value as the details table shows it; null leaves the row out. */
|
|
370
|
+
export function attributeText(value: ProductDetail['attributes'][number]['value']): string | null {
|
|
371
|
+
if (value === null) return null
|
|
372
|
+
if (typeof value === 'boolean') return value ? 'Yes' : 'No'
|
|
373
|
+
if (Array.isArray(value)) return value.length > 0 ? value.join(', ') : null
|
|
374
|
+
const trimmed = value.trim()
|
|
375
|
+
return trimmed === '' ? null : trimmed
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export interface RelatedGroup {
|
|
379
|
+
title: string
|
|
380
|
+
href: string | null
|
|
381
|
+
products: ProductSummary[]
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* The related listings grouped by the collection they share with the
|
|
386
|
+
* listing, in the order of the listing's own collections; one the api names
|
|
387
|
+
* without a shared collection goes under "More from the shop".
|
|
388
|
+
*/
|
|
389
|
+
export function relatedGroups(product: ProductDetail): RelatedGroup[] {
|
|
390
|
+
const groups = new Map<string, RelatedGroup>()
|
|
391
|
+
for (const ref of product.collections) groups.set(ref.slug, { title: ref.title, href: collectionPath(ref.slug), products: [] })
|
|
392
|
+
const loose: ProductSummary[] = []
|
|
393
|
+
for (const other of product.related) {
|
|
394
|
+
const shared = other.collections.find((c) => groups.has(c.slug))
|
|
395
|
+
if (shared) groups.get(shared.slug)!.products.push(other)
|
|
396
|
+
else loose.push(other)
|
|
397
|
+
}
|
|
398
|
+
const out = [...groups.values()].filter((g) => g.products.length > 0)
|
|
399
|
+
if (loose.length > 0) out.push({ title: 'More from the shop', href: null, products: loose })
|
|
400
|
+
return out
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const SCHEMA_AVAILABILITY: Record<ProductAvailability, string> = {
|
|
404
|
+
in_stock: 'https://schema.org/InStock',
|
|
405
|
+
low: 'https://schema.org/LimitedAvailability',
|
|
406
|
+
out: 'https://schema.org/OutOfStock',
|
|
407
|
+
preorder: 'https://schema.org/PreOrder',
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* The listing as schema.org Product JSON-LD with one Offer per item, from
|
|
412
|
+
* the build: the price in dollars and cents in USD, the availability, the
|
|
413
|
+
* SKU and (with a UPC) the gtin12, each Offer at the listing's page on this
|
|
414
|
+
* site.
|
|
415
|
+
*/
|
|
416
|
+
export function productJsonLd(product: ProductDetail, siteUrl: string): Record<string, unknown> {
|
|
417
|
+
const url = new URL(productPath(product.slug), siteUrl).toString()
|
|
418
|
+
const sku = product.items.find((i) => i.sku)?.sku ?? null
|
|
419
|
+
const description = product.seo.description ?? product.short_description
|
|
420
|
+
return {
|
|
421
|
+
'@context': 'https://schema.org',
|
|
422
|
+
'@type': 'Product',
|
|
423
|
+
name: product.name,
|
|
424
|
+
url,
|
|
425
|
+
...(description ? { description } : {}),
|
|
426
|
+
...(product.photos.length > 0 ? { image: product.photos.map((p) => p.url) } : {}),
|
|
427
|
+
...(sku ? { sku } : {}),
|
|
428
|
+
...(product.manufacturer ? { brand: { '@type': 'Brand', name: product.manufacturer.name } } : {}),
|
|
429
|
+
...(product.department.length > 0 ? { category: product.department.map((d) => d.label).join(' > ') } : {}),
|
|
430
|
+
offers: product.items.map((item) => ({
|
|
431
|
+
'@type': 'Offer',
|
|
432
|
+
...(item.sku ? { sku: item.sku } : {}),
|
|
433
|
+
...(item.upc ? { gtin12: item.upc } : {}),
|
|
434
|
+
name: item.name,
|
|
435
|
+
price: (item.price_cents / 100).toFixed(2),
|
|
436
|
+
priceCurrency: 'USD',
|
|
437
|
+
availability: SCHEMA_AVAILABILITY[item.availability],
|
|
438
|
+
url,
|
|
439
|
+
})),
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Where the order goes, as the one line under the availability word (never
|
|
445
|
+
* a choice): picked up at the store, shipped to a licensed dealer the buyer
|
|
446
|
+
* names at checkout, or shipped to the buyer when there is something to
|
|
447
|
+
* ship; null for a listing with nothing to ship to the buyer.
|
|
448
|
+
*/
|
|
449
|
+
export function shipLine(product: Pick<ProductDetail, 'ship_to' | 'ships'>): string | null {
|
|
450
|
+
if (product.ship_to === 'store') return 'Pick up at the store'
|
|
451
|
+
if (product.ship_to === 'third_party') return 'Ships to a licensed dealer you name at checkout'
|
|
452
|
+
return product.ships ? 'Ships to you' : null
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** The sentence a listing that counts as a political contribution carries before Add to cart (DS §18.1). */
|
|
456
|
+
export const contributionLine = (siteName: string): string => `A purchase is a political contribution to ${siteName}.`
|
|
457
|
+
|
|
458
|
+
// ── The checkout ─────────────────────────────────────────────────────────────
|
|
459
|
+
|
|
460
|
+
export interface Checkout {
|
|
461
|
+
/** The organization's slug on BackGold, the cart's key (`gg-cart:<org>`). */
|
|
462
|
+
org: string
|
|
463
|
+
/** The hosted checkout: `<origin>/s/<org>/checkout`. */
|
|
464
|
+
url: string
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/** Where a listing's cart checks out, read off its `buy_url` (`<origin>/s/<org>/<slug>`); null when it is not that shape. */
|
|
468
|
+
export function checkoutOf(buyUrl: string): Checkout | null {
|
|
469
|
+
try {
|
|
470
|
+
const url = new URL(buyUrl)
|
|
471
|
+
const m = /^\/s\/([^/]+)\/[^/]+\/?$/.exec(url.pathname)
|
|
472
|
+
if (!m || (url.protocol !== 'https:' && url.protocol !== 'http:')) return null
|
|
473
|
+
const org = decodeURIComponent(m[1]!)
|
|
474
|
+
return { org, url: `${url.origin}/s/${encodeURIComponent(org)}/checkout` }
|
|
475
|
+
} catch {
|
|
476
|
+
return null
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** The checkout of the first listing that names one. */
|
|
481
|
+
export function checkoutFor(products: readonly Pick<ProductSummary, 'buy_url'>[]): Checkout | null {
|
|
482
|
+
for (const p of products) {
|
|
483
|
+
const found = checkoutOf(p.buy_url)
|
|
484
|
+
if (found) return found
|
|
485
|
+
}
|
|
486
|
+
return null
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** What the island reads about the listing from the page: the build's view of it. */
|
|
490
|
+
export interface IslandProduct {
|
|
491
|
+
id: string
|
|
492
|
+
slug: string
|
|
493
|
+
name: string
|
|
494
|
+
photo_url: string | null
|
|
495
|
+
ships: boolean
|
|
496
|
+
/** Where the order goes: a pickup at the store ships nothing to the buyer. */
|
|
497
|
+
ship_to: ProductDetail['ship_to']
|
|
498
|
+
buy_url: string
|
|
499
|
+
/** When the build read the listing (its `updated_at`), for "As of the last build". */
|
|
500
|
+
built_at: string
|
|
501
|
+
items: Array<{ id: string; name: string; price_cents: number; compare_at_cents: number | null; availability: ProductAvailability }>
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
export function islandProduct(product: ProductDetail): IslandProduct {
|
|
505
|
+
return {
|
|
506
|
+
id: product.id,
|
|
507
|
+
slug: product.slug,
|
|
508
|
+
name: product.name,
|
|
509
|
+
photo_url: product.photos[0]?.url ?? null,
|
|
510
|
+
ships: product.ships,
|
|
511
|
+
ship_to: product.ship_to,
|
|
512
|
+
buy_url: product.buy_url,
|
|
513
|
+
built_at: product.updated_at,
|
|
514
|
+
items: product.items.map(({ id, name, price_cents, compare_at_cents, availability }) => ({ id, name, price_cents, compare_at_cents, availability })),
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/** The item a page opens on: the first one that can be bought, else the first. */
|
|
519
|
+
export function firstBuyable<T extends { availability: ProductAvailability }>(items: readonly T[]): T | null {
|
|
520
|
+
return items.find((i) => i.availability !== 'out') ?? items[0] ?? null
|
|
521
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// A live stream's moments in words, for the server and the browser alike (no
|
|
2
|
+
// imports, so components/live/LocalTime.astro's script can carry it): the
|
|
3
|
+
// server writes them in UTC, and the visitor's browser writes them again in
|
|
4
|
+
// its own zone with the same words.
|
|
5
|
+
|
|
6
|
+
/** `time`: "7:02 PM CDT" (when a stream went live). `day`: "Thursday, October 30 at 6:00 PM CDT" (when the next one starts). */
|
|
7
|
+
export type StreamMoment = 'time' | 'day'
|
|
8
|
+
|
|
9
|
+
const OPTIONS: Record<StreamMoment, Intl.DateTimeFormatOptions> = {
|
|
10
|
+
time: { hour: 'numeric', minute: '2-digit', timeZoneName: 'short' },
|
|
11
|
+
day: { weekday: 'long', month: 'long', day: 'numeric', hour: 'numeric', minute: '2-digit', timeZoneName: 'short' },
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The moment in words, in `timeZone` (the reader's own when left out); null for a value that is not a date. */
|
|
15
|
+
export function streamTime(value: string | null | undefined, moment: StreamMoment, timeZone?: string): string | null {
|
|
16
|
+
if (!value) return null
|
|
17
|
+
const date = new Date(value)
|
|
18
|
+
if (Number.isNaN(date.getTime())) return null
|
|
19
|
+
return new Intl.DateTimeFormat('en-US', timeZone ? { ...OPTIONS[moment], timeZone } : OPTIONS[moment]).format(date)
|
|
20
|
+
}
|