import * as React from "react" import Image from "next/image" import Link from "next/link" import { useAtom } from "jotai" import { useSortable } from "@dnd-kit/sortable" import { CSS } from "@dnd-kit/utilities" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { LaIcon } from "@/components/custom/la-icon" import { Popover, PopoverTrigger, PopoverContent } from "@/components/ui/popover" import { LearningStateSelectorContent } from "@/components/custom/learning-state-selector" import { PersonalLink } from "@/lib/schema/personal-link" import { LinkForm } from "./form/link-form" import { cn, ensureUrlProtocol } from "@/lib/utils" import { LEARNING_STATES, LearningStateValue } from "@/lib/constants" import { linkOpenPopoverForIdAtom } from "@/store/link" interface LinkItemProps extends React.HTMLAttributes { personalLink: PersonalLink disabled?: boolean editId: string | null isActive: boolean index: number onItemSelected?: (personalLink: PersonalLink) => void onFormClose?: () => void } export const LinkItem = React.forwardRef( ({ personalLink, disabled, editId, isActive, index, onItemSelected, onFormClose, ...props }, ref) => { const [openPopoverForId, setOpenPopoverForId] = useAtom(linkOpenPopoverForIdAtom) const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ id: personalLink.id, disabled }) const style = React.useMemo( () => ({ transform: CSS.Transform.toString(transform), transition }), [transform, transition] ) const selectedLearningState = React.useMemo( () => LEARNING_STATES.find(ls => ls.value === personalLink.learningState), [personalLink.learningState] ) const handleLearningStateSelect = React.useCallback( (value: string) => { const learningState = value as LearningStateValue personalLink.learningState = personalLink.learningState === learningState ? undefined : learningState setOpenPopoverForId(null) }, [personalLink, setOpenPopoverForId] ) const handleKeyDown = React.useCallback( (ev: React.KeyboardEvent) => { if (ev.key === "Enter") { ev.preventDefault() ev.stopPropagation() onItemSelected?.(personalLink) } }, [personalLink, onItemSelected] ) if (editId === personalLink.id) { return {}} /> } return (
{ setNodeRef(node) if (typeof ref === "function") { ref(node) } else if (ref) { ref.current = node } }} style={style as React.CSSProperties} {...props} {...attributes} {...listeners} tabIndex={0} onDoubleClick={() => onItemSelected?.(personalLink)} aria-disabled={disabled} aria-selected={isActive} data-disabled={disabled} data-active={isActive} className={cn( "w-full overflow-visible border-b-[0.5px] border-transparent outline-none", "data-[active='true']:bg-[var(--link-background-muted)] data-[keyboard-active='true']:focus-visible:shadow-[var(--link-shadow)_0px_0px_0px_1px_inset]" )} onKeyDown={handleKeyDown} >
setOpenPopoverForId(open ? personalLink.id : null)} >
{personalLink.icon && ( {personalLink.title} )}

{personalLink.title}

{personalLink.url && (
)}
{personalLink.topic && ( {personalLink.topic.prettyName} )}
) } ) LinkItem.displayName = "LinkItem"