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";