Skip to content

Commit

Permalink
Refactor Conf Code (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
brookslybrand authored Jan 17, 2024
1 parent 0b94abb commit 01e746a
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 809 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ WORKDIR /remixapp

COPY --from=production-deps /remixapp/node_modules /remixapp/node_modules
COPY --from=build /remixapp/build /remixapp/build
COPY --from=build /remixapp/data /remixapp/data
COPY --from=build /remixapp/server.js /remixapp/server.js
COPY --from=build /remixapp/package.json /remixapp/package.json
COPY --from=build /remixapp/start.sh /remixapp/start.sh
Expand Down
58 changes: 19 additions & 39 deletions app/lib/conf.server.ts → app/lib/conf2022.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fsp from "fs/promises";
import path from "path";
import invariant from "tiny-invariant";
import { processMarkdown } from "~/lib/md.server";

Expand Down Expand Up @@ -27,6 +25,11 @@ import {
sluggify,
} from "./conf";

import speakersYamlFileContents from "../../data/conf/2022/speakers.yaml?raw";
import sponsorsYamlFileContents from "../../data/conf/2022/sponsors.yaml?raw";
import talksYamlFileContents from "../../data/conf/2022/talks.yaml?raw";
import scheduleYamlFileContents from "../../data/conf/2022/schedule.yaml?raw";

let cache = new LRUCache<
string,
Array<Speaker> | Array<Sponsor> | Array<Talk> | Array<ScheduleItem>
Expand All @@ -38,30 +41,13 @@ let cache = new LRUCache<
},
});

const DATA_DIR = path.join(process.cwd(), "data");
const CONF_ROOT_DIR = path.join(DATA_DIR, "conf");

function getConfPaths(year: ConfYear) {
let confDir = path.join(CONF_ROOT_DIR, String(year));
return {
speakersFile: path.join(confDir, "speakers.yaml"),
sponsorsFile: path.join(confDir, "sponsors.yaml"),
talksFile: path.join(confDir, "talks.yaml"),
scheduleFile: path.join(confDir, "schedule.yaml"),
};
}

type ConfYear = 2022 | 2023;

