diff --git a/src/lib/mops.ts b/src/lib/mops.ts index bd2bfbb..ed87d99 100644 --- a/src/lib/mops.ts +++ b/src/lib/mops.ts @@ -1,11 +1,9 @@ import type { Story } from "../types/index" - export class StoryService { private endpoint = 'https://raw.githubusercontent.com/papua-opensource/mops/dataset/data.json'; private stories: Story[] = []; - private dailyStory: Story | null = null; - private lastUpdated: Date | null = null; + private lastUsedIndex: number = -1; constructor() { this.fetchStories(); @@ -17,6 +15,8 @@ export class StoryService { const response = await fetch(this.endpoint); const data = await response.json(); this.stories = data.stories; + // Sort stories by id + this.stories.sort((a, b) => a.id - b.id); } catch (error) { console.error('Failed to fetch stories:', error); } @@ -32,11 +32,17 @@ export class StoryService { return this.stories.find(story => story.slug === slug); } - // Get a random slug (used for the random story each night) - getRandomSlug(): string | undefined { + // Get the next story in sequence + getNextStory(): Story | undefined { if (this.stories.length === 0) return undefined; - const randomIndex = Math.floor(Math.random() * this.stories.length); - return this.stories[randomIndex].slug; + this.lastUsedIndex = (this.lastUsedIndex + 1) % this.stories.length; + return this.stories[this.lastUsedIndex]; + } + + // Get the current story's slug + getCurrentStorySlug(): string | undefined { + if (this.lastUsedIndex === -1 || this.stories.length === 0) return undefined; + return this.stories[this.lastUsedIndex].slug; } } \ No newline at end of file diff --git a/src/pages/index.astro b/src/pages/index.astro index 28805d8..c43084c 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -17,14 +17,15 @@ let storedSlug = Astro.cookies.get('dailyStorySlug')?.value; // Cek apakah ini hari baru atau tidak ada slug yang tersimpan if (storedDate !== today || !storedSlug) { - // Jika setelah jam 9 pagi, ambil cerita acak baru + // Jika setelah jam 9 pagi, ambil cerita berikutnya dalam urutan if (now.getHours() >= 9) { - storedSlug = storyService.getRandomSlug(); + dailyStory = storyService.getNextStory(); + storedSlug = dailyStory?.slug; Astro.cookies.set('dailyStoryDate', today, { path: '/' }); Astro.cookies.set('dailyStorySlug', storedSlug || '', { path: '/' }); } else if (!storedSlug) { - // Jika sebelum jam 9 pagi dan tidak ada slug tersimpan, ambil yang acak - storedSlug = storyService.getRandomSlug(); + // Jika sebelum jam 9 pagi dan tidak ada slug tersimpan, gunakan cerita saat ini + storedSlug = storyService.getCurrentStorySlug(); Astro.cookies.set('dailyStorySlug', storedSlug || '', { path: '/' }); } // Jika sebelum jam 9 pagi dan ada slug tersimpan, gunakan yang tersimpan diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 73780f5..13ca406 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -7,6 +7,7 @@ type Scene = { } export type Story = { + id: number; title: string; slug: string; description: string;