2024-07-29 07:45:59 -07:00
|
|
|
import { useState } from 'react';
|
2024-08-16 10:30:14 +02:00
|
|
|
import { MenuItem } from '@headlessui/react';
|
2024-07-29 07:45:59 -07:00
|
|
|
import { BookmarkFilledIcon, BookmarkIcon } from '@radix-ui/react-icons';
|
|
|
|
|
import type { FC } from 'react';
|
|
|
|
|
import { Spinner } from '~/components/svg';
|
|
|
|
|
import { cn } from '~/utils';
|
|
|
|
|
|
|
|
|
|
type MenuItemProps = {
|
2024-08-08 18:16:17 +02:00
|
|
|
tag: string | React.ReactNode;
|
2024-07-29 07:45:59 -07:00
|
|
|
selected: boolean;
|
|
|
|
|
count?: number;
|
2024-08-16 10:30:14 +02:00
|
|
|
handleSubmit: (tag?: string) => Promise<void>;
|
2024-07-29 07:45:59 -07:00
|
|
|
icon?: React.ReactNode;
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-16 10:30:14 +02:00
|
|
|
const BookmarkItem: FC<MenuItemProps> = ({ tag, selected, handleSubmit, icon, ...rest }) => {
|
2024-07-29 07:45:59 -07:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
const clickHandler = async () => {
|
2024-08-16 10:30:14 +02:00
|
|
|
if (tag === 'New Bookmark') {
|
|
|
|
|
await handleSubmit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 07:45:59 -07:00
|
|
|
setIsLoading(true);
|
2024-08-08 18:16:17 +02:00
|
|
|
await handleSubmit(tag as string);
|
2024-07-29 07:45:59 -07:00
|
|
|
setIsLoading(false);
|
|
|
|
|
};
|
2024-08-08 18:16:17 +02:00
|
|
|
|
|
|
|
|
const breakWordStyle: React.CSSProperties = {
|
|
|
|
|
wordBreak: 'break-word',
|
|
|
|
|
overflowWrap: 'anywhere',
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-08 21:25:10 -04:00
|
|
|
const renderIcon = () => {
|
|
|
|
|
if (icon) {
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
if (isLoading) {
|
|
|
|
|
return <Spinner className="size-4" />;
|
|
|
|
|
}
|
|
|
|
|
if (selected) {
|
|
|
|
|
return <BookmarkFilledIcon className="size-4" />;
|
|
|
|
|
}
|
|
|
|
|
return <BookmarkIcon className="size-4" />;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-29 07:45:59 -07:00
|
|
|
return (
|
2024-08-16 10:30:14 +02:00
|
|
|
<MenuItem
|
|
|
|
|
aria-label={tag as string}
|
2024-07-29 07:45:59 -07:00
|
|
|
className={cn(
|
2024-08-16 10:30:14 +02:00
|
|
|
'group flex w-full gap-2 rounded-lg p-2.5 text-sm text-text-primary transition-colors duration-200',
|
|
|
|
|
selected ? 'bg-surface-hover' : 'data-[focus]:bg-surface-hover',
|
2024-07-29 07:45:59 -07:00
|
|
|
)}
|
|
|
|
|
{...rest}
|
2024-08-16 10:30:14 +02:00
|
|
|
as="button"
|
2024-07-29 07:45:59 -07:00
|
|
|
onClick={clickHandler}
|
|
|
|
|
>
|
|
|
|
|
<div className="flex grow items-center justify-between gap-2">
|
|
|
|
|
<div className="flex items-center gap-2">
|
2024-08-08 21:25:10 -04:00
|
|
|
{renderIcon()}
|
2024-08-08 18:16:17 +02:00
|
|
|
<div style={breakWordStyle}>{tag}</div>
|
2024-07-29 07:45:59 -07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-08-16 10:30:14 +02:00
|
|
|
</MenuItem>
|
2024-07-29 07:45:59 -07:00
|
|
|
);
|
|
|
|
|
};
|
2024-08-08 21:25:10 -04:00
|
|
|
|
2024-07-29 07:45:59 -07:00
|
|
|
export default BookmarkItem;
|