import { useEffect, useState } from "react"; import { get } from "../../request"; import type { ArticleType } from "./Article"; import React from "react"; import NavigateButton from "./NavigateButton"; import type { ITocPathNode } from "../corpus/TocPath"; interface INavButton { title: string; subtitle: string; id: string; } interface INavResponse { ok: boolean; message: string; data: { prev?: INavButton; next?: INavButton; }; } interface IWidget { type?: ArticleType; articleId?: string; path?: ITocPathNode[]; onChange?: ( event: React.MouseEvent, id: string ) => void; onPathChange?: (key: string) => void; } const NavigateWidget = ({ type, articleId, path, onChange, onPathChange, }: IWidget) => { const [prev, setPrev] = useState(); const [next, setNext] = useState(); useEffect(() => { if (type && articleId) { get(`/v2/article-nav?type=${type}&id=${articleId}`).then( (json) => { if (json.ok) { setPrev(json.data.prev); setNext(json.data.next); } } ); } }, [articleId, type]); return ( ) => { if (typeof onChange !== "undefined" && prev) { onChange(event, prev.id); } }} onNext={(event: React.MouseEvent) => { if (typeof onChange !== "undefined" && next) { onChange(event, next.id); } }} onPathChange={(key: string) => { if (typeof onPathChange !== "undefined") { onPathChange(key); } }} /> ); }; export default NavigateWidget;