From 8a7742af1beb52259b5b3011f7afe6903c541d10 Mon Sep 17 00:00:00 2001 From: kovakmedya Date: Thu, 30 Apr 2026 03:33:53 +0300 Subject: [PATCH] feat(dashboard): personalized header + reuse dashboard-2 components MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - /dashboard now a server component: * fetches active user + active tenant settings via getActiveContext() * redirects to /onboarding if user has no tenant yet * header shows companyName + 'Hoş geldiniz, {firstName}' + Turkish description - Body reuses dashboard-2 components (Metrics/Sales/Revenue/Transactions/ TopProducts/CustomerInsights/QuickActions). Mock data for now; will be swapped for live Appwrite queries as modules ship. - New lib/appwrite/active-context.ts: getActiveContext() returns { user, tenantId, settings } for any server component / action. - Made schema.ts SDK-agnostic by replacing Models.Document import with a local SystemRow type ({ $id, $createdAt, $updatedAt, $permissions, ... }). Avoids type clashes between appwrite (browser) and node-appwrite (server). --- src/app/(dashboard)/dashboard/page.tsx | 70 ++++++++++++++++---------- src/lib/appwrite/active-context.ts | 44 ++++++++++++++++ src/lib/appwrite/schema.ts | 14 ++++-- 3 files changed, 98 insertions(+), 30 deletions(-) create mode 100644 src/lib/appwrite/active-context.ts diff --git a/src/app/(dashboard)/dashboard/page.tsx b/src/app/(dashboard)/dashboard/page.tsx index 807b867..62f5a5d 100644 --- a/src/app/(dashboard)/dashboard/page.tsx +++ b/src/app/(dashboard)/dashboard/page.tsx @@ -1,35 +1,51 @@ -import { ChartAreaInteractive } from "./components/chart-area-interactive" -import { DataTable } from "./components/data-table" -import { SectionCards } from "./components/section-cards" +import { redirect } from "next/navigation"; -import data from "./data/data.json" -import pastPerformanceData from "./data/past-performance-data.json" -import keyPersonnelData from "./data/key-personnel-data.json" -import focusDocumentsData from "./data/focus-documents-data.json" +import { getActiveContext } from "@/lib/appwrite/active-context"; +import { CustomerInsights } from "../dashboard-2/components/customer-insights"; +import { MetricsOverview } from "../dashboard-2/components/metrics-overview"; +import { QuickActions } from "../dashboard-2/components/quick-actions"; +import { RecentTransactions } from "../dashboard-2/components/recent-transactions"; +import { RevenueBreakdown } from "../dashboard-2/components/revenue-breakdown"; +import { SalesChart } from "../dashboard-2/components/sales-chart"; +import { TopProducts } from "../dashboard-2/components/top-products"; + +export default async function DashboardPage() { + const ctx = await getActiveContext(); + if (!ctx) redirect("/onboarding"); + + const firstName = ctx.user.name?.split(" ")[0] ?? ""; + const companyName = ctx.settings?.companyName ?? "Çalışma alanı"; -export default function Page() { return ( - <> - {/* Page Title and Description */} -
-
-

Dashboard

-

Welcome to your admin dashboard

+
+
+
+

{companyName}

+

+ {firstName ? `Hoş geldiniz, ${firstName}` : "Genel bakış"} +

+

+ İşletmenizin temel metriklerini ve son hareketleri buradan takip edin. +

+
-
- - +
+ + +
+ + +
+ +
+ + +
+ +
-
- -
- - ) +
+ ); } diff --git a/src/lib/appwrite/active-context.ts b/src/lib/appwrite/active-context.ts new file mode 100644 index 0000000..58a6e20 --- /dev/null +++ b/src/lib/appwrite/active-context.ts @@ -0,0 +1,44 @@ +import "server-only"; + +import { Query } from "node-appwrite"; + +import { createAdminClient, getCurrentUser } from "./server"; +import { DATABASE_ID, TABLES, type TenantSettings } from "./schema"; +import { getActiveTenantId, getUserTeams } from "./tenant"; + +export type ActiveContext = { + user: { id: string; name: string; email: string }; + tenantId: string; + settings: TenantSettings | null; +}; + +export async function getActiveContext(): Promise { + const user = await getCurrentUser(); + if (!user) return null; + + let tenantId = await getActiveTenantId(); + if (!tenantId) { + const teams = await getUserTeams(); + tenantId = teams?.teams[0]?.$id ?? null; + } + if (!tenantId) return null; + + let settings: TenantSettings | null = null; + try { + const { tablesDB } = createAdminClient(); + const result = await tablesDB.listRows({ + databaseId: DATABASE_ID, + tableId: TABLES.tenantSettings, + queries: [Query.equal("tenantId", tenantId), Query.limit(1)], + }); + settings = (result.rows[0] as unknown as TenantSettings) ?? null; + } catch { + settings = null; + } + + return { + user: { id: user.$id, name: user.name, email: user.email }, + tenantId, + settings, + }; +} diff --git a/src/lib/appwrite/schema.ts b/src/lib/appwrite/schema.ts index f1280f4..16a0937 100644 --- a/src/lib/appwrite/schema.ts +++ b/src/lib/appwrite/schema.ts @@ -1,5 +1,3 @@ -import type { Models } from "appwrite"; - export const DATABASE_ID = "isletmem"; export const TABLES = { @@ -18,7 +16,17 @@ export const TABLES = { export type TableId = (typeof TABLES)[keyof typeof TABLES]; -type Row = Models.Document; +export type SystemRow = { + $id: string; + $createdAt: string; + $updatedAt: string; + $permissions: string[]; + $databaseId?: string; + $tableId?: string; + $sequence?: number; +}; + +type Row = SystemRow; export type TenantRole = "owner" | "admin" | "member";