From 03a2ceafa1daf2020a98f4291e56826240576ac4 Mon Sep 17 00:00:00 2001 From: SimbaFs Date: Wed, 28 Feb 2024 21:03:15 +0800 Subject: [PATCH] get finished puzzle from opass server --- src/app/(game)/page.tsx | 12 ++++++++---- src/hooks/useFinished.ts | 19 +++++++++++++++++++ src/lib/getPlayerPuzzle.ts | 2 +- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 src/hooks/useFinished.ts diff --git a/src/app/(game)/page.tsx b/src/app/(game)/page.tsx index 5b4cc63..dc47147 100644 --- a/src/app/(game)/page.tsx +++ b/src/app/(game)/page.tsx @@ -3,11 +3,13 @@ import { useDetail } from "@/components/Detail"; import { Menu } from "@/components/Menu"; import { activities, type ActivityType } from "@/data/activity"; +import { useFinished } from "@/hooks/useFinished"; import { heading } from "@/varients/heading"; import { text } from "@/varients/text"; import Link from "next/link"; -import { useEffect, useState } from "react"; +import { useState } from "react"; import { twMerge } from "tailwind-merge"; +import { useLocalStorage } from "usehooks-ts"; function Activity({ activity, @@ -99,13 +101,15 @@ const menus = [ export default function Home() { const [active, setActive] = useState(0); - const finished = [[1, 2, 5], [1, 2], [4]][active]; // TODO: useFinished('session') const [filter, setFilter] = useState("all"); const [Detail, setOpen] = useDetail(); + const finishedList = useFinished(useLocalStorage("token", "")[0]); // This maybe need to be modified function isActivityFinished(activity: ActivityType, index: number) { - return finished.includes(index); + const finished = finishedList.includes(activity.name); + // console.log(activity.name, finished); + return finished; } const data = activities[active].map((item, index) => ({ @@ -129,7 +133,7 @@ export default function Home() {
如何完成任務?
-

{menus[active].description}

+
{menus[active].description}
diff --git a/src/hooks/useFinished.ts b/src/hooks/useFinished.ts new file mode 100644 index 0000000..d2c5b29 --- /dev/null +++ b/src/hooks/useFinished.ts @@ -0,0 +1,19 @@ +import { getPlayerPuzzle } from "@/lib/getPlayerPuzzle"; +import { useEffect, useState } from "react"; + +type finished = string[]; + +export function useFinished(token: string) { + const [finished, setFinished] = useState([]); + + useEffect(() => { + getPlayerPuzzle(token).then((data) => { + if (data == "Invalid token, please try again after checkin.") { + return; + } + setFinished(data.deliverers.map((d: any) => d.deliverer)); + }); + }, [token]); + + return finished; +} diff --git a/src/lib/getPlayerPuzzle.ts b/src/lib/getPlayerPuzzle.ts index 7506a1c..16a940e 100644 --- a/src/lib/getPlayerPuzzle.ts +++ b/src/lib/getPlayerPuzzle.ts @@ -21,7 +21,7 @@ export async function getPlayerPuzzle(playerToken: string) { if (data.message && data.message.startsWith("Invalid token")) { return data.message; } else { - return data.puzzles.length as number; + return data } }); }