Skip to content

Commit

Permalink
feat: more dynamic pages
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Jan 19, 2024
1 parent a1d1740 commit 283a3c3
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 60 deletions.
5 changes: 5 additions & 0 deletions src/content/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Getting Started
description: Getting Started with green energy!
category: green
---
15 changes: 15 additions & 0 deletions src/content/guarantees-in-life.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Taxes
description: "There are only two guarantees in life: death and taxes."
category: monies
---

## Tax Rebates

Looking for information about tax rebates and incentives for green
energy?

Please keep in mind that, like death, taxes are a certainty, and thus
tax evasion is illegal. If you wish to avoid taxes, please consult a
registered accountant rather than willy-nilly skipping them based on
our advice.
5 changes: 5 additions & 0 deletions src/content/pricing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Pricing
description: Pricing for green energy
category: monies
---
5 changes: 5 additions & 0 deletions src/content/programs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Programs
description: Green energy programs
category: green
---
15 changes: 13 additions & 2 deletions src/content/solar.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
---
title: Solar Energy Solutions
description: Solar Energy is an undertapped energy resource.
category: solar
---

<!-- The website should provide basic information, cost, tax rebate information, and clean/green energy practices. -->

## What is it?

Solar panels are useful. They collect energy from the sun, and can be placed on roofs discreetly.

## Cost

## Tax Rebates

## Best Practices

## Buy Now

You can use our [online calculator](/calculator/) to calculate costs and buy _your_ solar energy solution today!

## Sources
2 changes: 0 additions & 2 deletions src/fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as $_app from "./routes/_app.tsx";
import * as $_layout from "./routes/_layout.tsx";
import * as $about from "./routes/about.tsx";
import * as $index from "./routes/index.tsx";
import * as $monies_guarantees_in_life from "./routes/monies/guarantees-in-life.tsx";
import * as $solutions_slug_ from "./routes/solutions/[slug].tsx";
import * as $HeaderMenu from "./islands/HeaderMenu.tsx";
import { type Manifest } from "$fresh/server.ts";
Expand All @@ -21,7 +20,6 @@ const manifest = {
"./routes/_layout.tsx": $_layout,
"./routes/about.tsx": $about,
"./routes/index.tsx": $index,
"./routes/monies/guarantees-in-life.tsx": $monies_guarantees_in_life,
"./routes/solutions/[slug].tsx": $solutions_slug_,
},
islands: {
Expand Down
27 changes: 0 additions & 27 deletions src/routes/monies/guarantees-in-life.tsx

This file was deleted.

90 changes: 65 additions & 25 deletions src/utils/site-organization.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
// TODO(lishaduck): generate from `solutions`.
export const menus = [
{
title: "Going Green?",
url: "/green/",
items: [
{ name: "Getting Started", href: "getting-started/" },
{ name: "Programs", href: "programs/" },
],
},
{
title: "Monies",
url: "/monies/",
items: [
{ name: "Taxes", href: "guarantees-in-life/" },
{ name: "Pricing", href: "pricing/" },
],
},
{
title: "About",
url: "/about/",
},
] as const satisfies Menu[];
import { solutions } from "./solutions.ts";
import { isKey } from "./type-helpers.ts";

export interface Menu {
readonly title: string;
readonly url: `${string}/`;
readonly url: `/${string}/`;
readonly items?: readonly MenuItem[];
}

Expand All @@ -34,5 +13,66 @@ export interface MenuItem {
}

export interface MenuWithItems extends Menu {
readonly items: readonly [MenuItem, ...(readonly MenuItem[])];
readonly items: readonly [MenuItem, ...MenuItem[]];
}

const extraMenus = [
{
title: "About",
url: "/about/",
},
] as const satisfies Menu[];

/**
* Convert the `solutions` into to `Menu`s based on the .category.
* Should also append the `extraMenus` to the end to add the about page and such.
*
* @returns The generated menus.
*/

function generateMenus(): Menu[] {
const categories = new Map<string, Menu>();

for (const solution of solutions) {
// If the category doesn't exist yet, create it
if (!categories.has(solution.data.category)) {
categories.set(solution.data.category, {
title: isKey(categoryMap, solution.data.category)
? categoryMap[solution.data.category]
: solution.data.category,
url: "/solutions/",
items: [],
});
}

// Add the solution to the category's items
const category = categories.get(solution.data.category);
if (category !== undefined) {
categories.set(solution.data.category, {
...category,
items: [
...(category.items ?? []),
{
name: solution.data.title,
href: `${solution.slug}/`,
},
],
});
}
}

for (const menu of extraMenus) {
categories.set(menu.title, menu);
}

return Array.from(categories.values());
}

const categoryMap = {
green: "Going Green?",
monies: "Monies",
about: "About",
solar: "Solar",
} as const;

export const menus = generateMenus();
15 changes: 11 additions & 4 deletions src/utils/solutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ const solutionDataSchema = z
.object({
title: z.string(),
description: z.string(),
category: z.string(),
})
.passthrough()
.readonly();

const solutionPageSchema = z
.object({
slug: z.string(),
slug: z.string(), // The slug of the solution without a trailing slash.
markdown: z.string(),
data: solutionDataSchema,
})
Expand All @@ -38,6 +39,8 @@ const solutionPagesSchema = solutionPagesNullableSchema.transform(
);

const dir = "src/content";
const categorySort = ["green", "monies", "solar"];

export const solutions = await getSolutions();

/** Get all solutions. */
Expand All @@ -48,11 +51,15 @@ export async function getSolutions(): Promise<SolutionPages> {
const slug = file.name.replace(".md", "");
promises.push(getSolution(slug));
}
const solutions = await Promise.all(promises);
const unparsedSolutions = await Promise.all(promises);
const solutions = solutionPagesSchema.parse(unparsedSolutions);

return solutionPagesSchema.parse(solutions);
return solutions.toSorted(
(a, b) =>
categorySort.indexOf(a.data.category) -
categorySort.indexOf(b.data.category),
);
}

/** Get a solution. */
export async function getSolution(
slug: string,
Expand Down
7 changes: 7 additions & 0 deletions src/utils/type-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ import type { Menu, MenuWithItems } from "./site-organization.ts";
export function hasItems(menu: Menu): menu is MenuWithItems {
return (menu.items?.length ?? 0) > 0;
}

export function isKey<T extends object>(
obj: T,
key: PropertyKey,
): key is keyof T {
return Object.hasOwn(obj, key);
}

0 comments on commit 283a3c3

Please sign in to comment.