2024-02-21 17:19:29 +01:00
|
|
|
import { Button, ButtonGroup, Chip, Divider } from '@material-ui/core';
|
2024-02-29 18:11:13 +01:00
|
|
|
import React, { Component, DragEvent, useEffect, useState } from 'react';
|
2024-02-21 17:19:29 +01:00
|
|
|
import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp';
|
|
|
|
import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowUp';
|
2024-02-29 18:11:13 +01:00
|
|
|
import { $composition, Nugget as NuggetType, decreaseNuggetScore, increaseNuggetScore } from '../lib/prompt';
|
2024-02-21 17:19:29 +01:00
|
|
|
import "./Nugget.css";
|
2024-02-29 18:11:13 +01:00
|
|
|
import "./PromptItem.css"
|
|
|
|
import { $sourceItem, cancelDrop, completeDrop, isPromptItemDropTarget, startDrag, startHoverOver } from '../store/prompt-dnd';
|
|
|
|
import { PromptItemProps } from './PromptItem';
|
|
|
|
import { useStore } from '@nanostores/react';
|
2024-02-21 17:19:29 +01:00
|
|
|
|
2024-02-29 18:11:13 +01:00
|
|
|
export interface NuggetProps extends PromptItemProps {
|
|
|
|
nugget: NuggetType,
|
2024-02-29 20:10:04 +01:00
|
|
|
isTopLevel?: boolean,
|
2024-02-26 17:56:46 +01:00
|
|
|
}
|
|
|
|
|
2024-02-29 18:11:13 +01:00
|
|
|
export default function Nugget(props: NuggetProps) {
|
|
|
|
|
|
|
|
const { nugget,
|
|
|
|
onDragStart,
|
|
|
|
onDragOver,
|
|
|
|
onDragEnd,
|
|
|
|
onDrop,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
2024-02-29 20:10:04 +01:00
|
|
|
isTopLevel,
|
2024-02-29 18:11:13 +01:00
|
|
|
} = props;
|
2024-02-26 17:56:46 +01:00
|
|
|
|
|
|
|
const scoreDisp = nugget.score > 0 ? "+" + nugget.score : nugget.score;
|
2024-02-29 18:11:13 +01:00
|
|
|
|
|
|
|
const sourceItem = useStore($sourceItem);
|
|
|
|
const composition = useStore($composition)
|
|
|
|
const thisId = `prompt-item-${nugget.id}`
|
|
|
|
|
2024-02-29 20:10:04 +01:00
|
|
|
const className = isTopLevel ? 'nugget toplevel prompt-item' : 'nugget child prompt-item';
|
|
|
|
|
2024-02-29 18:11:13 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-02-21 17:19:29 +01:00
|
|
|
return (
|
2024-02-29 18:11:13 +01:00
|
|
|
<div
|
2024-02-29 20:10:04 +01:00
|
|
|
className={className}
|
2024-02-29 18:11:13 +01:00
|
|
|
id={thisId}
|
|
|
|
draggable
|
|
|
|
onDragStart={handleOnDragStart}
|
|
|
|
onDragOver={handleOnDragOver}
|
|
|
|
onDragEnd={handleOnDragEnd}
|
|
|
|
onMouseEnter={handleOnMouseEnter}
|
|
|
|
onMouseOut={handleOnMouseLeave}
|
|
|
|
data-promptitem-id={nugget.id}
|
|
|
|
>
|
2024-02-26 17:56:46 +01:00
|
|
|
<span className='text'>{nugget.item.name || nugget.item.prompt}</span>
|
2024-02-21 17:19:29 +01:00
|
|
|
<Divider orientation="vertical" variant="middle" flexItem />
|
2024-02-26 17:56:46 +01:00
|
|
|
<span className='score'>{scoreDisp}</span>
|
2024-02-21 17:19:29 +01:00
|
|
|
<span className='buttons'>
|
2024-02-29 18:11:13 +01:00
|
|
|
<ButtonGroup size="small" orientation='vertical'>
|
|
|
|
<Button onClick={() => increaseNuggetScore(nugget.id)} className='incScore' aria-label="incScore">
|
|
|
|
<KeyboardArrowUpIcon />
|
|
|
|
</Button>
|
|
|
|
<Button onClick={() => decreaseNuggetScore(nugget.id)} className='decScore' aria-label='decScore'>
|
|
|
|
<KeyboardArrowDownIcon />
|
|
|
|
</Button>
|
|
|
|
</ButtonGroup>
|
2024-02-21 17:19:29 +01:00
|
|
|
</span>
|
|
|
|
</div>
|
2024-02-28 17:36:40 +01:00
|
|
|
);
|
2024-02-21 17:19:29 +01:00
|
|
|
}
|