_index.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { useState, useEffect } from "react";
  2. import type { MetaFunction, LoaderFunction } from "@remix-run/node";
  3. import { json } from "@remix-run/node";
  4. import { useLoaderData } from "@remix-run/react";
  5. import { Button, Card, Typography, Space } from "antd";
  6. import { ClientOnly } from "remix-utils/client-only";
  7. import db from "~/db.server";
  8. const { Title, Paragraph } = Typography;
  9. export const meta: MetaFunction = () => {
  10. return [
  11. { title: "Remix 管理系统" },
  12. { name: "description", content: "欢迎来到 Remix 管理系统!" },
  13. ];
  14. };
  15. export const loader: LoaderFunction = async () => {
  16. const userCount = await db("users").count("* as count").first();
  17. return json({ userCount: userCount?.count || 0 });
  18. };
  19. function WelcomeDemo() {
  20. const { userCount } = useLoaderData<typeof loader>();
  21. const [currentTime, setCurrentTime] = useState(new Date());
  22. useEffect(() => {
  23. const timer = setInterval(() => setCurrentTime(new Date()), 1000);
  24. return () => clearInterval(timer);
  25. }, []);
  26. return (
  27. <Card className="w-full max-w-md shadow-lg">
  28. <Title level={2} className="text-center mb-6">欢迎来到 Remix 管理系统</Title>
  29. <Paragraph className="text-center mb-4">
  30. 当前时间:{currentTime.toLocaleString()}
  31. </Paragraph>
  32. <Paragraph className="text-center mb-6">
  33. 系统中共有 {userCount} 个用户
  34. </Paragraph>
  35. <Space direction="vertical" size="middle" className="w-full">
  36. <Button type="primary" block>
  37. 登录系统
  38. </Button>
  39. <Button block>
  40. 查看用户列表
  41. </Button>
  42. </Space>
  43. </Card>
  44. );
  45. }
  46. export default function Index() {
  47. return (
  48. <div className="min-h-screen bg-gray-100 flex items-center justify-center">
  49. <ClientOnly fallback={<p>加载中...</p>}>
  50. {() => <WelcomeDemo />}
  51. </ClientOnly>
  52. </div>
  53. );
  54. }