diff --git a/.github/workflows/deploy-web.yml b/.github/workflows/deploy-web.yml new file mode 100644 index 00000000..f58a1234 --- /dev/null +++ b/.github/workflows/deploy-web.yml @@ -0,0 +1,112 @@ +name: Deploy Web + +on: + pull_request: + workflow_dispatch: + workflow_call: + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} + cancel-in-progress: true + +permissions: + contents: read + deployments: write + pull-requests: write + +env: + # Must match mise.toml, otherwise wrangler-action runs `pnpm add wrangler`, which pnpm blocks. + WRANGLER_VERSION: 4.156.0 + +jobs: + deploy: + runs-on: ubuntu-latest + + # Fork PRs don't get repository secrets. + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + + environment: + name: ${{ github.event_name == 'pull_request' && 'web-preview' || 'web-production' }} + url: ${{ steps.deploy.outputs.deployment-url }} + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Install mise and dependencies + uses: jdx/mise-action@v3 + with: + install_args: node pnpm github:protomaps/go-pmtiles npm:wrangler + + - name: Install Dependencies + run: pnpm install + + - name: Cache Basemap + id: basemap + uses: actions/cache@v4 + with: + path: | + static/map/cstat.pmtiles + static/map/sprites + static/map/fonts + key: basemap-${{ hashFiles('pmtiles/cstat.sh') }} + + - name: Build Basemap + if: steps.basemap.outputs.cache-hit != 'true' + run: pnpm pmtiles + + # Content-addressed, so previews never overwrite the basemap production reads. + - name: Name Basemap Object + id: tiles + run: echo "key=cstat-$(sha256sum static/map/cstat.pmtiles | cut -c1-16).pmtiles" >> "$GITHUB_OUTPUT" + + - name: Build Web Assets + run: pnpm build + env: + VITE_PMTILES_URL: ${{ vars.MAP_TILES_URL }}/${{ steps.tiles.outputs.key }} + + # R2's REST object API rejects bucket-scoped tokens, so this uses the S3 API instead of wrangler. + - name: Upload Basemap to R2 + run: >- + aws s3 cp static/map/cstat.pmtiles "s3://${{ vars.MAP_TILES_BUCKET }}/${{ steps.tiles.outputs.key }}" + --endpoint-url "https://${{ vars.CLOUDFLARE_ACCOUNT_ID }}.r2.cloudflarestorage.com" + --content-type application/vnd.pmtiles + --cache-control "public, max-age=31536000, immutable" + env: + AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }} + AWS_DEFAULT_REGION: auto + + # The web build reads the basemap from R2. + - name: Remove Bundled Basemap + run: rm build/map/cstat.pmtiles + + - name: Deploy to Cloudflare Pages + id: deploy + uses: cloudflare/wrangler-action@v3 + with: + wranglerVersion: ${{ env.WRANGLER_VERSION }} + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} + command: pages deploy build --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=${{ github.event.pull_request.number && format('pr{0}', github.event.pull_request.number) || 'main' }} + + # Edits the bot's previous comment so the PR keeps one preview link. + - name: Comment Preview URL + if: github.event_name == 'pull_request' + run: | + gh pr comment "$PR_NUMBER" --edit-last --create-if-none --body "$(cat <({ baseUrl: API_URL }); +// Browsers can't set the User-Agent, so the platform and version also go in headers. +const client = createClient({ + baseUrl: API_URL, + headers: { + 'X-App-Platform': Capacitor.getPlatform(), + 'X-App-Version': APP_VERSION, + }, +}); export async function getSupportedVersion(): Promise { const { data, error } = await client.GET('/api/version/supported'); diff --git a/src/lib/components/ui/bottom-sheet/sheet.svelte b/src/lib/components/ui/bottom-sheet/sheet.svelte index 5ae44496..7c511a2e 100644 --- a/src/lib/components/ui/bottom-sheet/sheet.svelte +++ b/src/lib/components/ui/bottom-sheet/sheet.svelte @@ -2,6 +2,7 @@ import { beforeNavigate, goto } from '$app/navigation'; import * as Card from '$lib/components/ui/card'; import { cn, type WithElementRef } from '$lib/utils.js'; + import { SIDE_SHEET_GUTTER_PX, SIDE_SHEET_WIDTH_PX, sideSheetLayout } from '$lib/utils/layout'; import { clamp, last, minBy, range, throttle } from 'lodash-es'; import { onMount, setContext, tick } from 'svelte'; import type { HTMLAttributes } from 'svelte/elements'; @@ -632,7 +633,15 @@ {#if visible}
MapLibreGL.Map | null; getBounds: () => MapLibreGL.LngLatBounds | null; @@ -79,9 +82,7 @@ const protocol = new Protocol(); MapLibreGL.addProtocol('pmtiles', protocol.tile); - const PMTILE_URL = '/map/cstat.pmtiles'; - - const p = new PMTiles(PMTILE_URL); + const p = new PMTiles(PMTILES_URL); // The JS code and the map renderer share this one instance. protocol.add(p); @@ -94,7 +95,6 @@ const mapInstance = new MapLibreGL.Map({ container: mapContainer, - style: requestedStyle, renderWorldCopies: false, // TODO move attribution elsewhere attributionControl: false, @@ -120,6 +120,7 @@ loadedStyle = requestedStyle; }); + mapInstance.setStyle(requestedStyle, { transformStyle }); map = mapInstance; }); @@ -132,7 +133,7 @@ requestedStyle = style; loadedStyle = null; - map.setStyle(style, { diff: false }); + map.setStyle(style, { diff: false, transformStyle }); }); onDestroy(() => { diff --git a/src/lib/components/ui/map/MapControls.svelte b/src/lib/components/ui/map/MapControls.svelte index b5620463..add4a8e6 100644 --- a/src/lib/components/ui/map/MapControls.svelte +++ b/src/lib/components/ui/map/MapControls.svelte @@ -25,8 +25,8 @@ const loaded = $derived(mapCtx.isLoaded()); const positionClasses = { - 'top-left': 'top-16 left-2', - 'top-right': 'top-16 right-2', + 'top-left': 'top-[calc(env(safe-area-inset-top)+8px)] left-2', + 'top-right': 'top-[calc(env(safe-area-inset-top)+8px)] right-2', 'bottom-left': 'bottom-2 left-2', 'bottom-right': 'bottom-10 right-2', }; diff --git a/src/lib/components/ui/map/map-style.test.ts b/src/lib/components/ui/map/map-style.test.ts new file mode 100644 index 00000000..b00c95a7 --- /dev/null +++ b/src/lib/components/ui/map/map-style.test.ts @@ -0,0 +1,47 @@ +import type { StyleSpecification } from 'maplibre-gl'; +import { describe, expect, it } from 'vitest'; +import { resolveMapStyle } from './map-style'; + +const BASE_URL = 'https://web.maroonrides.app/route/1'; +const R2_PMTILES = 'https://tiles.maroonrides.app/cstat-abc.pmtiles'; + +const STYLE: StyleSpecification = { + version: 8, + sprite: '/map/sprites/light', + glyphs: '/map/fonts/{fontstack}/{range}.pbf', + sources: { + protomaps: { type: 'vector', url: 'pmtiles:///map/cstat.pmtiles', attribution: 'OSM' }, + routes: { type: 'geojson', data: { type: 'FeatureCollection', features: [] } }, + }, + layers: [], +}; + +describe('resolveMapStyle', () => { + it('points the basemap at the given pmtiles url', () => { + const style = resolveMapStyle(BASE_URL, R2_PMTILES)(undefined, STYLE); + + expect(style.sources.protomaps).toEqual({ + type: 'vector', + url: `pmtiles://${R2_PMTILES}`, + attribution: 'OSM', + }); + }); + + it('keeps the bundled basemap url when given the bundled path', () => { + const style = resolveMapStyle(BASE_URL, '/map/cstat.pmtiles')(undefined, STYLE); + + expect(style.sources.protomaps).toMatchObject({ url: 'pmtiles:///map/cstat.pmtiles' }); + }); + + it('makes the sprite url absolute against the page origin', () => { + const style = resolveMapStyle(BASE_URL, R2_PMTILES)(undefined, STYLE); + + expect(style.sprite).toBe('https://web.maroonrides.app/map/sprites/light'); + }); + + it('leaves other sources untouched', () => { + const style = resolveMapStyle(BASE_URL, R2_PMTILES)(undefined, STYLE); + + expect(style.sources.routes).toBe(STYLE.sources.routes); + }); +}); diff --git a/src/lib/components/ui/map/map-style.ts b/src/lib/components/ui/map/map-style.ts new file mode 100644 index 00000000..5e41d053 --- /dev/null +++ b/src/lib/components/ui/map/map-style.ts @@ -0,0 +1,22 @@ +import type { TransformStyleFunction, VectorSourceSpecification } from 'maplibre-gl'; + +/** Web builds point this at R2, since Cloudflare Pages ignores the Range headers PMTiles needs. */ +export const PMTILES_URL: string = import.meta.env.VITE_PMTILES_URL ?? '/map/cstat.pmtiles'; + +const BASEMAP_SOURCE = 'protomaps'; + +export function resolveMapStyle(baseUrl: string, pmtilesUrl: string): TransformStyleFunction { + return (_previous, next) => { + const basemap = next.sources[BASEMAP_SOURCE] as VectorSourceSpecification; + + return { + ...next, + // MapLibre rejects relative sprite URLs. + sprite: typeof next.sprite === 'string' ? new URL(next.sprite, baseUrl).href : next.sprite, + sources: { + ...next.sources, + [BASEMAP_SOURCE]: { ...basemap, url: `pmtiles://${pmtilesUrl}` }, + }, + }; + }; +} diff --git a/src/lib/managers/map.manager.svelte.ts b/src/lib/managers/map.manager.svelte.ts index 779725b6..8ce7d9c1 100644 --- a/src/lib/managers/map.manager.svelte.ts +++ b/src/lib/managers/map.manager.svelte.ts @@ -1,5 +1,6 @@ import type { Location, Route, Stop } from '$lib/data/types'; import { findBoundingBox } from '$lib/utils/geo'; +import { SIDE_SHEET_OCCUPIED_PX, sideSheetLayout } from '$lib/utils/layout'; import MapLibreGL, { type VectorSourceSpecification } from 'maplibre-gl'; export type MapSelection = { type: 'bus'; id: string } | { type: 'stop'; id: string } | null; @@ -92,10 +93,30 @@ class MapManager { center: [stop.location.longitude, stop.location.latitude], zoom: 17, duration: 750, - offset: [0, -this.mapHeight * 0.225], + offset: sideSheetLayout.current + ? [SIDE_SHEET_OCCUPIED_PX / 2, 0] + : [0, -this.mapHeight * 0.225], }); } + #sheetPadding(): MapLibreGL.PaddingOptions { + if (sideSheetLayout.current) { + return { + top: this.mapHeight * 0.1, + bottom: this.mapHeight * 0.1, + left: SIDE_SHEET_OCCUPIED_PX + 40, + right: 40, + }; + } + + return { + top: this.mapHeight * 0.1, + bottom: this.mapHeight * 0.45 + 8, + left: 40, + right: 40, + }; + } + zoomToFitPoints(points: Location[]) { const [min, max] = findBoundingBox(points); if (!min || !max) return; @@ -108,12 +129,7 @@ class MapManager { [max.longitude, max.latitude], ], { - padding: { - top: this.mapHeight * 0.1, - bottom: this.mapHeight * 0.45 + 8, - left: 40, - right: 40, - }, + padding: this.#sheetPadding(), duration: 1000, maxZoom: 16, }, diff --git a/src/lib/utils/layout.ts b/src/lib/utils/layout.ts new file mode 100644 index 00000000..e34a1641 --- /dev/null +++ b/src/lib/utils/layout.ts @@ -0,0 +1,7 @@ +import { MediaQuery } from 'svelte/reactivity'; + +export const SIDE_SHEET_WIDTH_PX = 420; +export const SIDE_SHEET_GUTTER_PX = 16; +export const SIDE_SHEET_OCCUPIED_PX = SIDE_SHEET_WIDTH_PX + SIDE_SHEET_GUTTER_PX; + +export const sideSheetLayout = new MediaQuery('(orientation: landscape) and (min-width: 768px)');