"use client"; import React, { useEffect, useState } from "react"; import QueryInput from "./queryinput"; import { homeSearchParamsCache } from "@/lib/searchParams"; import { getSessionAuthToken, getSpaces } from "@/app/actions/fetchers"; import { useRouter } from "next/navigation"; import { createChatThread, linkTelegramToUser } from "@/app/actions/doers"; import { toast } from "sonner"; import { useSession } from "next-auth/react"; import { motion } from "framer-motion"; import BackgroundPlus from "@/app/(landing)/GridPatterns/PlusGrid"; import { variants } from "./homeVariants"; const slap = { initial: { opacity: 0, scale: 1.1, }, whileInView: { opacity: 1, scale: 1 }, transition: { duration: 0.5, ease: "easeInOut", }, viewport: { once: true }, }; function Page({ searchParams, }: { searchParams: Record; }) { // TODO: use this to show a welcome page/modal // const { firstTime } = homeSearchParamsCache.parse(searchParams); const [telegramUser, setTelegramUser] = useState( searchParams.telegramUser as string, ); const { push } = useRouter(); const [spaces, setSpaces] = useState<{ id: number; name: string }[]>([]); const [showVariant, setShowVariant] = useState(0); useEffect(() => { if (telegramUser) { const linkTelegram = async () => { const response = await linkTelegramToUser(telegramUser); if (response.success) { toast.success("Your telegram has been linked successfully."); } else { toast.error("Failed to link telegram. Please try again."); } }; linkTelegram(); } getSpaces().then((res) => { if (res.success && res.data) { setSpaces(res.data); return; } // TODO: HANDLE ERROR }); setShowVariant(Math.floor(Math.random() * variants.length)); getSessionAuthToken().then((token) => { if (typeof window === "undefined") return; window.postMessage({ token: token.data }, "*"); }); }, []); return (
{/* all content goes here */} {/*
hi {firstTime ? 'first time' : ''}
*/} {variants[showVariant]!.map((v, i) => { return ( {v.content} ); })}
{ if (q.length === 0) { toast.error("Query is required"); return; } const threadid = await createChatThread(q); if (!threadid.success || !threadid.data) { toast.error("Failed to create chat thread"); return; } push( `/chat/${threadid.data}?spaces=${JSON.stringify(spaces)}&q=${q}`, ); }} initialSpaces={spaces} setInitialSpaces={setSpaces} />
); } export default Page;