invoke-prompt-editor-demo/src/components/Nugget.tsx

98 lines
3.4 KiB
TypeScript
Raw Normal View History

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,
isTopLevel?: boolean,
}
2024-02-29 18:11:13 +01:00
export default function Nugget(props: NuggetProps) {
const { nugget,
onDragStart,
onDragOver,
onDragEnd,
onDrop,
onMouseEnter,
onMouseLeave,
isTopLevel,
2024-02-29 18:11:13 +01:00
} = props;
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}`
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
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}
>
<span className='text'>{nugget.item.name || nugget.item.prompt}</span>
2024-02-21 17:19:29 +01:00
<Divider orientation="vertical" variant="middle" flexItem />
<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-21 17:19:29 +01:00
}