ChapterToc.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { Key } from "antd/lib/table/interface";
  2. import { useState, useEffect } from "react";
  3. import { get } from "../../request";
  4. import { IChapterToc, IChapterTocListResponse } from "../api/Corpus";
  5. import { ListNodeData } from "./EditableTree";
  6. import TocTree from "./TocTree";
  7. import { Skeleton } from "antd";
  8. interface IWidget {
  9. book?: number;
  10. para?: number;
  11. onSelect?: (selectedKeys: Key[]) => void;
  12. onData?: (data: IChapterToc[]) => void;
  13. }
  14. const ChapterTocWidget = ({ book, para, onSelect, onData }: IWidget) => {
  15. const [tocList, setTocList] = useState<ListNodeData[]>([]);
  16. const [loading, setLoading] = useState(true);
  17. useEffect(() => {
  18. let url = `/v2/chapter?view=toc&book=${book}&para=${para}`;
  19. setLoading(true);
  20. console.info("api request", url);
  21. get<IChapterTocListResponse>(url)
  22. .then((json) => {
  23. console.info("api response", json);
  24. onData && onData(json.data.rows);
  25. const toc = json.data.rows.map((item, id) => {
  26. return {
  27. key: `${item.book}-${item.paragraph}`,
  28. title: item.text,
  29. level: parseInt(item.level),
  30. };
  31. });
  32. setTocList(toc);
  33. if (json.data.rows.length > 0) {
  34. let path: string[] = [];
  35. for (let index = json.data.rows.length - 1; index >= 0; index--) {
  36. const element = json.data.rows[index];
  37. if (element.book === book && para && element.paragraph <= para) {
  38. path.push(`${element.book}-${element.paragraph}`);
  39. break;
  40. }
  41. }
  42. }
  43. })
  44. .finally(() => setLoading(false));
  45. }, [book, para]);
  46. return loading ? (
  47. <Skeleton active />
  48. ) : (
  49. <TocTree
  50. treeData={tocList}
  51. onSelect={(selectedKeys: Key[]) => {
  52. if (typeof onSelect !== "undefined") {
  53. onSelect && onSelect(selectedKeys);
  54. }
  55. }}
  56. />
  57. );
  58. };
  59. export default ChapterTocWidget;