diff --git a/src/Trips/TripsPage.tsx b/src/Trips/TripsPage.tsx index 68587fb..3766739 100644 --- a/src/Trips/TripsPage.tsx +++ b/src/Trips/TripsPage.tsx @@ -1,5 +1,5 @@ import { Fragment, useCallback, useEffect, useState, useRef } from "react"; -import { Feature, Leg, TripPattern } from "../types"; +import { Feature } from "../types"; import { ActionPanel, Action, Color, List, Icon } from "@raycast/api"; import { fetchTrip } from "../api"; import { @@ -10,6 +10,7 @@ import { formatDestinationDisplay, formatAsDate, } from "../utils"; +import { Leg, TripPattern } from "../api/tripsQuery"; type Props = { origin: Feature; @@ -122,7 +123,7 @@ const getTripAccessories = ( ): List.Item.Accessory[] => { // Filter out insignificant walk distances const legs = trip.legs.filter( - (leg) => leg.fromPlace.quay.stopPlace.id !== leg.toPlace.quay.stopPlace.id, + (leg) => leg.fromPlace.quay.stopPlace?.id !== leg.toPlace.quay.stopPlace?.id, ); let accessories: List.Item.Accessory[] = legs.map((leg) => ({ diff --git a/src/api/fragments.ts b/src/api/fragments.ts index 89e1058..96cf4f1 100644 --- a/src/api/fragments.ts +++ b/src/api/fragments.ts @@ -1,5 +1,22 @@ import { DestinationDisplay, DirectionType, TransportMode } from "../types"; +export type Quay = { + id: string; + name: string; + publicCode?: string; + stopPlace?: { id: string }; +}; +export const quayFragment = ` +fragment Q on Quay { + id + name + publicCode + stopPlace { + id + } +} +`; + export type EstimatedCall = { date: string; expectedDepartureTime: string | null; diff --git a/src/api/index.ts b/src/api/index.ts index bbe18ba..848be62 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,7 +1,7 @@ import fetch from "node-fetch"; import { getDepartureQueryDocument, QuayDepartures } from "./departuresQuery"; -import { tripsQueryDocument } from "./tripsQuery"; -import { Feature, QuayLineFavorites, StopPlaceQuayDeparturesQuery, TripQuery } from "../types"; +import { TripQuery, tripsQueryDocument } from "./tripsQuery"; +import { Feature, QuayLineFavorites, StopPlaceQuayDeparturesQuery } from "../types"; const CLIENT_NAME = "raycast-norwegian-public-transport"; diff --git a/src/api/tripsQuery.ts b/src/api/tripsQuery.ts index 329614f..6e7f328 100644 --- a/src/api/tripsQuery.ts +++ b/src/api/tripsQuery.ts @@ -1,3 +1,36 @@ +import { DestinationDisplay, TransportMode } from "../types"; +import { authorityFragment, Line, lineFragment, Quay, quayFragment } from "./fragments"; + +export type TripQuery = { + trip: { + nextPageCursor: string; + tripPatterns: TripPattern[]; + }; +}; +export type TripPattern = { + expectedStartTime: string; + expectedEndTime: string; + /** Duration in seconds */ + duration: number; + /** Distance in meters */ + distance: number; + legs: Leg[]; +}; +export type Leg = { + mode: TransportMode; + transportSubmode: string; + /** Distance in meters */ + distance: number; + expectedStartTime: string; + expectedEndTime: string; + line?: Line; + fromPlace: { quay: Quay }; + toPlace: { quay: Quay }; + fromEstimatedCall?: { + destinationDisplay?: DestinationDisplay; + }; +}; + export const tripsQueryDocument = ` query planTrip($fromPlace: String, $toPlace: String, $pageCursor: String) { trip( @@ -23,32 +56,16 @@ query planTrip($fromPlace: String, $toPlace: String, $pageCursor: String) { distance fromPlace { quay { - publicCode - name - stopPlace { - id - } + ...Q } } toPlace { quay { - publicCode - name - stopPlace { - id - } + ...Q } } line { - id - description - transportMode - publicCode - authority { - id - name - url - } + ...L } fromEstimatedCall { destinationDisplay { @@ -60,4 +77,7 @@ query planTrip($fromPlace: String, $toPlace: String, $pageCursor: String) { } } } +${quayFragment} +${lineFragment} +${authorityFragment} `; diff --git a/src/types.ts b/src/types.ts index 5224f5e..e99c10d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,5 @@ import { DeparturesQuery, QuayDepartures } from "./api/departuresQuery"; -import { Line } from "./api/fragments"; +import { Quay } from "./api/fragments"; export type QuayLineFavorites = { stopPlaceId: string; @@ -31,36 +31,6 @@ export enum TransportMode { Foot = "foot", } -export type Leg = { - mode: TransportMode; - transportSubmode: string; // TODO: make enums like we have for TransportMode? - distance: number; - expectedStartTime: string; - expectedEndTime: string; - line?: Line; - fromPlace: { - quay: { - publicCode?: string; - name: string; - stopPlace: { - id: string; - }; - }; - }; - toPlace: { - quay: { - publicCode?: string; - name: string; - stopPlace: { - id: string; - }; - }; - }; - fromEstimatedCall?: { - destinationDisplay?: DestinationDisplay; - }; -}; - export type DestinationDisplay = { frontText?: string; via?: string[]; @@ -83,13 +53,7 @@ export type StopPlace = { id: string; latitude?: number; longitude?: number; - quays?: Array<{ - id: string; - description?: string; - name: string; - publicCode?: string; - stopPlace?: { id: string }; - }>; + quays?: Quay[]; }; export type Feature = { @@ -120,18 +84,3 @@ export type VenueCategory = | "ferryStop" | "liftStation" | "vehicleRailInterchange"; - -export type TripPattern = { - expectedStartTime: string; // ISO 8601 - expectedEndTime: string; // ISO 8601 - duration: number; // seconds - distance: number; // meters - legs: Leg[]; -}; - -export type TripQuery = { - trip: { - nextPageCursor: string; - tripPatterns: TripPattern[]; - }; -};