"use client" import React, { useCallback, useMemo } 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 } from "@/lib/utils" import { LEARNING_STATES, LearningStateValue } from "@/lib/constants" import { linkOpenPopoverForIdAtom, linkShowCreateAtom } from "@/store/link" interface LinkItemProps { personalLink: PersonalLink disabled?: boolean isEditing: boolean setEditId: (id: string | null) => void isDragging: boolean isFocused: boolean setFocusedId: (id: string | null) => void registerRef: (id: string, ref: HTMLLIElement | null) => void } export const LinkItem: React.FC = ({ isEditing, setEditId, personalLink, disabled = false, isDragging, isFocused, setFocusedId, registerRef }) => { const [openPopoverForId, setOpenPopoverForId] = useAtom(linkOpenPopoverForIdAtom) const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ id: personalLink.id, disabled }) const style = useMemo( () => ({ transform: CSS.Transform.toString(transform), transition, pointerEvents: isDragging ? "none" : "auto" }), [transform, transition, isDragging] ) const refCallback = useCallback( (node: HTMLLIElement | null) => { setNodeRef(node) registerRef(personalLink.id, node) }, [setNodeRef, registerRef, personalLink.id] ) const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault() setEditId(personalLink.id) } }, [setEditId, personalLink.id] ) const handleSuccess = useCallback(() => setEditId(null), [setEditId]) const handleOnClose = useCallback(() => setEditId(null), [setEditId]) const handleRowDoubleClick = useCallback(() => setEditId(personalLink.id), [setEditId, personalLink.id]) const selectedLearningState = useMemo( () => LEARNING_STATES.find(ls => ls.value === personalLink.learningState), [personalLink.learningState] ) const handleLearningStateSelect = useCallback( (value: string) => { const learningState = value as LearningStateValue personalLink.learningState = personalLink.learningState === learningState ? undefined : learningState setOpenPopoverForId(null) }, [personalLink, setOpenPopoverForId] ) if (isEditing) { return {}} /> } return (
  • setFocusedId(personalLink.id)} onBlur={() => setFocusedId(null)} onKeyDown={handleKeyDown} className={cn("relative flex h-14 cursor-default items-center outline-none xl:h-11", { "bg-muted-foreground/10": isFocused, "hover:bg-muted/50": !isFocused })} onDoubleClick={handleRowDoubleClick} >
    setOpenPopoverForId(open ? personalLink.id : null)} > e.preventDefault()} > {personalLink.icon && ( {personalLink.title} )}

    {personalLink.title}

    {personalLink.url && (
    )}
    {personalLink.topic && {personalLink.topic.prettyName}}
  • ) }