From 2c97e985deeb1a368e178cc4668a7813317a26f6 Mon Sep 17 00:00:00 2001 From: danthonywalker Date: Fri, 13 Oct 2023 23:42:28 -0500 Subject: [PATCH] Rename prospective to perspective --- src/commands/carsized/carsized.manager.ts | 10 +++++----- src/commands/carsized/constants.ts | 2 +- src/commands/carsized/context.ts | 12 ++++++++++-- src/commands/carsized/index.ts | 18 +++++++++--------- src/commands/carsized/types.ts | 6 ++++-- src/commands/carsized/ui/front.button.ts | 4 ++-- src/commands/carsized/ui/index.ts | 20 ++++++++++---------- src/commands/carsized/ui/rear.button.ts | 4 ++-- src/commands/carsized/ui/side.button.ts | 4 ++-- 9 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/commands/carsized/carsized.manager.ts b/src/commands/carsized/carsized.manager.ts index ada134f..5e64f45 100644 --- a/src/commands/carsized/carsized.manager.ts +++ b/src/commands/carsized/carsized.manager.ts @@ -10,7 +10,7 @@ import Environment from "../../shared/environment"; import { isNonNullable } from "../../shared/nullable"; import { usePage } from "../../shared/puppeteer"; import RedisKey, * as redis from "../../shared/redis"; -import { Prospective } from "./constants"; +import { Perspective } from "./constants"; const logger = loggerFactory({ name: __filename, @@ -36,17 +36,17 @@ export const searchCars = (carName: string) => { export const compareCars = async ({ firstCar, - prospective, + perspective, secondCar, units, }: CompareCars) => usePage(async (page) => { await page.setJavaScriptEnabled(false); const compareSegment = `${firstCar.id}-vs-${secondCar.id}`; - const prospectiveSegment = - prospective === Prospective.Side ? "" : prospective; + const perspectiveSegment = + perspective === Perspective.Side ? "" : perspective; // prettier-ignore - await page.goto(`${CarsizedBaseUrl}/cars/compare/${compareSegment}/${prospectiveSegment}?units=${units}`); + await page.goto(`${CarsizedBaseUrl}/cars/compare/${compareSegment}/${perspectiveSegment}?units=${units}`); const contentHandle = await page.$("div.flowcontent"); assert(contentHandle !== null); diff --git a/src/commands/carsized/constants.ts b/src/commands/carsized/constants.ts index d22370f..dc80d3a 100644 --- a/src/commands/carsized/constants.ts +++ b/src/commands/carsized/constants.ts @@ -3,7 +3,7 @@ export enum Units { Metric = "metric", } -export enum Prospective { +export enum Perspective { Front = "front", Rear = "rear", Side = "side", diff --git a/src/commands/carsized/context.ts b/src/commands/carsized/context.ts index a69e7e9..f930286 100644 --- a/src/commands/carsized/context.ts +++ b/src/commands/carsized/context.ts @@ -24,10 +24,20 @@ const logger = loggerFactory({ name: __filename, }); +const session = new Session(); +export default session; + export const compareCarsUi = async ( context: Context, interaction: Interaction, ) => { + // "perspective" was wrongly spelled "prospective" initially + // Use new spelling while supporting older sessions + if (context.perspective === undefined) { + context.perspective = context.prospective; + context = await session.update(context, interaction); + } + const response = interaction.isCommand() ? await interaction.deferReply() : await interaction.update(UI.compareCars(context)); @@ -57,5 +67,3 @@ export const compareCarsUi = async ( return response; }; - -export default new Session(); diff --git a/src/commands/carsized/index.ts b/src/commands/carsized/index.ts index 0f24f76..aa7afbc 100644 --- a/src/commands/carsized/index.ts +++ b/src/commands/carsized/index.ts @@ -7,13 +7,13 @@ import { registerCommand, toChoices } from "../../shared/discord"; import Environment from "../../shared/environment"; import onAutocomplete from "./autocomplete"; import * as carsized from "./carsized.manager"; -import { Prospective, Units } from "./constants"; +import { Perspective, Units } from "./constants"; import session, * as withContext from "./context"; import UI from "./ui"; enum Option { FirstCar = "first-car", - Prospective = "prospective", + Perspective = "perspective", SecondCar = "second-car", Units = "units", } @@ -37,9 +37,9 @@ const json = new SlashCommandBuilder() ) .addStringOption((option) => option - .setName(Option.Prospective) - .setDescription("Prospective of the comparison") - .setChoices(...toChoices(Prospective)), + .setName(Option.Perspective) + .setDescription("Perspective of the comparison") + .setChoices(...toChoices(Perspective)), ) .addStringOption((option) => option @@ -63,16 +63,16 @@ const onCommand = async (interaction: CommandInteraction) => { if (secondCar === undefined) return interaction.reply(UI.noCarExists(secondCarId)); - const prospectiveOption = - options.getString(Option.Prospective) ?? Prospective.Side; - const prospective = prospectiveOption as Prospective; + const perspectiveOption = + options.getString(Option.Perspective) ?? Perspective.Side; + const perspective = perspectiveOption as Perspective; const unitsOption = options.getString(Option.Units) ?? Units.Metric; const units = unitsOption as Units; const partialContext = { firstCar, - prospective, + perspective, secondCar, units, }; diff --git a/src/commands/carsized/types.ts b/src/commands/carsized/types.ts index ec98565..f8d3d67 100644 --- a/src/commands/carsized/types.ts +++ b/src/commands/carsized/types.ts @@ -1,4 +1,4 @@ -import type { Prospective, Units } from "./constants"; +import type { Perspective, Units } from "./constants"; export type Car = { body: string; @@ -10,7 +10,9 @@ export type Car = { export type CompareCars = { firstCar: Car; - prospective: Prospective; + perspective: Perspective | undefined; + /** @deprecated Use perspective **/ + prospective?: Perspective; secondCar: Car; units: Units; }; diff --git a/src/commands/carsized/ui/front.button.ts b/src/commands/carsized/ui/front.button.ts index 0d1702b..8b08f28 100644 --- a/src/commands/carsized/ui/front.button.ts +++ b/src/commands/carsized/ui/front.button.ts @@ -1,11 +1,11 @@ import { registerComponent } from "../../../shared/discord"; -import { Prospective } from "../constants"; +import { Perspective } from "../constants"; import session, * as withContext from "../context"; import { UIID } from "../ui"; registerComponent(UIID.FrontButton, async (interaction, sessionId) => { const context = await session.read(sessionId); - context.prospective = Prospective.Front; + context.perspective = Perspective.Front; const newContext = await session.update(context, interaction); return withContext.compareCarsUi(newContext, interaction); diff --git a/src/commands/carsized/ui/index.ts b/src/commands/carsized/ui/index.ts index 07b074b..ed98882 100644 --- a/src/commands/carsized/ui/index.ts +++ b/src/commands/carsized/ui/index.ts @@ -14,7 +14,7 @@ import type { Context } from "../context"; import { Color } from "../../../shared/discord"; import Environment from "../../../shared/environment"; import * as carsized from "../carsized.manager"; -import { Prospective, Units } from "../constants"; +import { Perspective, Units } from "../constants"; export enum UIID { FrontButton = "26b9b11a-0134-46b0-9552-6a8d9353176e", @@ -62,32 +62,32 @@ const swapActionRow = (context: Context, isLoading: boolean) => ); // endregion -// region Prospective -const frontButton = ({ prospective, sessionId }: Context, isLoading: boolean) => +// region Perspective +const frontButton = ({ perspective, sessionId }: Context, isLoading: boolean) => new ButtonBuilder() .setCustomId(`${UIID.FrontButton}${sessionId}`) - .setDisabled(prospective === Prospective.Front || isLoading) + .setDisabled(perspective === Perspective.Front || isLoading) .setEmoji("🚘") .setLabel("| Front") .setStyle(ButtonStyle.Secondary); -const sideButton = ({ prospective, sessionId }: Context, isLoading: boolean) => +const sideButton = ({ perspective, sessionId }: Context, isLoading: boolean) => new ButtonBuilder() .setCustomId(`${UIID.SideButton}${sessionId}`) - .setDisabled(prospective === Prospective.Side || isLoading) + .setDisabled(perspective === Perspective.Side || isLoading) .setEmoji("🚗") .setLabel("| Side") .setStyle(ButtonStyle.Secondary); -const rearButton = ({ prospective, sessionId }: Context, isLoading: boolean) => +const rearButton = ({ perspective, sessionId }: Context, isLoading: boolean) => new ButtonBuilder() .setCustomId(`${UIID.RearButton}${sessionId}`) - .setDisabled(prospective === Prospective.Rear || isLoading) + .setDisabled(perspective === Perspective.Rear || isLoading) .setEmoji("🍑") .setLabel("| Rear") .setStyle(ButtonStyle.Secondary); -const prospectiveActionRow = (context: Context, isLoading: boolean) => +const perspectiveActionRow = (context: Context, isLoading: boolean) => new ActionRowBuilder().setComponents( frontButton(context, isLoading), sideButton(context, isLoading), @@ -121,7 +121,7 @@ const unitsActionRow = (context: Context, isLoading: boolean) => const compareCarsComponents = (context: Context, isLoading: boolean) => [ swapActionRow(context, isLoading), - prospectiveActionRow(context, isLoading), + perspectiveActionRow(context, isLoading), unitsActionRow(context, isLoading), ]; // endregion diff --git a/src/commands/carsized/ui/rear.button.ts b/src/commands/carsized/ui/rear.button.ts index 55b6efe..61144fe 100644 --- a/src/commands/carsized/ui/rear.button.ts +++ b/src/commands/carsized/ui/rear.button.ts @@ -1,11 +1,11 @@ import { registerComponent } from "../../../shared/discord"; -import { Prospective } from "../constants"; +import { Perspective } from "../constants"; import session, * as withContext from "../context"; import { UIID } from "../ui"; registerComponent(UIID.RearButton, async (interaction, sessionId) => { const context = await session.read(sessionId); - context.prospective = Prospective.Rear; + context.perspective = Perspective.Rear; const newContext = await session.update(context, interaction); return withContext.compareCarsUi(newContext, interaction); diff --git a/src/commands/carsized/ui/side.button.ts b/src/commands/carsized/ui/side.button.ts index 8cfaf94..8c241df 100644 --- a/src/commands/carsized/ui/side.button.ts +++ b/src/commands/carsized/ui/side.button.ts @@ -1,11 +1,11 @@ import { registerComponent } from "../../../shared/discord"; -import { Prospective } from "../constants"; +import { Perspective } from "../constants"; import session, * as withContext from "../context"; import { UIID } from "../ui"; registerComponent(UIID.SideButton, async (interaction, sessionId) => { const context = await session.read(sessionId); - context.prospective = Prospective.Side; + context.perspective = Perspective.Side; const newContext = await session.update(context, interaction); return withContext.compareCarsUi(newContext, interaction);