feat: location.pathname based bar navigation + admin layout + separate

auth routes
This commit is contained in:
2025-05-29 23:42:17 +02:00
parent 78e84567c7
commit db2cb36f54
14 changed files with 219 additions and 143 deletions

View File

@ -7,14 +7,42 @@ import RegisterPage from "./pages/Register";
import AuthorizePage from "./pages/Authorize";
import AuthenticatePage from "./pages/Authenticate";
import AuthLayout from "./layout/AuthLayout";
import DashboardLayout from "./layout/DashboardLayout";
import PersonalInfoPage from "./pages/PersonalInfo";
import ApiServicesPage from "./pages/ApiServices";
import AdminLayout from "./layout/AdminLayout";
const router = createBrowserRouter([
{
path: "/",
element: <AuthLayout />,
children: [
{ index: true, element: <IndexPage /> },
{ path: "authorize", element: <AuthorizePage /> },
{
path: "/",
element: <DashboardLayout />,
children: [
{
index: true,
element: <IndexPage />,
},
{
path: "personal-info",
element: <PersonalInfoPage />,
},
{
path: "admin",
element: <AdminLayout />,
children: [{ path: "api-services", element: <ApiServicesPage /> }],
},
],
},
],
},
{
path: "/auth",
element: <AuthLayout />,
children: [
{ index: true, element: <AuthorizePage /> },
{ path: "login", element: <LoginPage /> },
{ path: "register", element: <RegisterPage /> },
{ path: "authenticate", element: <AuthenticatePage /> },

View File

@ -1,26 +1,22 @@
import type { FC } from "react";
import { useBarItems } from "../tabs";
import { Link } from "react-router";
export interface ISidebarProps {
activeTab: string;
onChangeTab: (tab: string) => void;
}
const Sidebar: FC<ISidebarProps> = ({ activeTab, onChangeTab }) => {
const barItems = useBarItems();
const Sidebar: FC = () => {
const [barItems, isActive] = useBarItems();
return (
<div className="hidden sm:flex flex-col gap-2 items-stretch min-w-80 w-80 p-5 pt-18 min-h-screen select-none bg-white/65 dark:bg-black/65 shadow-lg shadow-gray-300 dark:shadow-gray-700">
{barItems.map((item) => (
<div
key={item.tab}
onClick={() => onChangeTab(item.tab)}
className={`dark:text-gray-200 transition-colors text-sm cursor-pointer p-4 rounded-lg flex flex-row items-center gap-3${
item.tab === activeTab ? " bg-gray-200 dark:bg-gray-900" : ""
}`}
>
{item.icon} {item.title}
</div>
<Link to={item.pathname} key={item.tab}>
<div
className={`dark:text-gray-200 transition-colors text-sm cursor-pointer p-4 rounded-lg flex flex-row items-center gap-3${
isActive(item) ? " bg-gray-200 dark:bg-gray-900" : ""
}`}
>
{item.icon} {item.title}
</div>
</Link>
))}
</div>
);

View File

@ -1,30 +0,0 @@
import { Button } from "@/components/ui/button";
import Avatar from "@/feature/Avatar";
import { useAuth } from "@/store/auth";
import { type FC } from "react";
const Home: FC = () => {
const profile = useAuth((state) => state.profile);
const signOut = useAuth((state) => state.signOut);
return (
<div className="flex flex-col items-center gap-2 p-7">
<div className="w-24 h-24 sm:w-36 sm:h-36 overflow-hidden rounded-full flex items-center justify-center bg-gray-300">
<Avatar iconSize={64} />
</div>
<h1 className="dark:text-gray-200 text-gray-800 text-2xl select-none">
Welcome, {profile?.full_name}
</h1>
<p className="text-gray-600 dark:text-gray-500 select-none text-center text-sm/normal sm:text-lg">
Manage your info, private and security to make Home Guard work better
for you.
</p>
<Button className="mt-10" onClick={signOut}>
Sign Out
</Button>
</div>
);
};
export default Home;

View File

@ -1,28 +1,24 @@
import { type FC } from "react";
import { useBarItems } from "../tabs";
import { Link } from "react-router";
export interface ITopBarProps {
activeTab: string;
onChangeTab: (tab: string) => void;
}
const TopBar: FC<ITopBarProps> = ({ activeTab, onChangeTab }) => {
const barItems = useBarItems();
const TopBar: FC = () => {
const [barItems, isActive] = useBarItems();
return (
<div className="sm:hidden flex w-full overflow-x-auto sm:overflow-x-visible max-w-full min-w-full sm:justify-center sm:space-x-4 no-scrollbar shadow-md shadow-gray-300 dark:shadow-gray-700 dark:bg-black/70 bg-white/70 pt-14">
{barItems.map((item) => (
<div
key={item.tab}
onClick={() => onChangeTab(item.tab)}
className={`flex-shrink-0 transition-all border-b-4 px-4 py-2 min-w-[120px] sm:min-w-0 sm:flex-1 flex items-center justify-center cursor-pointer select-none whitespace-nowrap text-sm font-medium ${
item.tab === activeTab
? " border-b-4 border-b-blue-500 text-blue-500"
: " border-b-transparent text-gray-500"
}`}
>
{item.title}
</div>
<Link to={item.pathname} key={item.tab}>
<div
className={`flex-shrink-0 transition-all border-b-4 px-4 py-2 min-w-[120px] sm:min-w-0 sm:flex-1 flex items-center justify-center cursor-pointer select-none whitespace-nowrap text-sm font-medium ${
isActive(item)
? " border-b-4 border-b-blue-500 text-blue-500"
: " border-b-transparent text-gray-500"
}`}
>
{item.title}
</div>
</Link>
))}
</div>
);

View File

@ -1,37 +1,61 @@
import { useAuth } from "@/store/auth";
import { Blocks, Home, Settings2, User } from "lucide-react";
import { useCallback, type ReactNode } from "react";
import { useLocation } from "react-router";
export const useBarItems = () => {
export interface BarItem {
icon: ReactNode;
title: string;
tab: string;
pathname: string;
}
export const useBarItems = (): [BarItem[], (item: BarItem) => boolean] => {
const profile = useAuth((state) => state.profile);
const location = useLocation();
const isActive = useCallback(
(item: BarItem) => {
return location.pathname === item.pathname;
},
[location.pathname],
);
if (!profile) {
return [];
return [[], isActive];
}
return [
{
icon: <Home />,
title: "Home",
tab: "home",
},
{
icon: <User />,
title: "Personal Info",
tab: "personal-info",
},
{
icon: <Settings2 />,
title: "Data & Personalization",
tab: "data-personalization",
},
...(profile.isAdmin
? [
{
icon: <Blocks />,
title: "API Services",
tab: "api-services",
},
]
: []),
[
{
icon: <Home />,
title: "Home",
tab: "home",
pathname: "/",
},
{
icon: <User />,
title: "Personal Info",
tab: "personal-info",
pathname: "/personal-info",
},
{
icon: <Settings2 />,
title: "Data & Personalization",
tab: "data-personalization",
pathname: "/data-personalize",
},
...(profile.isAdmin
? [
{
icon: <Blocks />,
title: "API Services",
tab: "api-services",
pathname: "/admin/api-services",
},
]
: []),
],
isActive,
];
};

View File

@ -41,7 +41,7 @@ const AccountList: FC = () => {
</div>
</div>
))}
<Link to="/login" state={location.state}>
<Link to="/auth/login" state={location.state}>
<div className="flex flex-row items-center p-4 border-gray-200 dark:border-gray-700/65 border-b border-r-0 border-l-0 select-none cursor-pointer hover:bg-gray-50/50 dark:hover:bg-gray-800/10 transition-colors mb-0">
<div>
<div className="rounded-full p-2 text-gray-900 dark:text-gray-200 mr-3">

View File

@ -0,0 +1,42 @@
import { useAuth } from "@/store/auth";
import type { FC } from "react";
import { Navigate, Outlet } from "react-router";
const AdminLayout: FC = () => {
const profile = useAuth((state) => state.profile);
if (!profile) {
<div className="w-screen h-screen flex flex-1 items-center justify-center flex-col">
<div role="status">
<svg
aria-hidden="true"
className="w-10 h-10 text-gray-400 animate-spin fill-white"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
<p className="text-gray-200 dark:text-gray-400 mt-4 text-lg">
Loading...
</p>
</div>;
}
if (!profile?.isAdmin) {
return <Navigate to="/" />;
}
return <Outlet />;
};
export default AdminLayout;

View File

@ -45,8 +45,7 @@ const AuthLayout = () => {
const navigate = useNavigate();
const isAuthPage = useMemo(() => {
const allowedPaths = ["/login", "/register", "/authenticate"];
return allowedPaths.some((p) => location.pathname.startsWith(p));
return location.pathname.startsWith("/auth");
}, [location.pathname]);
const loading = useMemo(() => {
@ -116,7 +115,7 @@ const AuthLayout = () => {
if (signInRequired && !isAuthPage) {
return (
<Navigate
to={hasAccounts ? "/authenticate" : "/login"}
to={hasAccounts ? "/auth/authenticate" : "/auth/login"}
state={{ from: location.pathname }}
/>
);

View File

@ -0,0 +1,41 @@
import Sidebar from "@/components/Home/Sidebar";
import TopBar from "@/components/Home/TopBar";
import { Card, CardContent } from "@/components/ui/card";
import { type FC } from "react";
import { Outlet } from "react-router";
const DashboardLayout: FC = () => {
return (
<div className="relative z-10 flex items-center justify-center min-h-screen">
<Card className="overflow-y-auto min-h-screen w-full min-w-full shadow-lg bg-white/85 dark:bg-black/85 backdrop-blur-md sm:rounded-none">
<div className="flex flex-col items-center sm:pt-0 relative">
<div className="flex flex-row items-center absolute left-4 top-4">
<img src="/icon.png" alt="icon" className="w-6 h-6" />
<div className="ml-2">
<p className="text-sm text-gray-600 text-left dark:text-gray-500">
Home Guard
</p>
</div>
</div>
<CardContent className="w-full space-y-4 flex-1" spacing={false}>
<div className="flex flex-row">
<Sidebar />
<div className="sm:p-4 max-w-full flex-1">
<div className="flex flex-col w-full items-center gap-2">
<TopBar />
</div>
<div className="p-4">
<Outlet />
</div>
</div>
</div>
</CardContent>
</div>
</Card>
</div>
);
};
export default DashboardLayout;

View File

@ -36,7 +36,7 @@ const services = [
},
];
const ApiServices: FC = () => {
const ApiServicesPage: FC = () => {
return (
<div className="overflow-x-auto rounded shadow-md dark:shadow-gray-800">
<table className="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
@ -101,4 +101,4 @@ const ApiServices: FC = () => {
);
};
export default ApiServices;
export default ApiServicesPage;

View File

@ -1,51 +1,31 @@
import { useState, type FC } from "react";
import { Button } from "@/components/ui/button";
import Avatar from "@/feature/Avatar";
import { useAuth } from "@/store/auth";
import { type FC } from "react";
// import overlay from "@/assets/overlay.jpg";
// import darkOverlay from "@/assets/dark-overlay.jpg";
import { Card, CardContent } from "@/components/ui/card";
import Sidebar from "@/components/Home/Sidebar";
import TopBar from "@/components/Home/TopBar";
import Home from "@/components/Home/Tabs/Home";
import PersonalInfo from "@/components/Home/Tabs/PersonalInfo";
import ApiServices from "@/components/Home/Tabs/ApiServices";
const IndexPage: FC = () => {
const [tab, setTab] = useState<string>("home");
const profile = useAuth((state) => state.profile);
const signOut = useAuth((state) => state.signOut);
return (
<div className="relative z-10 flex items-center justify-center min-h-screen">
<Card className="overflow-y-auto min-h-screen w-full min-w-full shadow-lg bg-white/85 dark:bg-black/85 backdrop-blur-md sm:rounded-none">
<div className="flex flex-col items-center sm:pt-0 relative">
<div className="flex flex-row items-center absolute left-4 top-4">
<img src="/icon.png" alt="icon" className="w-6 h-6" />
<div className="flex flex-col items-center gap-2 p-7">
<div className="w-24 h-24 sm:w-36 sm:h-36 overflow-hidden rounded-full flex items-center justify-center bg-gray-300">
<Avatar iconSize={64} />
</div>
<h1 className="dark:text-gray-200 text-gray-800 text-2xl select-none">
Welcome, {profile?.full_name}
</h1>
<p className="text-gray-600 dark:text-gray-500 select-none text-center text-sm/normal sm:text-lg">
Manage your info, private and security to make Home Guard work better
for you.
</p>
<div className="ml-2">
<p className="text-sm text-gray-600 text-left dark:text-gray-500">
Home Guard
</p>
</div>
</div>
{/* <LogIn className="w-8 h-8 text-gray-700 mb-4" /> */}
<CardContent className="w-full space-y-4 flex-1" spacing={false}>
<div className="flex flex-row">
<Sidebar activeTab={tab} onChangeTab={(tab) => setTab(tab)} />
<div className="sm:p-4 max-w-full flex-1">
<div className="flex flex-col w-full items-center gap-2">
<TopBar activeTab={tab} onChangeTab={(tab) => setTab(tab)} />
{tab === "home" && <Home />}
{/* {tab === "personal-info" && <PersonalInfo />} */}
</div>
<div className="p-4">
{tab === "personal-info" && <PersonalInfo />}
{tab === "api-services" && <ApiServices />}
</div>
</div>
</div>
</CardContent>
</div>
</Card>
<Button className="mt-10" onClick={signOut}>
Sign Out
</Button>
</div>
);
};

View File

@ -161,7 +161,7 @@ export default function LoginPage() {
<div className="text-sm text-center text-gray-600">
Don't have an account?{" "}
<Link
to="/register"
to="/auth/register"
state={location.state}
className="text-blue-600 hover:underline"
>

View File

@ -1,9 +1,9 @@
import Avatar from "@/feature/Avatar";
import { useAuth } from "@/store/auth";
import { ChevronRight } from "lucide-react";
import { type FC } from "react";
import type { FC } from "react";
const PersonalInfo: FC = () => {
const PersonalInfoPage: FC = () => {
const profile = useAuth((state) => state.profile);
return (
@ -65,4 +65,4 @@ const PersonalInfo: FC = () => {
);
};
export default PersonalInfo;
export default PersonalInfoPage;

View File

@ -239,7 +239,7 @@ export default function RegisterPage() {
<div className="text-sm text-center text-gray-600">
Already have an account?{" "}
<Link
to="/login"
to="/auth/login"
state={location.state}
className="text-blue-600 hover:underline"
>