ChapterToc.tsx 2.0 KB

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