Skip to content

Commit eefe7f8

Browse files
committed
feat: switch cat image shuffle to client-side for random change on refresh
1 parent 761a6b2 commit eefe7f8

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pages/index.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import AdSense from '../components/AdSense';
77
import { catImages } from '../lib/mainImage';
88
import BookCard from '../components/BookCard';
99
import { getBooksMetaOnly, BookMeta } from '../lib/books';
10+
import { useEffect, useState } from 'react';
1011

1112
type Post = {
1213
id: string;
@@ -19,7 +20,6 @@ type Post = {
1920

2021
type HomeProps = {
2122
allPostsData: Post[];
22-
gradientsForPosts: string[];
2323
latestBook?: BookMeta | null;
2424
};
2525

@@ -35,17 +35,12 @@ function shuffle<T>(array: T[]): T[] {
3535
export const getStaticProps: GetStaticProps<HomeProps> = async () => {
3636
const allPostsData = getPostsMetaOnly();
3737

38-
const [latest, ...restAll] = allPostsData;
39-
const rest = restAll.filter((post) => post.id !== latest.id).slice(0, 3);
40-
const shuffledImages = shuffle(catImages).slice(0, rest.length);
41-
4238
const books = getBooksMetaOnly?.() ?? [];
4339
const latestBook = books.length > 0 ? books[0] : null;
4440

4541
return {
4642
props: {
4743
allPostsData,
48-
gradientsForPosts: shuffledImages,
4944
latestBook,
5045
},
5146
};
@@ -57,7 +52,6 @@ const byDesc = (a?: string, b?: string) =>
5752

5853
export default function Home({
5954
allPostsData,
60-
gradientsForPosts,
6155
latestBook,
6256
}: HomeProps) {
6357
if (!allPostsData.length) {
@@ -86,6 +80,15 @@ export default function Home({
8680
.filter((post) => post.id !== updatedPost.id)
8781
.slice(0, 3);
8882

83+
const [bgImages, setBgImages] = useState<string[]>(
84+
() => Array(rest.length).fill('')
85+
);
86+
87+
useEffect(() => {
88+
// rest 길이가 바뀌면 새로 셔플
89+
setBgImages(shuffle(catImages).slice(0, rest.length));
90+
}, [rest.length]);
91+
8992
return (
9093
<>
9194
<div className="min-h-screen">
@@ -214,7 +217,9 @@ export default function Home({
214217
<div
215218
className="flex flex-col justify-end p-6 h-48 sm:h-64 rounded-xl text-white shadow hover:shadow-xl transition"
216219
style={{
217-
backgroundImage: `url(${gradientsForPosts[idx]})`,
220+
backgroundImage: bgImages[idx]
221+
? `url(${bgImages[idx]})`
222+
: undefined,
218223
backgroundSize: 'cover',
219224
backgroundPosition: 'center',
220225
}}

0 commit comments

Comments
 (0)