Repository navigation
Expand file tree
/
Copy pathHomePage.tsx
More file actions
166 lines (150 loc) · 5.43 KB
/
Copy pathHomePage.tsx
File metadata and controls
166 lines (150 loc) · 5.43 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use client";
/**
* Home (store overview) for admin: statistics cards and quick links to products, categories, suppliers.
* REQ-0021 — shell-first layout; hooks receive SSR initialData directly.
*/
import Link from "next/link";
import { useRouter, useSearchParams } from "next/navigation";
import React, { useEffect, useState, useRef } from "react";
import { useAuth } from "@/contexts";
import ProductList from "@/components/products/ProductList";
import SupplierList from "@/components/supplier/SupplierList";
import CategoryList from "@/components/category/CategoryList";
import { StatisticsSection } from "@/components/home/StatisticsSection";
import Navbar from "@/components/layouts/Navbar";
import { PageContentWrapper } from "@/components/shared";
import FloatingActionButtons from "@/components/shared/FloatingActionButtons";
import { PageSectionHeader } from "@/components/shared";
import { useProducts } from "@/hooks/queries";
import { queryKeys, useSyncSsrQueryData } from "@/lib/react-query";
import type {
ProductForHome,
CategoryForHome,
SupplierForHome,
} from "@/lib/server/home-data";
import type { DashboardStats } from "@/types";
import { LayoutDashboard } from "lucide-react";
export type HomePageProps = {
initialProducts?: ProductForHome[];
initialCategories?: CategoryForHome[];
initialSuppliers?: SupplierForHome[];
initialStats?: DashboardStats | null;
/** From server searchParams — avoids relying on client-only OAuth detection on first paint */
initialOAuthSuccess?: boolean;
};
/**
* Home page client component (initialOAuthSuccess from server; useSearchParams for URL fallback).
* Accepts optional server-fetched data for first-render hydration via hook initialData.
*/
export default function HomePage({
initialProducts,
initialCategories,
initialSuppliers,
initialStats,
initialOAuthSuccess = false,
}: HomePageProps = {}) {
const router = useRouter();
const searchParams = useSearchParams();
const { isLoggedIn, isCheckingAuth, user, refreshSession } = useAuth();
const { data: allProducts = [] } = useProducts(initialProducts);
useSyncSsrQueryData(queryKeys.products.lists(), initialProducts);
useSyncSsrQueryData(queryKeys.categories.lists(), initialCategories);
useSyncSsrQueryData(queryKeys.suppliers.lists(), initialSuppliers);
const [isRefreshingOAuth, setIsRefreshingOAuth] = useState(false);
const [oauthRefreshComplete, setOauthRefreshComplete] = useState(false);
const oauthHandledRef = useRef(false);
const urlCleanedRef = useRef(false);
useEffect(() => {
const isOAuthFlow =
initialOAuthSuccess || searchParams.get("oauth_success") === "true";
if (isOAuthFlow && !oauthHandledRef.current) {
oauthHandledRef.current = true;
queueMicrotask(() => setIsRefreshingOAuth(true));
refreshSession()
.then(() => {
setOauthRefreshComplete(true);
setIsRefreshingOAuth(false);
if (
!urlCleanedRef.current &&
typeof window !== "undefined" &&
window.location.search.includes("oauth_success=true")
) {
urlCleanedRef.current = true;
window.history.replaceState(
{ ...window.history.state, as: "/", url: "/" },
"",
"/",
);
}
})
.catch(() => {
setOauthRefreshComplete(true);
setIsRefreshingOAuth(false);
});
return;
}
if (!isOAuthFlow || oauthRefreshComplete) {
if (isOAuthFlow && oauthRefreshComplete) {
if (!isCheckingAuth && !isLoggedIn) {
router.replace("/login", { scroll: false });
}
return;
}
if (!isOAuthFlow && !isCheckingAuth && !isLoggedIn) {
router.replace("/login", { scroll: false });
}
}
}, [
initialOAuthSuccess,
searchParams,
isLoggedIn,
isCheckingAuth,
router,
refreshSession,
isRefreshingOAuth,
oauthRefreshComplete,
]);
return (
<Navbar>
<PageContentWrapper>
<PageSectionHeader
as="h2"
icon={LayoutDashboard}
tone="sky"
title="Store Overview"
description={
<>
The cards below show your store-wide metrics as the store owner,
including your own activity and activity from clients and others.
Numbers update automatically when you or others make changes. For
your personal orders, products, and activity only, visit{" "}
<Link
href="/admin/my-activity"
className="font-medium text-sky-600 hover:text-sky-800 dark:text-sky-400 dark:hover:text-sky-300"
>
My Activities
</Link>
.
</>
}
/>
<div id="statistics" className="pb-6 scroll-mt-20">
<StatisticsSection initialStats={initialStats} />
</div>
<div id="products" className="pb-6 scroll-mt-20">
<ProductList initialProducts={initialProducts} />
</div>
<div id="suppliers" className="pb-6 scroll-mt-20">
<SupplierList initialSuppliers={initialSuppliers} />
</div>
<div id="categories" className="pb-6 scroll-mt-20">
<CategoryList initialCategories={initialCategories} />
</div>
<FloatingActionButtons
allProducts={allProducts}
userId={user?.id || ""}
/>
</PageContentWrapper>
</Navbar>
);
}