Spaces:
Runtime error

File size: 3,698 Bytes
ad79323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useEffect, useState } from "react";
import { Menu01Icon } from "@hugeicons/core-free-icons";

import { DashboardSidebar } from "@/components/dashboard/dashboard-sidebar";
import { AppLogo } from "@/components/shared/app-logo";
import { Icon } from "@/components/shared/icon";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";

const SIDEBAR_STORAGE_KEY = "courtrix_sidebar_collapsed";

type DashboardShellProps = {
  user: {
    firstName: string;
    lastName: string;
    email: string;
  };
  children: React.ReactNode;
};

export function DashboardShell({ user, children }: DashboardShellProps) {
  const [isDesktopCollapsed, setIsDesktopCollapsed] = useState(() => {
    if (typeof window === "undefined") {
      return false;
    }

    return window.localStorage.getItem(SIDEBAR_STORAGE_KEY) === "true";
  });
  const [isMobile, setIsMobile] = useState(() => {
    if (typeof window === "undefined") {
      return false;
    }

    return window.matchMedia("(max-width: 1023px)").matches;
  });
  const [isMobileOpen, setIsMobileOpen] = useState(false);

  useEffect(() => {
    const mediaQuery = window.matchMedia("(max-width: 1023px)");
    const handleViewportChange = () => {
      const mobile = mediaQuery.matches;
      setIsMobile(mobile);

      if (mobile) {
        setIsMobileOpen(false);
      }
    };

    mediaQuery.addEventListener("change", handleViewportChange);

    return () => mediaQuery.removeEventListener("change", handleViewportChange);
  }, []);

  function toggleDesktopCollapse() {
    setIsDesktopCollapsed((current) => {
      const next = !current;
      window.localStorage.setItem(SIDEBAR_STORAGE_KEY, String(next));
      return next;
    });
  }

  return (
    <div className="flex min-h-screen bg-[var(--background)]" dir="rtl">
      {/* Sidebar - placed first in DOM means it will be on the right in RTL */}
      <aside
        className={cn(
          "hidden lg:block sticky top-0 h-screen shrink-0 border-l border-[var(--border)] transition-[width] duration-300",
          isDesktopCollapsed ? "w-[92px]" : "w-[290px]"
        )}
      >
        <DashboardSidebar
          collapsed={isDesktopCollapsed}
          onToggleCollapse={toggleDesktopCollapse}
          user={user}
        />
      </aside>

      <div className="flex-1 min-w-0 flex flex-col h-screen overflow-y-auto">
        <div className="p-4 lg:p-8">
          <div className="mb-6 flex items-center justify-between gap-3 lg:hidden">
            <Button size="icon" variant="outline" onClick={() => setIsMobileOpen(true)}>
              <Icon icon={Menu01Icon} size={18} />
            </Button>
            <AppLogo />
          </div>

          <main className="mx-auto max-w-[1200px] space-y-5">{children}</main>
        </div>
      </div>

      {isMobile ? (
        <div
          className={cn(
            "fixed inset-0 z-40 bg-[rgba(20,13,7,0.5)] backdrop-blur-[2px] transition-opacity lg:hidden",
            isMobileOpen
              ? "pointer-events-auto opacity-100"
              : "pointer-events-none opacity-0",
          )}
          onClick={() => setIsMobileOpen(false)}
        >
          <div
            className={cn(
              "absolute inset-y-0 right-0 w-[290px] max-w-full transform p-3 transition-transform",
              isMobileOpen ? "translate-x-0" : "translate-x-full",
            )}
            onClick={(event) => event.stopPropagation()}
          >
            <DashboardSidebar
              mobile
              onCloseMobile={() => setIsMobileOpen(false)}
              user={user}
            />
          </div>
        </div>
      ) : null}
    </div>
  );
}