Skip to content

Commit

Permalink
refactor: 重构头像组件
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanEichDisciple committed Aug 27, 2024
1 parent 783be91 commit 146ad8d
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 97 deletions.
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 2 additions & 45 deletions src/lib/components/Nav.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@
import { page } from '$app/stores';
import { base } from '$app/paths';
import * as Menubar from '$lib/components/ui/menubar';
import * as Avatar from '$lib/components/ui/avatar';
import { Button } from '$lib/components/ui/button';
import * as DropdownMenu from '$lib/components/ui/dropdown-menu';
import LifeBuoy from 'lucide-svelte/icons/life-buoy';
import LogOut from 'lucide-svelte/icons/log-out';
import Settings from 'lucide-svelte/icons/settings';
import User from 'lucide-svelte/icons/user';
import NavAvatar from './NavAvatar.svelte';
$: activeUrl = $page.url.pathname;
</script>
Expand All @@ -35,42 +29,5 @@
</Menubar.Menu>
</Menubar.Root>

<DropdownMenu.Root>
{#if $page.data.user}
<DropdownMenu.Trigger>
<Avatar.Root>
<Avatar.Image src={$page.data.user.photo} alt="用户头像" />
<Avatar.Fallback>CN</Avatar.Fallback>
</Avatar.Root>
</DropdownMenu.Trigger>
{:else}
<Button variant="link" href="{base}/login">登录</Button>
{/if}
<DropdownMenu.Content class="w-56">
{#if $page.data.user}
<DropdownMenu.Label>我的账户</DropdownMenu.Label>
<DropdownMenu.Separator />
<DropdownMenu.Group>
<DropdownMenu.Item>
<User class="mr-2 h-4 w-4" />
<span>Profile</span>
</DropdownMenu.Item>
<DropdownMenu.Item href="{base}/settings">
<Settings class="mr-2 h-4 w-4" />
<span>设置</span>
</DropdownMenu.Item>
</DropdownMenu.Group>
<DropdownMenu.Separator />
<DropdownMenu.Item>
<LifeBuoy class="mr-2 h-4 w-4" />
<span>支持</span>
</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Item>
<LogOut class="mr-2 h-4 w-4" />
<slot name="logout" />
</DropdownMenu.Item>
{/if}
</DropdownMenu.Content>
</DropdownMenu.Root>
<NavAvatar></NavAvatar>
</nav>
69 changes: 69 additions & 0 deletions src/lib/components/NavAvatar.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<script>
import { page } from '$app/stores';
import { base } from '$app/paths';
import { goto } from '$app/navigation';
import * as Avatar from '$lib/components/ui/avatar';
import * as DropdownMenu from '$lib/components/ui/dropdown-menu';
import { Button } from '$lib/components/ui/button/index.js';
import LifeBuoy from 'lucide-svelte/icons/life-buoy';
import LogOut from 'lucide-svelte/icons/log-out';
import Settings from 'lucide-svelte/icons/settings';
import User from 'lucide-svelte/icons/user';
const user = $page.data?.user;
const src = user?.photo || `${base}/defavatar.png`;
async function handleLogout() {
const response = await fetch(`${base}/logout?/logout`, {
method: 'POST',
body: Object.create({}),
headers: {
'x-sveltekit-action': 'true'
}
});
if (response.ok) {
goto(`${base}/login`);
} else {
// 退出失败,你可以在这里处理错误
}
}
</script>
<DropdownMenu.Root>
{#if user}
<DropdownMenu.Trigger>
<Avatar.Root>
<Avatar.Image {src} alt="用户头像" />
<Avatar.Fallback>未登录</Avatar.Fallback>
</Avatar.Root>
</DropdownMenu.Trigger>
{:else}
<Button variant="link" href="{base}/login">登录</Button>
{/if}
<DropdownMenu.Content class="w-56">
{#if user}
<DropdownMenu.Label>我的账户</DropdownMenu.Label>
<DropdownMenu.Separator />
<DropdownMenu.Group>
<DropdownMenu.Item class="cursor-pointer" href="{base}/user">
<User class="mr-2 h-4 w-4" />
<span>控制台</span>
</DropdownMenu.Item>
<DropdownMenu.Item class="cursor-pointer" href="{base}/settings">
<Settings class="mr-2 h-4 w-4" />
<span>个人中心</span>
</DropdownMenu.Item>
</DropdownMenu.Group>
<DropdownMenu.Separator />
<DropdownMenu.Item class="cursor-pointer">
<LifeBuoy class="mr-2 h-4 w-4" />
<span>支持</span>
</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Item class="cursor-pointer">
<LogOut class="mr-2 h-4 w-4" />
<button on:click={handleLogout}>退出</button>
</DropdownMenu.Item>
{/if}
</DropdownMenu.Content>
</DropdownMenu.Root>
6 changes: 6 additions & 0 deletions src/lib/components/ui/separator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Root from "./separator.svelte";
export {
Root,
//
Root as Separator,
};
19 changes: 19 additions & 0 deletions src/lib/components/ui/separator/separator.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script>
import { Separator as SeparatorPrimitive } from "bits-ui";
import { cn } from "$lib/utils.js";
let className = undefined;
export let orientation = "horizontal";
export let decorative = undefined;
export { className as class };
</script>

<SeparatorPrimitive.Root
class={cn(
"bg-border shrink-0",
orientation === "horizontal" ? "h-[1px] w-full" : "min-h-full w-[1px]",
className
)}
{orientation}
{decorative}
{...$$restProps}
/>
20 changes: 0 additions & 20 deletions src/routes/(admin)/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
<script>
import { goto } from '$app/navigation';
import { navigating } from '$app/stores';
import { base } from '$app/paths';
import Nav from '$lib/components/Nav.svelte';
import PreloadingIndicator from '$lib/components/PreloadingIndicator.svelte';
async function handleLogout() {
const response = await fetch('/logout?/logout', {
method: 'POST',
body: Object.create({}),
headers: {
'x-sveltekit-action': 'true'
}
});
if (response.ok) {
goto(`${base}/login`);
} else {
// 退出失败,你可以在这里处理错误
}
}
</script>

{#if $navigating}
Expand All @@ -30,9 +13,6 @@
<img src="https://www.gov.cn/images/gtrs_logo_lt.png" class="mr-3 h-6 sm:h-9" alt="Svelte Logo" />
<span class="self-center whitespace-nowrap text-xl font-semibold dark:text-white">医保</span>
</svelte:fragment>
<svelte:fragment slot="logout">
<button on:click={handleLogout}>退出</button>
</svelte:fragment>
</Nav>

<main>
Expand Down
6 changes: 3 additions & 3 deletions src/routes/(admin)/user/data-table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import DataTableActions from './data-table-actions.svelte';
import DataTableCheckbox from './data-table-checkbox.svelte';
/** @type {User}*/
/** @type {Array<import('./types').User>}*/
export let userList = [];
/** @type {boolean} */
export let isNextPage = false;
Expand Down Expand Up @@ -233,7 +233,7 @@
{Object.keys($selectedDataIds).length} of{' '}
{$rows.length} row(s) selected.
</div>
<Button variant="outline" size="sm" on:click={() => ($pageIndex = $pageIndex - 1)} disabled={!$hasPreviousPage}>Previous</Button>
<Button variant="outline" size="sm" disabled={!$hasNextPage} on:click={() => ($pageIndex = $pageIndex + 1)}>Next</Button>
<Button variant="outline" size="sm" on:click={() => ($pageIndex = $pageIndex - 1)} disabled={!$hasPreviousPage}>上一页</Button>
<Button variant="outline" size="sm" disabled={!$hasNextPage} on:click={() => ($pageIndex = $pageIndex + 1)}>下一页</Button>
</div>
</div>
28 changes: 28 additions & 0 deletions src/routes/(admin)/user/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

/**
* Represents a user with various properties.
*/
export interface User {
id: number | string;
email: string | null;
provider: string;
socialId?: string | null;
firstName: string | null;
lastName: string | null;
photo?: FileType | null;
role?: Role | null;
status?: Status;
createdAt: Date;
updatedAt: Date;
deletedAt: Date;
}

export interface FileType {
id: string;
path: string;
}

export interface Role {
id: number | string;
name?: string;
}
31 changes: 2 additions & 29 deletions src/routes/index/avatar.svelte
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
<script>
import { page } from '$app/stores';
import { base } from '$app/paths';
import * as Avatar from '$lib/components/ui/avatar';
import * as Popover from '$lib/components/ui/popover';
import { Button } from '$lib/components/ui/button/index.js';
import { cn } from '$lib/utils.js';
import LogOut from 'lucide-svelte/icons/log-out';
import NavAvatar from '$lib/components/NavAvatar.svelte';
let className = undefined;
const user = $page.data?.user;
const src = user?.photo || `${base}/defavatar.png`;
</script>

<div class={cn('h-12 w-12', className)} {...$$restProps}>
<Popover.Root>
<Popover.Trigger>
<Avatar.Root>
<Avatar.Image {src} alt="用户头像" />
<Avatar.Fallback>未登录</Avatar.Fallback>
</Avatar.Root>
</Popover.Trigger>
<Popover.Content>
{#if user}
<form method="post" action="{base}/logout?/logout">
<div class="flex cursor-pointer flex-col items-center">
<Button variant="outline" size="icon" type="submit">
<LogOut class="mr-2 h-4 w-4" />
</Button>
</div>
</form>
{:else}
<Button variant="link" href="{base}/login" class="w-full">登录</Button>
{/if}
</Popover.Content>
</Popover.Root>
<NavAvatar></NavAvatar>
</div>

0 comments on commit 146ad8d

Please sign in to comment.