Skip to content

Commit

Permalink
refactor: share trip fragments with departures
Browse files Browse the repository at this point in the history
  • Loading branch information
rosvik committed Jan 22, 2025
1 parent 69e288a commit 25b92c4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 76 deletions.
5 changes: 3 additions & 2 deletions src/Trips/TripsPage.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,6 +10,7 @@ import {
formatDestinationDisplay,
formatAsDate,
} from "../utils";
import { Leg, TripPattern } from "../api/tripsQuery";

type Props = {
origin: Feature;
Expand Down Expand Up @@ -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) => ({
Expand Down
17 changes: 17 additions & 0 deletions src/api/fragments.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
58 changes: 39 additions & 19 deletions src/api/tripsQuery.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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 {
Expand All @@ -60,4 +77,7 @@ query planTrip($fromPlace: String, $toPlace: String, $pageCursor: String) {
}
}
}
${quayFragment}
${lineFragment}
${authorityFragment}
`;
55 changes: 2 additions & 53 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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[];
Expand All @@ -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 = {
Expand Down Expand Up @@ -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[];
};
};

0 comments on commit 25b92c4

Please sign in to comment.