Skip to content

Commit 72e00f2

Browse files
committed
Update components to use index.json
Index now delivers all data from the individual components to prevent constant network look ups. This is fine since we're building a static site, but in a real deployment we should probably use an API for this.
1 parent c1b4955 commit 72e00f2

File tree

5 files changed

+43
-41
lines changed

5 files changed

+43
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ yarn-error.log*
3939
# typescript
4040
*.tsbuildinfo
4141
next-env.d.ts
42+
public/data/index.json

app/page.tsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
import fs from 'fs';
1+
import fs from 'fs';
22
import path from 'path';
33
import SkyboxGrid from './ui/skyboxgrid';
4-
import Header from './ui/header';
5-
6-
function getSkyboxSlugs(): string[] {
7-
const skyboxesDir = path.join(process.cwd(), 'public', 'skyboxes');
84

9-
// read everything in /public/skyboxes and keep only directories
10-
return fs
11-
.readdirSync(skyboxesDir)
12-
.filter((name) =>
13-
fs.statSync(path.join(skyboxesDir, name)).isDirectory()
14-
);
15-
}
5+
const allPath = path.join(process.cwd(), 'public', 'data', 'index.json');
6+
const allData = JSON.parse(fs.readFileSync(allPath, 'utf8')) as Record<string, any>;
7+
const slugs = Object.keys(allData);
168

179
export default function Home() {
18-
const slugs = getSkyboxSlugs();
19-
20-
return (
21-
<>
22-
<SkyboxGrid slugs={slugs} />
23-
</>
24-
);
25-
}
10+
return <SkyboxGrid slugs={slugs} meta={allData} />;
11+
}

app/ui/skyboxcard.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import { useState } from 'react';
66

77
type Props = { slug: string };
88

9-
export default function SkyboxCard({ slug }: Props) {
9+
export default function SkyboxCard({
10+
slug,
11+
meta,
12+
}: {
13+
slug: string;
14+
meta: any;
15+
}) {
1016
const [open, setOpen] = useState(false);
1117
const imgBase = `/Source_Skyboxes_NextJS/skyboxes/${slug}/images`;
1218

app/ui/skyboxgrid.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,33 @@ import { useMemo, useState } from 'react';
44
import SkyboxCard from './skyboxcard';
55
import Header from './header';
66

7-
export default function SkyboxGrid({ slugs }: { slugs: string[] }) {
8-
const [options, setOptions] = useState<{ sort: string; query: string }>({
7+
export default function SkyboxGrid({
8+
slugs,
9+
meta,
10+
}: {
11+
slugs: string[];
12+
meta: Record<string, any>;
13+
}) {
14+
const [filter, setFilter] = useState<{ sort: string; query: string }>({
915
sort: 'alpha',
1016
query: '',
1117
});
1218

1319
const visible = useMemo(() => {
1420
let list = [...slugs];
15-
16-
if (options.query) {
17-
const q = options.query.toLowerCase();
18-
list = list.filter((s) => s.toLowerCase().includes(q));
21+
if (filter.query) {
22+
const q = filter.query.toLowerCase();
23+
list = list.filter((s) => s.includes(q));
1924
}
25+
if (filter.sort === 'date') list.sort().reverse();
26+
else list.sort();
2027

21-
if (options.sort === 'date') {
22-
list.sort().reverse(); // slug folders are ISO dates? adjust as needed
23-
} else {
24-
list.sort();
25-
}
2628
return list;
27-
}, [slugs, options]);
29+
}, [slugs, filter]);
2830

2931
return (
3032
<>
31-
<Header onChange={setOptions} />
33+
<Header onChange={setFilter} />
3234

3335
<section
3436
className="
@@ -41,7 +43,7 @@ export default function SkyboxGrid({ slugs }: { slugs: string[] }) {
4143
"
4244
>
4345
{visible.map((slug) => (
44-
<SkyboxCard key={slug} slug={slug} />
46+
<SkyboxCard key={slug} slug={slug} meta={meta[slug]} />
4547
))}
4648
</section>
4749
</>

scripts/generate-index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
const fs = require('fs');
22
const path = require('path');
33

4-
const dataDir = path.join(__dirname, '..', 'public', 'data');
5-
const files = fs.readdirSync(dataDir).filter(file => file.endsWith('.json'));
6-
const slugs = files.map(fileName => path.basename(fileName, '.json'));
4+
const dataDir = path.join(__dirname, '..', 'public', 'data');
5+
const files = fs
6+
.readdirSync(dataDir)
7+
.filter((f) => f.endsWith('.json') && f !== 'index.json');
78

8-
const outputPath = path.join(dataDir, 'index.json');
9-
fs.writeFileSync(outputPath, JSON.stringify(slugs, null, 2));
9+
const all = {};
1010

11-
console.log(`Generated index.json with ${slugs.length} entries at ${outputPath}`);
11+
files.forEach((fname) => {
12+
const slug = fname.replace(/\.json$/, '');
13+
const data = JSON.parse(fs.readFileSync(path.join(dataDir, fname), 'utf8'));
14+
all[slug] = data;
15+
});
16+
17+
const out = path.join(dataDir, 'index.json');
18+
fs.writeFileSync(out, JSON.stringify(all, null, 2));

0 commit comments

Comments
 (0)