summaryrefslogtreecommitdiff
path: root/src/pages/tones.tsx
blob: 066f3cbb9e87aeb3c930752eb9c5305a7efa2237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import "@/styles/globals.css";
import { Suspense } from "react";
import { fetchWordsByToneAndSyllables } from "@/actions/tones";
import ToneSelectorClient from "@/components/tones/ToneSelectorClient";
import { Skeleton } from "@/components/ui/skeleton"; // For Suspense fallback
import { thaiTones } from "@/lib/types/phonetics";
import { randomFromArray } from "@/lib/utils";

export const getConfig = async () => {
  return {
    render: "static", // Or 'dynamic' if you prefer SSR for every request
  };
};
async function randomTones(tries = 0) {
  const syllables = Math.floor(Math.random() * 5);
  const toneStrings = Object.values(thaiTones);
  const tones = Array.from(Array(syllables)).map((_) =>
    randomFromArray(toneStrings),
  );
  console.log({ tones, toneStrings });
  const initialWords = await fetchWordsByToneAndSyllables(tones);
  if (!initialWords || initialWords.length === 0) return randomTones(tries + 1);
  else return { initialWords, tones };
}

// Function to fetch the initial word on the server
async function InitialWordLoader() {
  // Fetch a random 1-syllable Thai word with any tone initially
  const { initialWords, tones } = await randomTones();
  return <ToneSelectorClient initialData={initialWords} initialTones={tones} />;
}

// Loading fallback component
function TonePageSkeleton() {
  return (
    <div className="container mx-auto p-4 max-w-2xl">
      <div className="mb-6 p-6 border rounded-lg shadow">
        <Skeleton className="h-8 w-1/2 mb-4" />
        <Skeleton className="h-6 w-3/4 mb-6" />

        <div className="space-y-6">
          <div>
            <Skeleton className="h-6 w-1/4 mb-2" />
            <Skeleton className="h-10 w-full md:w-1/2" />
          </div>
          <div>
            <Skeleton className="h-6 w-1/4 mb-2" />
            <Skeleton className="h-10 w-full md:w-1/2" />
          </div>
        </div>
        <Skeleton className="h-10 w-full md:w-1/4 mt-6" />
      </div>
      <div className="p-6 border rounded-lg shadow">
        <Skeleton className="h-8 w-1/3 mx-auto mb-4" />
        <Skeleton className="h-24 w-3/4 mx-auto mb-4" />
        <Skeleton className="h-6 w-1/2 mx-auto" />
      </div>
    </div>
  );
}

export default function TonesPage() {
  return (
    <div className="py-8">
      <Suspense fallback={<TonePageSkeleton />}>
        <InitialWordLoader />
      </Suspense>
    </div>
  );
}

export const metadata = {
  title: "Thai Tone Explorer",
  description: "Explore Thai words by syllable count and tones.",
};