export async function getSpeakers(year: ConfYear) {
let cached = cache.get(`speakers-${year}`);
export async function getSpeakers() {
let cached = cache.get("speakers");
if (isSpeakerArray(cached)) {
return cached;
}

let { speakersFile: SPEAKERS_FILE } = getConfPaths(year);
let speakersFileContents = await fsp.readFile(SPEAKERS_FILE, "utf8");
let speakersRaw = yaml.parse(speakersFileContents);
let speakersRaw = yaml.parse(speakersYamlFileContents);
let speakers: Array<Speaker> = [];
for (let speakerRaw of speakersRaw) {
let { html: bioHTML } = await processMarkdown(speakerRaw.bio);
Expand All @@ -86,15 +72,13 @@ export async function getSpeakers(year: ConfYear) {
return speakers;
}

export async function getSponsors(year: ConfYear) {
let cached = cache.get(`sponsors-${year}`);
export async function getSponsors() {
let cached = cache.get("sponsors");
if (isSponsorArray(cached)) {
return cached;
}

let { sponsorsFile: SPONSORS_FILE } = getConfPaths(year);
let sponsorsFileContents = await fsp.readFile(SPONSORS_FILE, "utf8");
let sponsorsRaw = yaml.parse(sponsorsFileContents);
let sponsorsRaw = yaml.parse(sponsorsYamlFileContents);
let sponsors: Array<Sponsor> = [];
for (let sponsorRaw of sponsorsRaw) {
invariant(
Expand All @@ -110,15 +94,13 @@ export async function getSponsors(year: ConfYear) {
return sponsors;
}

export async function getTalks(year: ConfYear) {
let cached = cache.get(`talks-${year}`);
export async function getTalks() {
let cached = cache.get("talks");
if (isTalkArray(cached)) {
return cached;
}

let { talksFile: TALKS_FILE } = getConfPaths(year);
let talksFileContents = await fsp.readFile(TALKS_FILE, "utf8");
let talksRaw = yaml.parse(talksFileContents);
let talksRaw = yaml.parse(talksYamlFileContents);
let talks: Array<Talk> = [];
for (let talkRaw of talksRaw) {
invariant(
Expand All @@ -138,18 +120,16 @@ export async function getTalks(year: ConfYear) {
return talks;
}

export async function getSchedule(year: ConfYear) {
let cached = cache.get(`schedule-${year}`);
export async function getSchedule() {
let cached = cache.get("schedule");
if (isScheduleItemArray(cached)) {
return cached;
}

let { scheduleFile: SCHEDULE_FILE } = getConfPaths(year);
let allTalks = await getTalks(year);
let allSpeakers = await getSpeakers(year);
let allTalks = await getTalks();
let allSpeakers = await getSpeakers();

let schedulesFileContents = await fsp.readFile(SCHEDULE_FILE, "utf8");
let scheduleItemsRaw = yaml.parse(schedulesFileContents);
let scheduleItemsRaw = yaml.parse(scheduleYamlFileContents);
let scheduleItems: Array<ScheduleItem> = [];

for (let scheduleItemRaw of scheduleItemsRaw) {
Expand Down
30 changes: 29 additions & 1 deletion app/lib/conf2023.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import LRUCache from "lru-cache";
import { DateTime } from "luxon";
import yaml from "yaml";
import invariant from "tiny-invariant";
import {
sluggify,
validateSessionizeSessionData,
validateSessionizeSpeakerData,
} from "./conf2023";
import sponsorsYamlFileContents from "../../data/conf/2023/sponsors.yaml?raw";
import type {
Speaker,
SpeakerSession,
Expand All @@ -13,20 +16,23 @@ import type {
SessionizeSpeakerData,
SessionizeSessionData,
} from "./conf2023";
import type { Sponsor } from "./conf";
import { isSponsor, isSponsorArray } from "./conf";

const CONF_TIME_ZONE = "America/Denver";
const NO_CACHE =
process.env.NO_CACHE != null ? process.env.NO_CACHE === "true" : undefined;
const SPEAKERS_CACHE_KEY = "speakers";
const SESSIONS_CACHE_KEY = "sessions";
const SCHEDULES_CACHE_KEY = "schedules";
const SPONSORS_CACHE_KEY = "schedules";
const SESSIONIZE_ENDPOINT = "https://sessionize.com/api/v2/s8ds2hnu";
const SESSIONIZE_API_DETAILS_URL =
"https://sessionize.com/app/organizer/schedule/api/endpoint/9617/7818";

let cache = new LRUCache<
"speakers" | "sessions" | "schedules",
(Speaker | SpeakerSession | Schedule)[]
(Speaker | SpeakerSession | Schedule | Sponsor)[]
>({
max: 250,
maxSize: 1024 * 1024 * 12, // 12 mb
Expand Down Expand Up @@ -495,3 +501,25 @@ async function fetchNaiveStaleWhileRevalidate(
});
}
}

export async function getSponsors() {
let cached = cache.get(SPONSORS_CACHE_KEY);
if (isSponsorArray(cached)) {
return cached;
}

let sponsorsRaw = yaml.parse(sponsorsYamlFileContents);
let sponsors: Array<Sponsor> = [];
for (let sponsorRaw of sponsorsRaw) {
invariant(
isSponsor(sponsorRaw),
`Sponsor ${JSON.stringify(
sponsorRaw,
)} is not valid. Please check the sponsors file.`,
);
sponsors.push(sponsorRaw);
}
cache.set(SPONSORS_CACHE_KEY, sponsors);

return sponsors;
}
6 changes: 3 additions & 3 deletions app/routes/conf.2022._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { OutlineButtonLink, primaryButtonLinkClass } from "~/ui/buttons";
import "~/styles/index.css";
import { Fragment } from "react";
import type { Sponsor, Speaker } from "~/lib/conf";
import { getSpeakers, getSponsors } from "~/lib/conf.server";
import { getSpeakers, getSponsors } from "~/lib/conf2022.server";
import { Link } from "~/ui/link";
import { CACHE_CONTROL } from "~/lib/http.server";

Expand Down Expand Up @@ -35,7 +35,7 @@ export const meta: MetaFunction<typeof loader> = (args) => {
};

export const loader = async ({ request }: LoaderFunctionArgs) => {
const speakersOrdered = await getSpeakers(2022);
const speakersOrdered = await getSpeakers();
const speakersShuffled = speakersOrdered
// save a bit of data by not sending along the bio to the home page
.map(
Expand All @@ -47,7 +47,7 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
)
.sort(() => Math.random() - 0.5);

const allSponsors = await getSponsors(2022);
const allSponsors = await getSponsors();
const sponsors = {
premier: allSponsors.find((s) => s.level === "premier"),
gold: allSponsors
Expand Down
4 changes: 2 additions & 2 deletions app/routes/conf.2022._inner.schedule.may-25.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { json } from "@remix-run/node";
import { Link, useLoaderData } from "@remix-run/react";
import type { MetaFunction } from "@remix-run/react";
import { metaV1 } from "@remix-run/v1-meta";
import { getSchedule } from "~/lib/conf.server";
import { getSchedule } from "~/lib/conf2022.server";
import { CACHE_CONTROL } from "~/lib/http.server";
import { sluggify } from "~/lib/conf";

Expand All @@ -17,7 +17,7 @@ export const meta: MetaFunction = (args) => {
type LoaderData = { scheduleItems: Awaited<ReturnType<typeof getSchedule>> };

export const loader: LoaderFunction = async () => {
const scheduleItems = await getSchedule(2022);
const scheduleItems = await getSchedule();
return json<LoaderData>(
{ scheduleItems },
{ headers: { "Cache-Control": CACHE_CONTROL.DEFAULT } },
Expand Down
6 changes: 3 additions & 3 deletions app/routes/conf.2022._inner.speakers.$speakerSlug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import type { MetaFunction } from "@remix-run/react";
import { json } from "@remix-run/node";
import { metaV1 } from "@remix-run/v1-meta";
import { getSpeakers, getTalks } from "~/lib/conf.server";
import { getSpeakers, getTalks } from "~/lib/conf2022.server";
import "~/styles/conf-speaker.css";
import { sluggify } from "~/lib/conf";
import { CACHE_CONTROL } from "~/lib/http.server";
Expand All @@ -34,8 +34,8 @@ export const meta: MetaFunction<typeof loader> = (args) => {

export const loader = async ({ params }: LoaderFunctionArgs) => {
const [allTalks, allSpeakers] = await Promise.all([
getTalks(2022),
getSpeakers(2022),
getTalks(),
getSpeakers(),
]);
const speaker = allSpeakers.find((s) => s.slug === params.speakerSlug);
if (!speaker) throw new Response("Speaker not found", { status: 404 });
Expand Down
4 changes: 2 additions & 2 deletions app/routes/conf.2022._inner.workshops.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function Workshops() {
href="https://twitter.com/kentcdodds"
>
<img
src="/k.jpg"
src="/authors/profile-kent-c-dodds.png"
alt="Kent C. Dodds"
className="w-36 rounded-md"
/>
Expand All @@ -53,7 +53,7 @@ export default function Workshops() {
href="https://twitter.com/ryanflorence"
>
<img
src="/r.jpg"
src="/authors/profile-ryan-florence.png"
alt="Ryan Florence"
className="w-36 rounded-md"
/>
Expand Down
7 changes: 3 additions & 4 deletions app/routes/conf.2023._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import {
} from "~/ui/buttons";
import "~/styles/index.css";
import { Fragment } from "react";
import { getSponsors } from "~/lib/conf.server";
import type { Sponsor, SponsorLevel } from "~/lib/conf";
import { Link } from "~/ui/link";
import { CACHE_CONTROL } from "~/lib/http.server";
import { getSpeakers } from "~/lib/conf2023.server";
import { getSpeakers, getSponsors } from "~/lib/conf2023.server";
import type { Speaker } from "~/lib/conf2023";

export const meta: MetaFunction<typeof loader> = (args) => {
Expand Down Expand Up @@ -50,7 +49,7 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
console.error(err);
}

let allSponsors = await getSponsors(2023);
let allSponsors = await getSponsors();
let sponsors: Partial<Record<SponsorLevel, Sponsor[]>> = {};
for (let sponsor of allSponsors.sort(randomSort)) {
let level = sponsor.level;
Expand Down Expand Up @@ -149,7 +148,7 @@ function EarlySponsors() {
</p>
<div className="w-72 max-w-full sm:w-80 xl:w-96">
<GridCell>
<GridCellLink to=".">
<GridCellLink to="https://shopify.com/">
<div className="flex w-full p-12 md:p-14 lg:p-16 2xl:p-20">
<LogoShopify className="h-auto w-full" aria-hidden />
</div>
Expand Down
89 changes: 0 additions & 89 deletions data/conf/2023/schedule.yaml

This file was deleted.

Loading

0 comments on commit 01e746a

Please sign in to comment.