Skip to content

Commit

Permalink
clean Home
Browse files Browse the repository at this point in the history
  • Loading branch information
mehotkhan committed Sep 2, 2024
1 parent 6fd1ba9 commit 7ea9187
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 49 deletions.
83 changes: 58 additions & 25 deletions src/front/home.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
import { Environment } from "../types";
import { KVModel } from "../utils/kv-storage";
import { convertToPersianNumbers } from "../utils/tools";

export const HomePageContent = async (env: Environment) => {
const statsModel = new KVModel<number>("stats", env.NekonymousKV);

// GitHub repository information
const githubOwner = "mehotkhan";
const githubRepo = "Nekonymous";
const githubUrl = `https://github.com/${githubOwner}/${githubRepo}`;

let commitHash = "N/A";
let commitDate = "N/A";
let commitMessage = "N/A";
let commitUrl = githubUrl;
let conversationsCount = "";
let usersCount = "";

const today = new Date().toISOString().split("T")[0];

conversationsCount = convertToPersianNumbers(
(await statsModel.get(`newConversation:${today}`)) || 0
);
usersCount = convertToPersianNumbers(
(await statsModel.get(`newUser:${today}`)) || 0
);

// Fetch the latest commit from GitHub
const commitInfo = await fetch(
`https://api.github.com/repos/${githubOwner}/${githubRepo}/commits/master`,
{
headers: {
"User-Agent": "Cloudflare Worker",
Accept: "application/vnd.github.v3+json",
},
}
);

if (commitInfo.ok) {
const commitData = await commitInfo.json();
commitHash = commitData.sha.substring(0, 7); // Shortened commit hash
commitDate = new Date(commitData.commit.author.date).toLocaleDateString();
commitMessage = commitData.commit.message.split("\n")[0]; // Extract first line of commit message
commitUrl = commitData.html_url; // URL to the specific commit on GitHub
}

return `
<div class="max-w-4xl mx-auto p-6">
<h1 class="text-3xl font-bold text-center mb-8">
Expand All @@ -10,19 +54,18 @@ export const HomePageContent = async (env: Environment) => {
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
<div class="bg-blue-100 p-6 rounded-lg shadow-lg text-center">
<h2 class="text-xl font-bold text-blue-700 mb-2">کاربران</h2>
<p id="usersCount" class="text-lg text-blue-600">
در حال بارگذاری...
<p class="text-lg text-blue-600">
${usersCount}
</p>
</div>
<div class="bg-green-100 p-6 rounded-lg shadow-lg text-center">
<h2 class="text-xl font-bold text-green-700 mb-2">تعداد مکالمات</h2>
<p id="conversationsCount" class="text-lg text-green-600">
در حال بارگذاری...
<p class="text-lg text-green-600">
${conversationsCount}
</p>
</div>
</div>
<p class="text-lg leading-relaxed mb-4">
نِکونیموس به شما این امکان را می‌دهد که به صورت ناشناس و امن با دیگر
کاربران چت کنید. این ربات با استفاده از تکنولوژی‌های پیشرفته و رمزنگاری
Expand All @@ -41,27 +84,17 @@ export const HomePageContent = async (env: Environment) => {
شروع به استفاده از ربات
</a>
</div>
<script>
async function fetchData() {
try {
const response = await fetch("/api/stats");
const data = await response.json();
document.getElementById("conversationsCount").textContent =
data.conversationsCount + " مکالمه";
document.getElementById("usersCount").textContent =
data.usersCount + " نفر";
} catch (error) {
console.error("Error fetching chart data:", error);
}
}
// Fetch initial data and set interval for periodic updates
fetchData();
// setInterval(fetchData, 5000); // Update the chart and stats every 10 seconds
</script>
<!-- Footer Section -->
<footer class="text-center mt-10 border-t pt-4">
<p class="text-sm text-gray-600">
<a href="${githubUrl}" class="underline">GitHub Repository</a> |
<a href="${commitUrl}" class="underline">Latest Commit: ${commitHash} on ${commitDate}</a><br />
Commit Message: ${commitMessage}
</p>
</footer>
</div>
`;
};
Expand Down
24 changes: 0 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { AboutPageContent } from "./front/about";
import { HomePageContent } from "./front/home";
import pageLayout from "./front/layout";
import { Environment } from "./types";
import { KVModel } from "./utils/kv-storage";
import { Router } from "./utils/router";
import { convertToPersianNumbers } from "./utils/tools";

// INBOX DURABLE OBJECTS
export { InboxDurableObject };
Expand Down Expand Up @@ -53,28 +51,6 @@ router.get(
}
);

/**
* API endpoint to get chart data in JSON format.
* This will be used to update the chart data on the home page every 5 seconds.
*/
router.get(
"/api/stats",
async (request: Request, env: Environment, ctx: ExecutionContext) => {
const statsModel = new KVModel<number>("stats", env.NekonymousKV);

const today = new Date().toISOString().split("T")[0];

const conversationsCount =
(await statsModel.get(`newConversation:${today}`)) || 0;
const usersCount = (await statsModel.get(`newUser:${today}`)) || 0;

return Response.json({
conversationsCount: convertToPersianNumbers(conversationsCount),
usersCount: convertToPersianNumbers(usersCount),
});
}
);

/**
* Define the bot webhook route.
* This handles incoming webhook requests from Telegram to the bot.
Expand Down

0 comments on commit 7ea9187

Please sign in to comment.