import { Button, ButtonGroup, Chip, Divider } from '@material-ui/core'; import React, { Component, DragEvent, useEffect, useState } from 'react'; import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp'; import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowUp'; import { $composition, Nugget as NuggetType, decreaseNuggetScore, increaseNuggetScore } from '../lib/prompt'; import "./Nugget.css"; import "./PromptItem.css" import { $sourceItem, cancelDrop, completeDrop, isPromptItemDropTarget, startDrag, startHoverOver } from '../store/prompt-dnd'; import { PromptItemProps } from './PromptItem'; import { useStore } from '@nanostores/react'; export interface NuggetProps extends PromptItemProps { nugget: NuggetType, isTopLevel?: boolean, } export default function Nugget(props: NuggetProps) { const { nugget, onDragStart, onDragOver, onDragEnd, onDrop, onMouseEnter, onMouseLeave, isTopLevel, } = props; const scoreDisp = nugget.score > 0 ? "+" + nugget.score : nugget.score; const sourceItem = useStore($sourceItem); const composition = useStore($composition) const thisId = `prompt-item-${nugget.id}` const className = isTopLevel ? 'nugget toplevel prompt-item' : 'nugget child prompt-item'; const handleOnDragStart = () => { onDragStart ? onDragStart(nugget) : null; } const handleOnMouseEnter = () => { if (!sourceItem) { return; } startHoverOver(nugget); } const handleOnMouseLeave = () => { onMouseLeave ? onMouseLeave(nugget) : null; } const handleOnDragOver = ($e: DragEvent) => { if (!sourceItem) return; // extract the prompt item's ID: const targetId = $e.currentTarget.getAttribute("data-promptitem-id"); if (sourceItem.id == targetId) return; console.log("current target id: %s", targetId); const promptItem = composition.find(i => (targetId === i.id)); if (!promptItem) { console.warn("Could not find promptitem with ID %s", targetId); console.log(composition.map(c => c.id)); return; } startHoverOver(promptItem); } const handleOnDragEnd = () => { completeDrop(); } return (
{nugget.item.name || nugget.item.prompt} {scoreDisp}
); }