2024-07-29 07:45:59 -07:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
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;
|
2024-08-08 21:25:10 -04:00
|
|
|
ctx: 'header' | 'nav';
|
2024-07-29 07:45:59 -07:00
|
|
|
count?: number;
|
|
|
|
|
handleSubmit: (tag: string) => Promise<void>;
|
|
|
|
|
icon?: React.ReactNode;
|
|
|
|
|
highlightSelected?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const BookmarkItem: FC<MenuItemProps> = ({
|
|
|
|
|
tag,
|
2024-08-08 21:25:10 -04:00
|
|
|
ctx,
|
2024-07-29 07:45:59 -07:00
|
|
|
selected,
|
|
|
|
|
count,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
icon,
|
|
|
|
|
highlightSelected,
|
|
|
|
|
...rest
|
|
|
|
|
}) => {
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
const clickHandler = async () => {
|
|
|
|
|
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" />;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ariaLabel =
|
|
|
|
|
ctx === 'header' ? `${selected ? 'Remove' : 'Add'} bookmark for ${tag}` : (tag as string);
|
|
|
|
|
|
2024-07-29 07:45:59 -07:00
|
|
|
return (
|
2024-08-08 21:25:10 -04:00
|
|
|
<button
|
|
|
|
|
aria-label={ariaLabel}
|
2024-07-29 07:45:59 -07:00
|
|
|
role="menuitem"
|
|
|
|
|
className={cn(
|
2024-08-08 21:25:10 -04:00
|
|
|
'group m-1.5 flex w-[225px] cursor-pointer gap-2 rounded bg-transparent px-2 py-2.5 !pr-3 text-sm !opacity-100 focus:ring-0 radix-disabled:pointer-events-none radix-disabled:opacity-50',
|
|
|
|
|
highlightSelected && selected ? 'bg-surface-secondary' : '',
|
|
|
|
|
ctx === 'header' ? 'hover:bg-header-hover' : 'hover:bg-surface-hover',
|
2024-07-29 07:45:59 -07:00
|
|
|
)}
|
|
|
|
|
tabIndex={-1}
|
|
|
|
|
{...rest}
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
|
|
{count !== undefined && (
|
|
|
|
|
<div className="flex items-center justify-end">
|
|
|
|
|
<span
|
2024-08-08 21:25:10 -04:00
|
|
|
className="ml-auto w-7 min-w-max whitespace-nowrap rounded-md bg-surface-secondary px-2.5 py-0.5 text-center text-xs font-medium leading-5 text-text-secondary"
|
2024-07-29 07:45:59 -07:00
|
|
|
aria-hidden="true"
|
|
|
|
|
>
|
|
|
|
|
{count}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2024-08-08 21:25:10 -04:00
|
|
|
</button>
|
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;
|