SuperFiles.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import * as React from "react";
  2. import { CommandBar, ContextualMenuItemType, ICommandBarItemProps, Link, ScrollablePane, Sticky } from "@fluentui/react";
  3. import nlsHPCC from "src/nlsHPCC";
  4. import { useFluentGrid } from "../hooks/grid";
  5. import { useFile } from "../hooks/file";
  6. import { ShortVerticalDivider } from "./Common";
  7. import { selector } from "./DojoGrid";
  8. const defaultUIState = {
  9. hasSelection: false
  10. };
  11. interface SuperFilesProps {
  12. cluster: string;
  13. logicalFile: string;
  14. }
  15. export const SuperFiles: React.FunctionComponent<SuperFilesProps> = ({
  16. cluster,
  17. logicalFile
  18. }) => {
  19. const [file, , , refreshData] = useFile(cluster, logicalFile);
  20. const [uiState, setUIState] = React.useState({ ...defaultUIState });
  21. const [data, setData] = React.useState<any[]>([]);
  22. // Grid ---
  23. const [Grid, selection, copyButtons] = useFluentGrid({
  24. data,
  25. primaryID: "Name",
  26. sort: [{ attribute: "Name", "descending": false }],
  27. filename: "superFiles",
  28. columns: {
  29. col1: selector({
  30. width: 27,
  31. selectorType: "checkbox"
  32. }),
  33. Name: {
  34. label: nlsHPCC.Name,
  35. sortable: true,
  36. formatter: function (name, row) {
  37. return <Link href={`#/files/${cluster}/${name}`}>{name}</Link>;
  38. }
  39. },
  40. }
  41. });
  42. // Command Bar ---
  43. const buttons = React.useMemo((): ICommandBarItemProps[] => [
  44. {
  45. key: "refresh", text: nlsHPCC.Refresh, iconProps: { iconName: "Refresh" },
  46. onClick: () => refreshData()
  47. },
  48. { key: "divider_1", itemType: ContextualMenuItemType.Divider, onRender: () => <ShortVerticalDivider /> },
  49. {
  50. key: "open", text: nlsHPCC.Open, disabled: !uiState.hasSelection, iconProps: { iconName: "WindowEdit" },
  51. onClick: () => {
  52. if (selection.length === 1) {
  53. window.location.href = `#/files/${cluster}/${selection[0].Name}`;
  54. } else {
  55. for (let i = selection.length - 1; i >= 0; --i) {
  56. window.open(`#/files/${cluster}/${selection[i].Name}`, "_blank");
  57. }
  58. }
  59. }
  60. },
  61. ], [cluster, refreshData, selection, uiState.hasSelection]);
  62. // Selection ---
  63. React.useEffect(() => {
  64. const state = { ...defaultUIState };
  65. if (selection.length) {
  66. state.hasSelection = true;
  67. }
  68. setUIState(state);
  69. }, [selection]);
  70. React.useEffect(() => {
  71. if (file?.Superfiles?.DFULogicalFile) {
  72. setData(file?.Superfiles?.DFULogicalFile);
  73. }
  74. }, [file]);
  75. return <ScrollablePane>
  76. <Sticky>
  77. <CommandBar items={buttons} farItems={copyButtons} />
  78. </Sticky>
  79. <Grid />
  80. </ScrollablePane>;
  81. };