Resources.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import * as React from "react";
  2. import { CommandBar, ContextualMenuItemType, ICommandBarItemProps } from "@fluentui/react";
  3. import { useConst } from "@fluentui/react-hooks";
  4. import { AlphaNumSortMemory } from "src/Memory";
  5. import * as Observable from "dojo/store/Observable";
  6. import nlsHPCC from "src/nlsHPCC";
  7. import { useWorkunitResources } from "../hooks/Workunit";
  8. import { HolyGrail } from "../layouts/HolyGrail";
  9. import { ShortVerticalDivider } from "./Common";
  10. import { DojoGrid, selector } from "./DojoGrid";
  11. const defaultUIState = {
  12. hasSelection: false
  13. };
  14. interface ResourcesProps {
  15. wuid: string;
  16. }
  17. export const Resources: React.FunctionComponent<ResourcesProps> = ({
  18. wuid
  19. }) => {
  20. const [grid, setGrid] = React.useState<any>(undefined);
  21. const [selection, setSelection] = React.useState([]);
  22. const [uiState, setUIState] = React.useState({ ...defaultUIState });
  23. const [resources] = useWorkunitResources(wuid);
  24. // Command Bar ---
  25. const buttons: ICommandBarItemProps[] = [
  26. {
  27. key: "refresh", text: nlsHPCC.Refresh, iconProps: { iconName: "Refresh" },
  28. onClick: () => refreshTable()
  29. },
  30. { key: "divider_1", itemType: ContextualMenuItemType.Divider, onRender: () => <ShortVerticalDivider /> },
  31. {
  32. key: "open", text: nlsHPCC.Open, disabled: !uiState.hasSelection, iconProps: { iconName: "WindowEdit" },
  33. onClick: () => {
  34. if (selection.length === 1) {
  35. window.location.href = `#/iframe?src=${encodeURIComponent(`WsWorkunits/${selection[0].URL}`)}`;
  36. } else {
  37. for (let i = selection.length - 1; i >= 0; --i) {
  38. window.open(`#/iframe?src=${encodeURIComponent(`WsWorkunits/${selection[i].URL}`)}`, "_blank");
  39. }
  40. }
  41. }
  42. },
  43. {
  44. key: "content", text: nlsHPCC.Content, disabled: !uiState.hasSelection, iconProps: { iconName: "WindowEdit" },
  45. onClick: () => {
  46. if (selection.length === 1) {
  47. window.location.href = `#/text?src=${encodeURIComponent(`WsWorkunits/${selection[0].URL}`)}`;
  48. } else {
  49. for (let i = selection.length - 1; i >= 0; --i) {
  50. window.open(`#/text?src=${encodeURIComponent(`WsWorkunits/${selection[i].URL}`)}`, "_blank");
  51. }
  52. }
  53. }
  54. },
  55. ];
  56. const rightButtons: ICommandBarItemProps[] = [
  57. {
  58. key: "copy", text: nlsHPCC.CopyWUIDs, disabled: true, iconOnly: true, iconProps: { iconName: "Copy" },
  59. onClick: () => {
  60. // TODO: HPCC-25473
  61. }
  62. },
  63. {
  64. key: "download", text: nlsHPCC.DownloadToCSV, disabled: true, iconOnly: true, iconProps: { iconName: "Download" },
  65. onClick: () => {
  66. // TODO: HPCC-25473
  67. }
  68. }
  69. ];
  70. // Grid ---
  71. const gridStore = useConst(new Observable(new AlphaNumSortMemory("DisplayPath", { Name: true, Value: true })));
  72. const gridQuery = useConst({});
  73. const gridSort = useConst([{ attribute: "Wuid", "descending": true }]);
  74. const gridColumns = useConst({
  75. col1: selector({
  76. width: 27,
  77. selectorType: "checkbox"
  78. }),
  79. DisplayPath: {
  80. label: nlsHPCC.Name, sortable: true,
  81. formatter: function (url, row) {
  82. return `<a href='#/iframe?src=${encodeURIComponent(`WsWorkunits/${row.URL}`)}' class='dgrid-row-url'>${url}</a>`;
  83. }
  84. }
  85. });
  86. const refreshTable = (clearSelection = false) => {
  87. grid?.set("query", gridQuery);
  88. if (clearSelection) {
  89. grid?.clearSelection();
  90. }
  91. };
  92. // Selection ---
  93. React.useEffect(() => {
  94. const state = { ...defaultUIState };
  95. for (let i = 0; i < selection.length; ++i) {
  96. state.hasSelection = true;
  97. break;
  98. }
  99. setUIState(state);
  100. }, [selection]);
  101. React.useEffect(() => {
  102. gridStore.setData(resources.filter((row, idx) => idx > 0).map(row => {
  103. return {
  104. URL: row,
  105. DisplayPath: row.substring(`res/${wuid}/`.length)
  106. };
  107. }));
  108. refreshTable();
  109. // eslint-disable-next-line react-hooks/exhaustive-deps
  110. }, [gridStore, resources]);
  111. return <HolyGrail
  112. header={<CommandBar items={buttons} overflowButtonProps={{}} farItems={rightButtons} />}
  113. main={
  114. <DojoGrid store={gridStore} query={gridQuery} sort={gridSort} columns={gridColumns} setGrid={setGrid} setSelection={setSelection} />
  115. }
  116. />;
  117. };