callfi/app/page.tsx

160 lines
6.4 KiB
TypeScript

"use client";
import React, { useState, useEffect } from "react";
import { SunIcon, MoonIcon } from "@heroicons/react/solid";
import { PrivyProvider, usePrivy } from "@privy-io/react-auth";
import { FaTwitter, FaWallet } from "react-icons/fa";
import { CheckIcon } from "@heroicons/react/outline"; // For the check mark icon
// Helper functions to generate pseudo data
const generateRandomName = () => {
const names = ["Alice", "Bob", "Charlie", "David", "Eva", "Frank", "Grace", "Hannah", "Ivan", "Judy"];
return names[Math.floor(Math.random() * names.length)];
};
const generateRandomCoin = () => {
const coins = ["Bitcoin", "Ethereum", "Litecoin", "Ripple", "Cardano", "Polkadot", "Solana", "Chainlink"];
return coins[Math.floor(Math.random() * coins.length)];
};
const generateRandomPoint = () => {
return Math.floor(Math.random() * (100 - 10 + 1)) + 10;
};
function Home() {
const { login, user, ready, logout, linkTwitter, unlinkTwitter, linkWallet, unlinkWallet } = usePrivy();
const [darkMode, setDarkMode] = useState(true);
const [twitterConnected, setTwitterConnected] = useState(false); // State to track Twitter connection
const [walletConnected, setWalletConnected] = useState(false); // State to track Wallet connection
const [incrementNumber, setIncrementNumber] = useState(1);
const[leaderboardData, setLeaderboardData] = useState([]);
useEffect(() => {
if (darkMode) {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
}, [darkMode]);
useEffect(() => {
const intervalId = setInterval(async () => {
setIncrementNumber((prevNumber) => prevNumber + 1);
const response = await fetch('https://vps.playpoolstudios.com/callfi/get_leaderboard.php');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log('Leaderboard data:', data);
setLeaderboardData(data);
}, 5000);
return () => clearInterval(intervalId); // Clean up the interval on component unmount
}, []);
const toggleDarkMode = () => {
setDarkMode(!darkMode);
document.documentElement.classList.toggle("dark", !darkMode);
};
const postLoginAPI = async (usertag) => {
try {
const response = await fetch('https://vps.playpoolstudios.com/callfi/register_twitter_user.php?tag=' + usertag);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log('API call successful:', data);
setTwitterConnected(true); // Set Twitter connection state to true on successful API call
} catch (error) {
console.error('Error during API call:', error);
}
};
useEffect(() => {
if (ready && user) {
const username = user.twitter?.username ? `@${user.twitter.username}` : '@unknownUser';
if (username !== "@unknownUser") {
postLoginAPI(username);
}
// Assume we have a way to check if the wallet is connected
const walletStatus = user.wallet; // This is an assumption; replace it with actual wallet connection check
if (walletStatus) {
setWalletConnected(true);
}
}
}, [ready, user]);
return (
<main className={`flex flex-col min-h-screen items-center justify-between ${darkMode ? "bg-gray-900 text-white" : "bg-white text-black"}`}>
<header className="w-full flex justify-between items-center p-4 bg-gray-800 text-white">
<h1 className="text-xl font-bold">CallFi</h1>
<div className="flex items-center">
<button onClick={toggleDarkMode} className="bg-gray-700 hover:bg-gray-500 text-white font-bold py-2 px-4 rounded mr-2">
{darkMode ? <SunIcon className="h-5 w-5 text-white" /> : <MoonIcon className="h-5 w-5 text-white" />}
</button>
{ready && user ? (
<div className="flex items-center bg-white bg-opacity-20 rounded-full px-4 py-2">
<button className={`relative ${twitterConnected ? "bg-black" : "bg-gray-700"} hover:bg-gray-500 text-white font-bold py-2 px-4 rounded-full mr-2`} onClick={linkTwitter}>
<FaTwitter className="h-5 w-5 text-white" />
{twitterConnected && (
<CheckIcon className="absolute top-0 right-0 h-4 w-4 text-green-500" />
)}
</button>
<button className={`relative ${walletConnected ? "bg-black" : "bg-gray-700"} hover:bg-gray-500 text-white font-bold py-2 px-4 rounded-full mr-2` } onClick={linkWallet}>
<FaWallet className="h-5 w-5 text-white" />
{walletConnected && (
<CheckIcon className="absolute top-0 right-0 h-4 w-4 text-green-500" />
)}
</button>
<button onClick={logout} className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded">
Logout
</button>
</div>
):
!ready ? (
<p>Loading...</p>
) : <button onClick={login} className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Login
</button>
} </div>
</header>
<div className="flex-grow flex flex-col items-center justify-center w-full p-4">
<h2 className="text-2xl font-bold mb-4">Leaderboard </h2>
<div className="w-full max-w-4xl">
<table className={`min-w-full rounded-lg overflow-hidden shadow-lg ${darkMode ? "bg-gray-800" : "bg-white"}`}>
<thead className={`bg-gray-800 text-white`}>
<tr>
<th className="py-2 px-4">Name</th>
<th className="py-2 px-4">Points</th>
</tr>
</thead>
<tbody>
{leaderboardData.map((item, index) => (
<tr key={item["id"]} className={`${index % 2 === 0 ? (darkMode ? "bg-gray-700" : "bg-gray-100") : (darkMode ? "bg-gray-600" : "bg-gray-200")}`}>
<td className="py-2 px-4 text-center">{item["username"]}</td>
<td className="py-2 px-4 text-center">{parseFloat(item["points"]).toFixed(2)}x</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
<footer className="w-full p-4 bg-gray-800 text-white text-center">
<p>&copy; 2024 CallFi. All rights reserved.</p>
</footer>
</main>
);
}
export default function App() {
return (
<PrivyProvider appId={process.env.NEXT_PUBLIC_PRIVY_APP_ID || 'clxc7nmy906ifhis11rkovoto'}>
<Home />
</PrivyProvider>
);
}