Skip to content

Commit

Permalink
refactor to build api at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
mattk committed Aug 6, 2024
1 parent 41ce1e8 commit 012fe20
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
24 changes: 11 additions & 13 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
"use client";

import { useState, useEffect } from 'react';
import { RogueApp } from '../lib/types';
import styles from '../styles/Home.module.css';
import Callout from './components/Callout';
import { getRogueApps } from '../lib/getRogueApps';

export default function Home() {
const [rogueApps, setRogueApps] = useState<RogueApp[]>([]);
export async function getStaticProps() {
const rogueApps = getRogueApps();
return {
props: {
rogueApps,
},
};
}

export default function Home({ rogueApps }: { rogueApps: RogueApp[] }) {
const [searchTerm, setSearchTerm] = useState('');
const [expandedCard, setExpandedCard] = useState<number | null>(null);

useEffect(() => {
async function fetchRogueApps() {
const res = await fetch('/api/rogueapps');
const data: RogueApp[] = await res.json();
setRogueApps(data);
}
fetchRogueApps();
}, []);

const filteredApps = rogueApps.filter(app =>
app.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
app.description.toLowerCase().includes(searchTerm.toLowerCase()) ||
Expand Down
5 changes: 2 additions & 3 deletions lib/getRogueApps.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import fs from 'fs';
import path from 'path';
import { RogueApp } from './types';

export async function getRogueApps(): Promise<RogueApp[]> {
export function getRogueApps() {
const filePath = path.join(process.cwd(), 'public', 'rogueapps.json');
const jsonData = fs.readFileSync(filePath, 'utf8');
const rogueApps: RogueApp[] = JSON.parse(jsonData);
const rogueApps = JSON.parse(jsonData);
return rogueApps;
}

0 comments on commit 012fe20

Please sign in to comment.