Spaces:
Running

File size: 11,804 Bytes
03608f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"use client";

import {
  ActivityIcon,
  BanknoteIcon,
  BarChart3Icon,
  BookOpenIcon,
  BrainIcon,
  Building2Icon,
  CheckIcon,
  ChevronsUpDownIcon,
  InboxIcon,
  LanguagesIcon,
  LogOutIcon,
  MenuIcon,
  MessageSquareIcon,
  MonitorIcon,
  MoonIcon,
  PackageIcon,
  PlugIcon,
  ScaleIcon,
  SettingsIcon,
  SunIcon,
  WorkflowIcon,
  type LucideIcon,
} from "lucide-react";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { useTheme } from "next-themes";
import { useState, type ReactNode } from "react";
import { toast } from "sonner";

import { Button } from "@/components/ui/button";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuRadioGroup,
  DropdownMenuRadioItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Sheet, SheetContent, SheetTitle, SheetTrigger } from "@/components/ui/sheet";
import { useAuth } from "@/lib/auth";
import { useI18n, type MessageKey } from "@/lib/i18n";
import type { Locale } from "@/lib/i18n/messages";
import type { ApprovalItem } from "@/lib/types";
import { useApi } from "@/lib/use-api";
import { cn } from "@/lib/utils";

interface NavItem {
  href: string;
  label: MessageKey;
  icon: LucideIcon;
}

const NAV: { group: MessageKey; items: NavItem[] }[] = [
  {
    group: "nav.operate",
    items: [
      { href: "/inbox", label: "nav.inbox", icon: InboxIcon },
      { href: "/runs", label: "nav.runs", icon: ActivityIcon },
      { href: "/workflows", label: "nav.workflows", icon: WorkflowIcon },
      { href: "/chat", label: "nav.chat", icon: MessageSquareIcon },
    ],
  },
  {
    group: "nav.commerce",
    items: [
      { href: "/orders", label: "nav.orders", icon: PackageIcon },
      { href: "/cod", label: "nav.cod", icon: BanknoteIcon },
    ],
  },
  {
    group: "nav.insight",
    items: [
      { href: "/analytics", label: "nav.analytics", icon: BarChart3Icon },
      { href: "/knowledge", label: "nav.knowledge", icon: BookOpenIcon },
      { href: "/memory", label: "nav.memory", icon: BrainIcon },
      { href: "/integrations", label: "nav.integrations", icon: PlugIcon },
      { href: "/rules", label: "nav.rules", icon: ScaleIcon },
      { href: "/settings", label: "nav.settings", icon: SettingsIcon },
    ],
  },
];

function Logo() {
  const { t } = useI18n();
  return (
    <Link href="/inbox" className="flex items-center gap-2 px-2 font-semibold tracking-tight">

      <span

        className="grid size-7 place-items-center rounded-md bg-primary text-primary-foreground"

        aria-hidden

      >

        <svg viewBox="0 0 24 24" className="size-4" fill="none" stroke="currentColor" strokeWidth={2.2}>

          <path d="M4 7l4 10 4-7 4 7 4-10" strokeLinecap="round" strokeLinejoin="round" />

        </svg>

      </span>

      <span>{t("app.name")}</span>

    </Link>
  );
}

function NavLinks({ onNavigate }: { onNavigate?: () => void }) {
  const { t } = useI18n();
  const pathname = usePathname();
  const pending = useApi<ApprovalItem[]>("/approvals", { pollMs: 15_000 });
  const count = pending.data?.length ?? 0;
  return (
    <nav className="space-y-5" aria-label={t("common.mainNav")}>

      {NAV.map((g) => (

        <div key={g.group} className="space-y-1">

          <p className="px-2 text-[11px] font-medium tracking-wide text-muted-foreground uppercase">

            {t(g.group)}

          </p>

          {g.items.map((item) => {

            const active = pathname === item.href || pathname.startsWith(`${item.href}/`);

            return (

              <Link

                key={item.href}

                href={item.href}

                onClick={onNavigate}

                aria-current={active ? "page" : undefined}

                className={cn(

                  "flex items-center gap-2.5 rounded-md px-2 py-1.5 text-sm transition-colors",

                  active

                    ? "bg-sidebar-accent font-medium text-sidebar-accent-foreground"

                    : "text-sidebar-foreground/80 hover:bg-sidebar-accent/60 hover:text-sidebar-accent-foreground",

                )}

              >

                <item.icon className="size-4 shrink-0" aria-hidden />

                <span className="flex-1">{t(item.label)}</span>

                {item.href === "/inbox" && count > 0 && (

                  <span

                    className="tabular min-w-5 rounded-full bg-warning-soft px-1.5 text-center text-[11px] font-semibold text-warning"

                    data-testid="inbox-count"

                  >

                    {count}

                  </span>

                )}

              </Link>

            );

          })}

        </div>

      ))}

    </nav>
  );
}

function TenantSwitcher() {
  const { me, switchTenant } = useAuth();
  const { t, m } = useI18n();
  const router = useRouter();
  if (!me) return null;
  return (
    <DropdownMenu>

      <DropdownMenuTrigger asChild>

        <Button

          variant="ghost"

          className="h-auto w-full justify-between gap-2 px-2 py-1.5 text-start"

          data-testid="tenant-switcher"

        >

          <span className="flex min-w-0 items-center gap-2">

            <Building2Icon className="size-4 shrink-0 text-muted-foreground" aria-hidden />

            <span className="min-w-0">

              <span className="block truncate text-sm font-medium">{me.tenant.name}</span>

              <span className="block text-[11px] text-muted-foreground">

                {m.settings.roles[me.role]}

              </span>

            </span>

          </span>

          <ChevronsUpDownIcon className="size-3.5 text-muted-foreground" aria-hidden />

        </Button>

      </DropdownMenuTrigger>

      <DropdownMenuContent align="start" className="w-60">

        <DropdownMenuLabel>{t("common.workspace")}</DropdownMenuLabel>

        {me.tenants.map((tn) => (

          <DropdownMenuItem

            key={tn.id}

            onSelect={async () => {

              if (tn.id === me.tenant.id) return;

              try {

                await switchTenant(tn.id);

                router.push("/inbox");

              } catch {

                toast.error(t("common.error"));

              }

            }}

          >

            <span className="flex-1 truncate">{tn.name}</span>

            <span className="text-[11px] text-muted-foreground">{m.settings.roles[tn.role]}</span>

            {tn.id === me.tenant.id && <CheckIcon className="size-3.5" aria-hidden />}

          </DropdownMenuItem>

        ))}

      </DropdownMenuContent>

    </DropdownMenu>
  );
}

