|
2 | 2 | import { navigations } from "$constants";
|
3 | 3 | import Separator from "$components/ui/separator/separator.svelte";
|
4 | 4 | import * as Sidebar from "$lib/components/ui/sidebar";
|
| 5 | + import * as Avatar from "$lib/components/ui/avatar"; |
| 6 | + import * as DropdownMenu from "$lib/components/ui/dropdown-menu"; |
| 7 | + import * as Dialog from "$lib/components/ui/dialog"; |
5 | 8 | import { useAuth } from "$lib/hooks/auth.svelte";
|
6 | 9 | import LogOut from "lucide-svelte/icons/log-out";
|
7 | 10 | import Mails from "lucide-svelte/icons/mails";
|
|
14 | 17 | import { crossfade } from "svelte/transition";
|
15 | 18 | import { cubicInOut } from "svelte/easing";
|
16 | 19 | import IconRoundcube from "$components/icons/icon-roundcube.svelte";
|
| 20 | + import Loading from "./loading.svelte"; |
| 21 | + import ChevronsUpDown from "lucide-svelte/icons/chevrons-up-down"; |
17 | 22 |
|
18 | 23 | let { ref = $bindable(null), ...restProps }: ComponentProps<typeof Sidebar.Root> = $props();
|
19 | 24 |
|
20 | 25 | const auth = useAuth();
|
21 | 26 | const sidebar = Sidebar.useSidebar();
|
22 | 27 |
|
| 28 | + let showModalConfirmation = $state(false); |
| 29 | +
|
23 | 30 | const [send, receive] = crossfade({
|
24 | 31 | duration: 250,
|
25 | 32 | easing: cubicInOut
|
26 | 33 | });
|
27 | 34 |
|
| 35 | + const getShortName = () => { |
| 36 | + const fullName = auth.user?.full_name ?? ""; |
| 37 | + const match = fullName.match(/\b(\w)/g) ?? []; |
| 38 | + return match.slice(0, 2).join("").toUpperCase(); |
| 39 | + }; |
| 40 | +
|
28 | 41 | const handleNavigationMobile = () => {
|
29 | 42 | if (!sidebar.isMobile) return;
|
30 | 43 | sidebar.toggle();
|
|
36 | 49 | };
|
37 | 50 | </script>
|
38 | 51 |
|
39 |
| -<Sidebar.Root bind:ref variant="inset" collapsible="icon" {...restProps}> |
40 |
| - <Sidebar.Content> |
41 |
| - <Sidebar.Group> |
42 |
| - <Sidebar.Header> |
43 |
| - <Sidebar.Menu> |
44 |
| - <Sidebar.MenuItem> |
45 |
| - <Sidebar.MenuButton onclick={() => sidebar.toggle()} size="lg"> |
46 |
| - <div |
47 |
| - class="flex aspect-square size-8 items-center justify-center rounded-lg bg-sidebar-primary text-sidebar-primary-foreground" |
48 |
| - > |
49 |
| - <Mails class="size-4" /> |
50 |
| - </div> |
51 |
| - <div class="grid flex-1 text-left text-sm leading-tight"> |
52 |
| - <span class="truncate font-semibold">{auth.user?.full_name}</span> |
53 |
| - <span class="truncate text-xs">@{auth.user?.username}</span> |
54 |
| - </div> |
55 |
| - </Sidebar.MenuButton> |
56 |
| - </Sidebar.MenuItem> |
57 |
| - </Sidebar.Menu> |
58 |
| - </Sidebar.Header> |
59 |
| - <Separator class="mb-3" /> |
60 |
| - <Sidebar.GroupContent> |
61 |
| - <Sidebar.Menu> |
62 |
| - {#each navigations as item (item.name)} |
| 52 | +<Dialog.Root open={showModalConfirmation} onOpenChange={(e) => (showModalConfirmation = e)}> |
| 53 | + <Sidebar.Root bind:ref variant="inset" collapsible="icon" {...restProps}> |
| 54 | + <Sidebar.Content> |
| 55 | + <Sidebar.Group> |
| 56 | + <Sidebar.Header> |
| 57 | + <Sidebar.Menu> |
63 | 58 | <Sidebar.MenuItem>
|
64 |
| - {@const isActive = page.url.pathname.startsWith(item.url)} |
65 |
| - <Sidebar.MenuButton {isActive} class="relative"> |
| 59 | + <Sidebar.MenuButton onclick={() => sidebar.toggle()} size="lg"> |
| 60 | + <div |
| 61 | + class="flex aspect-square size-8 items-center justify-center rounded-lg bg-sidebar-primary text-sidebar-primary-foreground" |
| 62 | + > |
| 63 | + <Mails class="size-4" /> |
| 64 | + </div> |
| 65 | + <h2 class="shrink-0 text-lg font-semibold">G/W Mail</h2> |
| 66 | + </Sidebar.MenuButton> |
| 67 | + </Sidebar.MenuItem> |
| 68 | + </Sidebar.Menu> |
| 69 | + </Sidebar.Header> |
| 70 | + <Separator class="mb-3" /> |
| 71 | + <Sidebar.GroupContent> |
| 72 | + <Sidebar.Menu> |
| 73 | + {#each navigations as item (item.name)} |
| 74 | + <Sidebar.MenuItem> |
| 75 | + {@const isActive = page.url.pathname.startsWith(item.url)} |
| 76 | + <Sidebar.MenuButton {isActive} class="relative"> |
| 77 | + {#snippet child({ props })} |
| 78 | + {@const className = props.class as string} |
| 79 | + <a |
| 80 | + href={item.url} |
| 81 | + onclick={handleNavigationMobile} |
| 82 | + {...props} |
| 83 | + class={cn("relative z-10", className)} |
| 84 | + > |
| 85 | + <item.icon /> |
| 86 | + <span>{item.name}</span> |
| 87 | + </a> |
| 88 | + {#if isActive} |
| 89 | + <!-- svelte-ignore element_invalid_self_closing_tag --> |
| 90 | + <div |
| 91 | + class={cn( |
| 92 | + "absolute inset-0 rounded-md bg-sidebar-accent", |
| 93 | + isActive && |
| 94 | + "overflow-hidden before:absolute before:left-0 before:h-full before:w-0.5 before:bg-foreground" |
| 95 | + )} |
| 96 | + in:send={{ key: "active-sidebar-tab" }} |
| 97 | + out:receive={{ key: "active-sidebar-tab" }} |
| 98 | + /> |
| 99 | + {/if} |
| 100 | + {/snippet} |
| 101 | + </Sidebar.MenuButton> |
| 102 | + </Sidebar.MenuItem> |
| 103 | + {/each} |
| 104 | + <Sidebar.MenuItem> |
| 105 | + <Sidebar.MenuButton> |
66 | 106 | {#snippet child({ props })}
|
67 | 107 | {@const className = props.class as string}
|
68 | 108 | <a
|
69 |
| - href={item.url} |
70 |
| - onclick={handleNavigationMobile} |
| 109 | + href="https://mail.gnuweeb.org/roundcube/" |
71 | 110 | {...props}
|
72 |
| - class={cn("relative z-10", className)} |
| 111 | + class={cn("group/roundcube", className)} |
| 112 | + target="_blank" |
73 | 113 | >
|
74 |
| - <item.icon /> |
75 |
| - <span>{item.name}</span> |
76 |
| - </a> |
77 |
| - {#if isActive} |
78 |
| - <!-- svelte-ignore element_invalid_self_closing_tag --> |
79 |
| - <div |
80 |
| - class={cn( |
81 |
| - "absolute inset-0 rounded-md bg-sidebar-accent", |
82 |
| - isActive && |
83 |
| - "overflow-hidden before:absolute before:left-0 before:h-full before:w-0.5 before:bg-foreground" |
84 |
| - )} |
85 |
| - in:send={{ key: "active-sidebar-tab" }} |
86 |
| - out:receive={{ key: "active-sidebar-tab" }} |
| 114 | + <IconRoundcube /> |
| 115 | + <span>Roundcube</span> |
| 116 | + <SquareArrowOutUpRight |
| 117 | + class="!size-3 transition-transform group-hover/roundcube:!scale-125" |
87 | 118 | />
|
88 |
| - {/if} |
| 119 | + </a> |
89 | 120 | {/snippet}
|
90 | 121 | </Sidebar.MenuButton>
|
91 | 122 | </Sidebar.MenuItem>
|
92 |
| - {/each} |
93 |
| - <Sidebar.MenuItem> |
94 |
| - <Sidebar.MenuButton> |
| 123 | + </Sidebar.Menu> |
| 124 | + </Sidebar.GroupContent> |
| 125 | + </Sidebar.Group> |
| 126 | + </Sidebar.Content> |
| 127 | + <Sidebar.Footer> |
| 128 | + <Sidebar.Menu> |
| 129 | + <Sidebar.MenuItem> |
| 130 | + <DropdownMenu.Root> |
| 131 | + <DropdownMenu.Trigger> |
95 | 132 | {#snippet child({ props })}
|
96 |
| - {@const className = props.class as string} |
97 |
| - <a |
98 |
| - href="https://mail.gnuweeb.org/roundcube/" |
| 133 | + <Sidebar.MenuButton |
| 134 | + size="lg" |
| 135 | + class="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground" |
99 | 136 | {...props}
|
100 |
| - class={cn("group/roundcube", className)} |
101 |
| - target="_blank" |
102 | 137 | >
|
103 |
| - <IconRoundcube /> |
104 |
| - <span>Roundcube</span> |
105 |
| - <SquareArrowOutUpRight |
106 |
| - class="!size-3 transition-transform group-hover/roundcube:!scale-125" |
107 |
| - /> |
108 |
| - </a> |
| 138 | + <Avatar.Root class="h-8 w-8 rounded-lg"> |
| 139 | + <Avatar.Image src={auth.user?.photo} alt="{auth.user?.username}@gnuweeb.org" /> |
| 140 | + <Avatar.Fallback class="rounded-lg text-xs"> |
| 141 | + {#if !Boolean(auth.user?.photo)} |
| 142 | + {getShortName()} |
| 143 | + {:else} |
| 144 | + <Loading class="size-3 text-black" /> |
| 145 | + {/if} |
| 146 | + </Avatar.Fallback> |
| 147 | + </Avatar.Root> |
| 148 | + <div class="grid flex-1 text-left text-sm leading-tight"> |
| 149 | + <span class="truncate font-semibold">{auth.user?.full_name}</span> |
| 150 | + <span class="truncate text-xs">{auth.user?.username}@gnuweeb.org</span> |
| 151 | + </div> |
| 152 | + <ChevronsUpDown class="ml-auto size-4" /> |
| 153 | + </Sidebar.MenuButton> |
109 | 154 | {/snippet}
|
110 |
| - </Sidebar.MenuButton> |
111 |
| - </Sidebar.MenuItem> |
112 |
| - </Sidebar.Menu> |
113 |
| - </Sidebar.GroupContent> |
114 |
| - </Sidebar.Group> |
115 |
| - </Sidebar.Content> |
116 |
| - <Sidebar.Footer> |
117 |
| - <Sidebar.Menu> |
118 |
| - <Button |
119 |
| - variant="destructive" |
120 |
| - onclick={handleLogout} |
121 |
| - class="flex w-full items-center justify-start" |
122 |
| - > |
123 |
| - <LogOut /> |
124 |
| - <span>Logout</span> |
125 |
| - </Button> |
126 |
| - </Sidebar.Menu> |
127 |
| - </Sidebar.Footer> |
128 |
| -</Sidebar.Root> |
| 155 | + </DropdownMenu.Trigger> |
| 156 | + <DropdownMenu.Content |
| 157 | + class="w-[var(--bits-dropdown-menu-anchor-width)] min-w-56 rounded-lg" |
| 158 | + side={sidebar.isMobile ? "bottom" : "right"} |
| 159 | + align="end" |
| 160 | + sideOffset={4} |
| 161 | + > |
| 162 | + <DropdownMenu.Label class="p-0 font-normal"> |
| 163 | + <div class="flex items-center gap-2 px-1 py-1.5 text-left text-sm"> |
| 164 | + <Avatar.Root class="h-8 w-8 rounded-lg"> |
| 165 | + <Avatar.Image src={auth.user?.photo} alt="{auth.user?.username}@gnuweeb.org" /> |
| 166 | + <Avatar.Fallback class="rounded-lg text-xs"> |
| 167 | + {#if !Boolean(auth.user?.photo)} |
| 168 | + {getShortName()} |
| 169 | + {:else} |
| 170 | + <Loading class="size-3 text-black" /> |
| 171 | + {/if} |
| 172 | + </Avatar.Fallback> |
| 173 | + </Avatar.Root> |
| 174 | + <div class="grid flex-1 text-left text-sm leading-tight"> |
| 175 | + <span class="truncate font-semibold">{auth.user?.full_name}</span> |
| 176 | + <span class="truncate text-xs">{auth.user?.username}@gnuweeb.org</span> |
| 177 | + </div> |
| 178 | + </div> |
| 179 | + </DropdownMenu.Label> |
| 180 | + <DropdownMenu.Separator /> |
| 181 | + <DropdownMenu.Item |
| 182 | + onclick={() => (showModalConfirmation = true)} |
| 183 | + class="text-destructive hover:!text-destructive" |
| 184 | + > |
| 185 | + <LogOut /> |
| 186 | + Log out |
| 187 | + </DropdownMenu.Item> |
| 188 | + </DropdownMenu.Content> |
| 189 | + </DropdownMenu.Root> |
| 190 | + </Sidebar.MenuItem> |
| 191 | + </Sidebar.Menu> |
| 192 | + </Sidebar.Footer> |
| 193 | + </Sidebar.Root> |
| 194 | + <Dialog.Content class="sm:max-w-[425px]"> |
| 195 | + <Dialog.Header> |
| 196 | + <Dialog.Title>Logout Confirmation</Dialog.Title> |
| 197 | + <Dialog.Description>Confirm logout from your profile.</Dialog.Description> |
| 198 | + </Dialog.Header> |
| 199 | + <p class="text-sm font-medium"> |
| 200 | + You are about to logout from your profile, click logout button below to proceed. |
| 201 | + </p> |
| 202 | + <Dialog.Footer> |
| 203 | + <Button type="submit" variant="destructive" onclick={handleLogout}>Logout</Button> |
| 204 | + </Dialog.Footer> |
| 205 | + </Dialog.Content> |
| 206 | +</Dialog.Root> |
0 commit comments