From 8531857c55988f4c61c8a8da26fbde6e1998a667 Mon Sep 17 00:00:00 2001 From: Nooc Date: Sat, 21 Sep 2024 12:07:09 +0800 Subject: [PATCH] Add CassetteOne advertisement --- app/[lang]/posts/[...slug]/Advertising.tsx | 22 ++++++++++++++++++++++ app/[lang]/posts/[...slug]/page.tsx | 18 ++++-------------- dictionaries/en.ts | 20 ++++++++++++++------ dictionaries/index.ts | 2 ++ dictionaries/zh.ts | 20 ++++++++++++++------ lib/array.ts | 4 ++++ 6 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 app/[lang]/posts/[...slug]/Advertising.tsx diff --git a/app/[lang]/posts/[...slug]/Advertising.tsx b/app/[lang]/posts/[...slug]/Advertising.tsx new file mode 100644 index 0000000..ecfe1fb --- /dev/null +++ b/app/[lang]/posts/[...slug]/Advertising.tsx @@ -0,0 +1,22 @@ +'use client'; + +import { Dictionary, getDictionary, Language } from "@/dictionaries"; +import { getRandomElement } from "@/lib/array"; +import Image from 'next/image'; +import { useMemo } from "react"; + +export function PostAdvertising({advertisements}: {advertisements: Dictionary["postAdvertisements"]}) { + const advertisement = useMemo(() => getRandomElement(advertisements), [advertisements]); + + return + {advertisement.title} +
+
{advertisement.title}
+
{advertisement.description}
+
+
+} \ No newline at end of file diff --git a/app/[lang]/posts/[...slug]/page.tsx b/app/[lang]/posts/[...slug]/page.tsx index 83bd39e..2a1f2b3 100644 --- a/app/[lang]/posts/[...slug]/page.tsx +++ b/app/[lang]/posts/[...slug]/page.tsx @@ -3,11 +3,11 @@ import { CalendarDaysIcon, LanguageIcon } from "@heroicons/react/24/solid"; import { displayDate } from "@/lib/date"; import { notFound } from "next/navigation"; import Link from "next/link"; -import { getDictionary, languageLabels } from "@/dictionaries"; +import { getDictionary, Language, languageLabels } from "@/dictionaries"; import { SiX } from "@icons-pack/react-simple-icons"; import { Metadata } from "next"; -import Image from "next/image"; import classNames from "classnames"; +import { PostAdvertising } from "./Advertising"; export const runtime = "edge"; @@ -53,7 +53,7 @@ export default async function PostPage({ params, }: { params: { - lang: string; + lang: Language; slug: string[]; }; }) { @@ -127,17 +127,7 @@ export default async function PostPage({ - - {dictionary.postAdvertising.title} -
-
{dictionary.postAdvertising.title}
-
{dictionary.postAdvertising.description}
-
-
+
diff --git a/dictionaries/en.ts b/dictionaries/en.ts index 2f214f9..8cbfe34 100644 --- a/dictionaries/en.ts +++ b/dictionaries/en.ts @@ -217,12 +217,20 @@ A fan of Serverless. icon: EnvelopeIcon, }, ], - postAdvertising: { - title: "Support me", - description: "Spark Memos is a concise note-taking app for capturing ideas, inspirations, or for later reading and book notes.", - icon: SparkMemosLogo, - link: "https://sparkmemos.com", - }, + postAdvertisements:[ + { + title: "Support me", + description: "Spark Memos is a concise note-taking app for capturing ideas, inspirations, or for later reading and book notes.", + icon: SparkMemosLogo, + link: "https://sparkmemos.com", + }, + { + title: "Support me", + description: "CassetteOne is a retro design cassette white noise and music player for iOS.", + icon: CassetteOneLogo, + link: "https://cassette.one", + }, + ], }; export default dictionary; diff --git a/dictionaries/index.ts b/dictionaries/index.ts index 742e028..1ac0970 100644 --- a/dictionaries/index.ts +++ b/dictionaries/index.ts @@ -10,6 +10,8 @@ export const languageLabels = { zh: "中文", }; +export type Dictionary = Awaited>; + export type Language = keyof typeof languageLabels; export const dictionaryKeys = Object.keys(dictionaries); diff --git a/dictionaries/zh.ts b/dictionaries/zh.ts index 57f35c5..5424c8e 100644 --- a/dictionaries/zh.ts +++ b/dictionaries/zh.ts @@ -212,12 +212,20 @@ Serverless 爱好者。 icon: EnvelopeIcon, }, ], - postAdvertising: { - title: "支持我", - description: "「星火记」是一个简洁的短笔记应用,可以用来记录灵感、想法,或者是用于稍后阅读、读书笔记等。", - icon: SparkMemosLogo, - link: "https://sparkmemos.com", - }, + postAdvertisements: [ + { + title: "支持我", + description: "「星火记」是一个简洁的短笔记应用,可以用来记录灵感、想法,或者是用于稍后阅读、读书笔记等。", + icon: SparkMemosLogo, + link: "https://sparkmemos.com", + }, + { + title: "支持我", + description: "「CassetteOne」是一个模拟磁带机设计的复古风格白噪音和音乐播放器。", + icon: CassetteOneLogo, + link: "https://cassette.one", + } + ], }; export default dictionary; diff --git a/lib/array.ts b/lib/array.ts index 66a04d7..783750d 100644 --- a/lib/array.ts +++ b/lib/array.ts @@ -10,3 +10,7 @@ export function shuffleArray(array: T[]): T[] { } return arrayToShuffle; } + +export function getRandomElement(array: T[]): T { + return array[Math.floor(Math.random() * array.length)]; +} \ No newline at end of file