freshyo/apps/admin-web/src/components/SnippetMenu.tsx
2026-10-28 00:11:50 +05:30

156 lines
4 KiB
TypeScript

import { useState, type FC } from 'react'
import { p as P, BottomDialog } from 'web-components'
import { SuccessToast } from '@/services/toaster'
import type { VendorSnippet } from '@/types/vendor-snippets'
import { Eye, Pencil, Link, Trash2, MoreVertical } from 'lucide-react'
import type { CSSProperties } from 'react'
export interface SnippetMenuOption {
title: string
icon: 'eye' | 'edit' | 'link' | 'trash'
onPress: () => void
disabled?: boolean
}
export interface SnippetMenuProps {
snippet: VendorSnippet
onEdit: (snippet: VendorSnippet) => void
onDelete: (id: number) => void
onViewOrders: (snippetCode: string) => void
triggerStyle?: any
iconSize?: number
iconColor?: string
}
const iconMap = {
eye: Eye,
edit: Pencil,
link: Link,
trash: Trash2,
}
export const SnippetMenu: FC<SnippetMenuProps> = ({
snippet,
onEdit,
onDelete,
onViewOrders,
triggerStyle = 'p-2 rounded-full bg-gray-50',
iconSize = 16,
iconColor = 'var(--color-gray-500)',
}) => {
const [isOpen, setIsOpen] = useState(false)
const handleOpenMenu = () => {
setIsOpen(true)
}
const handleCloseMenu = () => {
setIsOpen(false)
}
const handleViewOrders = () => {
setIsOpen(false)
onViewOrders(snippet.snippetCode)
}
const handleEdit = () => {
setIsOpen(false)
onEdit(snippet)
}
const handleDelete = () => {
setIsOpen(false)
const confirmed = window.confirm(
'Delete Snippet? Are you sure you want to delete this vendor snippet?'
)
if (confirmed) {
onDelete(snippet.id)
}
}
const handleCopyUrl = async () => {
setIsOpen(false)
if (!snippet.accessUrl) {
window.alert('Error: No URL available to copy')
return
}
await navigator.clipboard.writeText(snippet.accessUrl)
SuccessToast('URL copied to clipboard')
}
const options: SnippetMenuOption[] = [
{
title: 'View Orders',
icon: 'eye',
onPress: handleViewOrders,
},
{
title: 'Edit',
icon: 'edit',
onPress: handleEdit,
},
{
title: 'Copy URL',
icon: 'link',
onPress: handleCopyUrl,
},
{
title: 'Delete',
icon: 'trash',
onPress: handleDelete,
},
]
const handleOptionPress = (option: SnippetMenuOption) => {
if (option.disabled) return
option.onPress()
}
const triggerClassName = typeof triggerStyle === 'string' ? triggerStyle : 'p-2 rounded-full bg-gray-50'
const triggerStyleObj: CSSProperties | undefined = typeof triggerStyle === 'object' ? triggerStyle : undefined
return (
<>
{/* Menu Trigger */}
<button
type="button"
onClick={handleOpenMenu}
className={triggerClassName}
style={triggerStyleObj}
aria-label="Snippet options"
>
<MoreVertical size={iconSize} color={iconColor} />
</button>
{/* Menu Dialog */}
<BottomDialog open={isOpen} onClose={handleCloseMenu}>
<div className="p-6">
<P className="text-lg font-bold text-gray-800 mb-6 block">
Snippet Options
</P>
{options.map((option, index) => {
const Icon = iconMap[option.icon]
return (
<button
key={`${snippet.id}-${index}`}
type="button"
onClick={() => handleOptionPress(option)}
disabled={option.disabled}
className={`flex flex-row items-center p-4 bg-gray-50 rounded-lg w-full text-left ${index < options.length - 1 ? 'mb-3' : ''} ${option.disabled ? 'opacity-50' : ''}`}
>
<Icon
size={20}
color={option.disabled ? 'var(--color-gray-400)' : 'var(--color-gray-500)'}
/>
<P className={`font-medium ml-3 ${option.disabled ? 'text-gray-400' : 'text-gray-800'}`}>
{option.title}
</P>
</button>
)
})}
</div>
</BottomDialog>
</>
)
}