|
1 |
| -import { Button, Card, CardBody, CardFooter, Progress } from '@nextui-org/react' |
| 1 | +import { |
| 2 | + Button, |
| 3 | + Card, |
| 4 | + CardBody, |
| 5 | + CardFooter, |
| 6 | + Dropdown, |
| 7 | + DropdownItem, |
| 8 | + DropdownMenu, |
| 9 | + DropdownTrigger, |
| 10 | + Progress |
| 11 | +} from '@nextui-org/react' |
2 | 12 | import { calcPercent, calcTraffic } from '@renderer/utils/calc'
|
3 |
| -import React from 'react' |
4 |
| -import { IoMdRefresh } from 'react-icons/io' |
| 13 | +import { IoMdMore, IoMdRefresh } from 'react-icons/io' |
| 14 | +import dayjs from 'dayjs' |
| 15 | +import React, { Key, useMemo } from 'react' |
5 | 16 |
|
6 | 17 | interface Props {
|
7 | 18 | info: IProfileItem
|
8 | 19 | isCurrent: boolean
|
| 20 | + removeProfileItem: (id: string) => Promise<void> |
| 21 | + mutateProfileConfig: () => void |
9 | 22 | onClick: () => Promise<void>
|
10 | 23 | }
|
11 | 24 |
|
| 25 | +interface MenuItem { |
| 26 | + key: string |
| 27 | + label: string |
| 28 | + showDivider: boolean |
| 29 | + color: 'default' | 'danger' |
| 30 | + className: string |
| 31 | +} |
12 | 32 | const ProfileItem: React.FC<Props> = (props) => {
|
13 |
| - const { info, onClick, isCurrent } = props |
| 33 | + const { info, removeProfileItem, mutateProfileConfig, onClick, isCurrent } = props |
14 | 34 | const extra = info?.extra
|
15 | 35 | const usage = (extra?.upload ?? 0) + (extra?.download ?? 0)
|
16 | 36 | const total = extra?.total ?? 0
|
17 | 37 |
|
| 38 | + const menuItems: MenuItem[] = useMemo(() => { |
| 39 | + const list = [ |
| 40 | + { |
| 41 | + key: 'edit', |
| 42 | + label: '编辑文件', |
| 43 | + showDivider: true, |
| 44 | + color: 'default', |
| 45 | + className: '' |
| 46 | + } as MenuItem, |
| 47 | + { |
| 48 | + key: 'delete', |
| 49 | + label: '删除', |
| 50 | + showDivider: false, |
| 51 | + color: 'danger', |
| 52 | + className: 'text-danger' |
| 53 | + } as MenuItem |
| 54 | + ] |
| 55 | + if (info.home) { |
| 56 | + list.unshift({ |
| 57 | + key: 'home', |
| 58 | + label: '主页', |
| 59 | + showDivider: false, |
| 60 | + color: 'default', |
| 61 | + className: '' |
| 62 | + } as MenuItem) |
| 63 | + } |
| 64 | + return list |
| 65 | + }, [info]) |
| 66 | + |
| 67 | + const onMenuAction = (key: Key): void => { |
| 68 | + switch (key) { |
| 69 | + case 'edit': |
| 70 | + break |
| 71 | + case 'delete': { |
| 72 | + removeProfileItem(info.id) |
| 73 | + mutateProfileConfig() |
| 74 | + break |
| 75 | + } |
| 76 | + |
| 77 | + case 'home': { |
| 78 | + open(info.home) |
| 79 | + break |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
18 | 84 | return (
|
19 | 85 | <Card fullWidth isPressable onPress={onClick} className={isCurrent ? 'bg-primary' : ''}>
|
20 |
| - <CardBody> |
| 86 | + <CardBody className="pb-1"> |
21 | 87 | <div className="flex justify-between h-[32px]">
|
22 | 88 | <h3 className="select-none text-ellipsis whitespace-nowrap overflow-hidden text-md font-bold leading-[32px]">
|
23 | 89 | {info?.name}
|
24 | 90 | </h3>
|
25 |
| - <Button isIconOnly size="sm" variant="light" color="default"> |
26 |
| - <IoMdRefresh color="default" className="text-[24px]" /> |
27 |
| - </Button> |
| 91 | + <div className="flex"> |
| 92 | + <Button isIconOnly size="sm" variant="light" color="default"> |
| 93 | + <IoMdRefresh color="default" className="text-[24px]" /> |
| 94 | + </Button> |
| 95 | + <Dropdown> |
| 96 | + <DropdownTrigger> |
| 97 | + <Button isIconOnly size="sm" variant="light" color="default"> |
| 98 | + <IoMdMore color="default" className="text-[24px]" /> |
| 99 | + </Button> |
| 100 | + </DropdownTrigger> |
| 101 | + <DropdownMenu onAction={onMenuAction}> |
| 102 | + {menuItems.map((item) => ( |
| 103 | + <DropdownItem |
| 104 | + showDivider={item.showDivider} |
| 105 | + key={item.key} |
| 106 | + color={item.color} |
| 107 | + className={item.className} |
| 108 | + > |
| 109 | + {item.label} |
| 110 | + </DropdownItem> |
| 111 | + ))} |
| 112 | + </DropdownMenu> |
| 113 | + </Dropdown> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + <div className="mt-2 flex justify-between"> |
| 117 | + <small>{extra ? `${calcTraffic(usage)}/${calcTraffic(total)}` : undefined}</small> |
| 118 | + <small>{dayjs(info.updated).fromNow()}</small> |
28 | 119 | </div>
|
29 | 120 | </CardBody>
|
30 |
| - <CardFooter className="pt-1"> |
31 |
| - <Progress |
32 |
| - classNames={{ indicator: 'bg-foreground', label: 'select-none' }} |
33 |
| - label={extra ? `${calcTraffic(usage)}/${calcTraffic(total)}` : undefined} |
34 |
| - value={calcPercent(extra?.upload, extra?.download, extra?.total)} |
35 |
| - className="max-w-md" |
36 |
| - /> |
| 121 | + <CardFooter className="pt-0"> |
| 122 | + {extra && ( |
| 123 | + <Progress |
| 124 | + className="w-full" |
| 125 | + classNames={{ indicator: 'bg-foreground', label: 'select-none' }} |
| 126 | + value={calcPercent(extra?.upload, extra?.download, extra?.total)} |
| 127 | + /> |
| 128 | + )} |
37 | 129 | </CardFooter>
|
38 | 130 | </Card>
|
39 | 131 | )
|
|
0 commit comments