import React, { useCallback, useMemo } from "react" import Link from "next/link" import { cn } from "@/lib/utils" import { useColumnStyles } from "../hooks/use-column-styles" import { ListOfTopics, Topic } from "@/lib/schema" import { Column } from "@/components/custom/column" 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 { useAtom } from "jotai" import { topicOpenPopoverForIdAtom } from "../list" import { LEARNING_STATES, LearningStateValue } from "@/lib/constants" import { useAccount } from "@/lib/providers/jazz-provider" interface TopicItemProps { topic: Topic learningState: LearningStateValue isActive: boolean } export const TopicItem = React.forwardRef(({ topic, learningState, isActive }, ref) => { const columnStyles = useColumnStyles() const [openPopoverForId, setOpenPopoverForId] = useAtom(topicOpenPopoverForIdAtom) const { me } = useAccount({ root: { topicsWantToLearn: [], topicsLearning: [], topicsLearned: [] } }) let p: { index: number topic?: Topic | null learningState: LearningStateValue } | null = null const wantToLearnIndex = me?.root.topicsWantToLearn.findIndex(t => t?.id === topic.id) ?? -1 if (wantToLearnIndex !== -1) { p = { index: wantToLearnIndex, topic: me?.root.topicsWantToLearn[wantToLearnIndex], learningState: "wantToLearn" } } const learningIndex = me?.root.topicsLearning.findIndex(t => t?.id === topic.id) ?? -1 if (learningIndex !== -1) { p = { index: learningIndex, topic: me?.root.topicsLearning[learningIndex], learningState: "learning" } } const learnedIndex = me?.root.topicsLearned.findIndex(t => t?.id === topic.id) ?? -1 if (learnedIndex !== -1) { p = { index: learnedIndex, topic: me?.root.topicsLearned[learnedIndex], learningState: "learned" } } const selectedLearningState = useMemo(() => LEARNING_STATES.find(ls => ls.value === learningState), [learningState]) const handleLearningStateSelect = useCallback( (value: string) => { const newLearningState = value as LearningStateValue const topicLists: Record = { wantToLearn: me?.root.topicsWantToLearn, learning: me?.root.topicsLearning, learned: me?.root.topicsLearned } const removeFromList = (state: LearningStateValue, index: number) => { topicLists[state]?.splice(index, 1) } if (p) { if (newLearningState === p.learningState) { removeFromList(p.learningState, p.index) return } removeFromList(p.learningState, p.index) } topicLists[newLearningState]?.push(topic) setOpenPopoverForId(null) }, [setOpenPopoverForId, me?.root.topicsWantToLearn, me?.root.topicsLearning, me?.root.topicsLearned, p, topic] ) const handlePopoverTriggerClick = (e: React.MouseEvent) => { e.preventDefault() e.stopPropagation() setOpenPopoverForId(openPopoverForId === topic.id ? null : topic.id) } return (
{topic.prettyName} setOpenPopoverForId(open ? topic.id : null)} > e.stopPropagation()} onCloseAutoFocus={e => e.preventDefault()} >
) }) TopicItem.displayName = "TopicItem"