/ opencodehub / src / pages / index.astro
Swadhin Biswas Feat/federation (#82) · 8/19/2026
c0437ed
77 lines · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
import { getDatabase, schema } from "@/db";
import BaseLayout from "@/layouts/BaseLayout.astro";
import { asc, eq } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import UserProfileView from "@/components/profile/UserProfileView.astro";
import ServerDirectoryBar from "@/components/profile/ServerDirectoryBar.astro";

const db = getDatabase() as NodePgDatabase<typeof schema>;
const tab = Astro.url.searchParams.get("tab") || "overview";

// 1. Determine the primary profile to display on the landing page
let primaryUser: typeof schema.users.$inferSelect | null = null;

try {
  // If a user is logged in, show their profile (GitHub-style landing)
  const loggedInUser = Astro.locals.user;

  if (loggedInUser) {
    primaryUser = loggedInUser as any;
  } else {
    // Anonymous visitors: prefer the first admin, then first active user
    const adminUser = await db.query.users.findFirst({
      where: eq(schema.users.isAdmin, true),
      orderBy: [asc(schema.users.createdAt)],
    });

    if (adminUser) {
      primaryUser = adminUser;
    } else {
      // Find any first user
      const firstUser = await db.query.users.findFirst({
        where: eq(schema.users.isActive, true),
        orderBy: [asc(schema.users.createdAt)],
      });
      primaryUser = firstUser || null;
    }
  }
} catch (e) {
  console.error("Error resolving landing page user profile:", e);
}

// Fallback if the database has no users yet — generic placeholder so a fresh
// install still renders a coherent profile instead of showing a fabricated person.
if (!primaryUser) {
  primaryUser = {
    id: "placeholder-user",
    username: "welcome",
    displayName: "Welcome to OpenCodeHub",
    email: "",
    bio: "Your self-hosted Git platform. Create an account to get started.",
    company: "",
    location: "",
    website: "",
    avatarUrl: "",
    isAdmin: false,
    isActive: true,
    emailVerified: true,
    twoFactorEnabled: false,
    twoFactorSecret: null,
    passwordHash: null,
    lastLoginAt: null,
    createdAt: new Date(),
    updatedAt: new Date(),
    aiConfig: null,
  };
}
---

<BaseLayout title={`${primaryUser.displayName || primaryUser.username} - OpenCodeHub`}>
  <!-- Server-wide discovery bar for all users and organizations -->
  <ServerDirectoryBar currentUsername={primaryUser.username} />

  <!-- GitHub-Authentic User Profile View -->
  <UserProfileView user={primaryUser} tab={tab} isLanding={true} />
</BaseLayout>