import { useEffect, useRef, useState } from 'preact/hooks'; import { pulseElement } from './pulseElement'; export interface ResultTextPropsBase { text: string } export interface ResultTextPropsFull extends ResultTextPropsBase { mappingId: number textId: number updated: Date } export interface ResultTextPropsLimited extends ResultTextPropsBase { mappingId?: null textId?: null updated?: null } export type ResultTextProps = ResultTextPropsFull|ResultTextPropsLimited export function reconstituteResultText(button: HTMLButtonElement, partial: Partial = {}): ResultTextProps { const text = button.innerText if (typeof partial.mappingId ?? button.dataset["mappingId"] === "undefined") { return {text} } else { return { text, mappingId: partial.mappingId ?? parseInt(button.dataset["mappingId"]!), textId: partial.textId ?? parseInt(button.dataset["textId"]!), updated: partial.updated ?? new Date(parseInt(button.dataset["updated"]!)) } } } export function ResultText({text, mappingId, textId, updated}: ResultTextProps) { const ref = useRef(null) const [lastText, setLastText] = useState(text) useEffect(() => { if (text !== lastText) { setLastText(text) if (ref.current) { pulseElement(ref.current) } } }, [ref, text, lastText, setLastText]); return }