function Preferences() {
  const { t, locale, setLocale } = useI18n();
  const { theme, setTheme } = useTheme();
  const { me, logout } = useAuth();
  const router = useRouter();
  return (
    <div className="flex items-center gap-1">

      <Button

        variant="ghost"

        size="sm"

        onClick={() => setLocale(locale === "en" ? "ur" : "en")}

        aria-label={t("common.language")}

        data-testid="locale-toggle"

        className={locale === "en" ? "font-urdu" : ""}

      >

        <LanguagesIcon aria-hidden />

        {locale === "en" ? "اردو" : "English"}

      </Button>

      <DropdownMenu>

        <DropdownMenuTrigger asChild>

          <Button variant="ghost" size="icon-sm" aria-label={t("common.theme")} data-testid="theme-toggle">

            <SunIcon className="dark:hidden" aria-hidden />

            <MoonIcon className="hidden dark:block" aria-hidden />

          </Button>

        </DropdownMenuTrigger>

        <DropdownMenuContent align="end">

          <DropdownMenuLabel>{t("common.theme")}</DropdownMenuLabel>

          <DropdownMenuRadioGroup value={theme ?? "system"} onValueChange={setTheme}>

            <DropdownMenuRadioItem value="light">

              <SunIcon aria-hidden /> {t("common.light")}

            </DropdownMenuRadioItem>

            <DropdownMenuRadioItem value="dark">

              <MoonIcon aria-hidden /> {t("common.dark")}

            </DropdownMenuRadioItem>

            <DropdownMenuRadioItem value="system">

              <MonitorIcon aria-hidden /> {t("common.system")}

            </DropdownMenuRadioItem>

          </DropdownMenuRadioGroup>

          <DropdownMenuSeparator />

          <DropdownMenuLabel>{t("common.language")}</DropdownMenuLabel>

          <DropdownMenuRadioGroup value={locale} onValueChange={(v) => setLocale(v as Locale)}>

            <DropdownMenuRadioItem value="en">English</DropdownMenuRadioItem>

            <DropdownMenuRadioItem value="ur" className="font-urdu">

              اردو

            </DropdownMenuRadioItem>

          </DropdownMenuRadioGroup>

        </DropdownMenuContent>

      </DropdownMenu>

      {me && (

        <DropdownMenu>

          <DropdownMenuTrigger asChild>

            <Button

              variant="ghost"

              size="icon-sm"

              className="rounded-full"

              aria-label={me.user.email}

              data-testid="user-menu"

            >

              <span className="grid size-7 place-items-center rounded-full bg-muted text-xs font-semibold uppercase">

                {me.user.email.slice(0, 1)}

              </span>

            </Button>

          </DropdownMenuTrigger>

          <DropdownMenuContent align="end" className="w-56">

            <DropdownMenuLabel className="truncate font-normal text-muted-foreground" dir="ltr">

              {me.user.email}

            </DropdownMenuLabel>

            <DropdownMenuSeparator />

            <DropdownMenuItem

              onSelect={async () => {

                await logout();

                router.replace("/login");

              }}

            >

              <LogOutIcon aria-hidden />

              {t("common.signOut")}

            </DropdownMenuItem>

          </DropdownMenuContent>

        </DropdownMenu>

      )}

    </div>
  );
}

export function AppShell({ children }: { children: ReactNode }) {
  const [open, setOpen] = useState(false);
  const { dir, t } = useI18n();
  return (
    <div className="flex min-h-dvh">

      <aside className="sticky top-0 hidden h-dvh w-60 shrink-0 flex-col gap-4 border-e bg-sidebar px-3 py-4 md:flex">

        <Logo />

        <TenantSwitcher />

        <div className="flex-1 overflow-y-auto">

          <NavLinks />

        </div>

      </aside>

      <div className="flex min-w-0 flex-1 flex-col">

        <header className="sticky top-0 z-20 flex h-14 items-center gap-2 border-b bg-background/90 px-4 backdrop-blur">

          <Sheet open={open} onOpenChange={setOpen}>

            <SheetTrigger asChild>

              <Button variant="ghost" size="icon-sm" className="md:hidden" aria-label={t("common.menu")}>

                <MenuIcon aria-hidden />

              </Button>

            </SheetTrigger>

            <SheetContent side={dir === "rtl" ? "right" : "left"} className="w-64 gap-4 px-3 py-4">

              <SheetTitle className="sr-only">{t("common.menu")}</SheetTitle>

              <Logo />

              <TenantSwitcher />

              <NavLinks onNavigate={() => setOpen(false)} />

            </SheetContent>

          </Sheet>

          <div className="md:hidden">

            <Logo />

          </div>

          <div className="ms-auto">

            <Preferences />

          </div>

        </header>

        <main id="main" className="mx-auto w-full max-w-6xl flex-1 px-4 py-6 md:px-8">

          {children}

        </main>

      </div>

    </div>
  );
}