|
| 1 | +import { useEffect, useMemo } from "react"; |
| 2 | +import { |
| 3 | + useConfig, |
| 4 | + useEditorPanelConfig, |
| 5 | + useActionTrigger, |
| 6 | + useActionEffect, |
| 7 | + useVariable, |
| 8 | +} from "@sigmacomputing/plugin"; |
| 9 | + |
| 10 | +import type { ActualVariable } from "../types"; |
| 11 | +import { |
| 12 | + pluginContainerStyles, |
| 13 | + pluginHeaderStyles, |
| 14 | + pluginTitleStyles, |
| 15 | + pluginContentStyles, |
| 16 | + pluginStatusItemStyles, |
| 17 | + pluginLabelStyles, |
| 18 | + pluginValueStyles, |
| 19 | + statusColors, |
| 20 | +} from "../styles/pluginStyles"; |
| 21 | + |
| 22 | +interface ForEachConfig_t { |
| 23 | + index: string; |
| 24 | + endIndex: string; |
| 25 | + isDisabled: string; |
| 26 | + doOneLoop: string; |
| 27 | + runNextLoop: string; |
| 28 | +} |
| 29 | + |
| 30 | +function ForEach() { |
| 31 | + useEditorPanelConfig([ |
| 32 | + { |
| 33 | + type: "variable", |
| 34 | + name: "index", |
| 35 | + label: "Current index we are looping over", |
| 36 | + allowedTypes: ["number"], |
| 37 | + }, |
| 38 | + { |
| 39 | + type: "variable", |
| 40 | + name: "endIndex", |
| 41 | + label: "Index to end at", |
| 42 | + allowedTypes: ["number"], |
| 43 | + }, |
| 44 | + { |
| 45 | + type: "variable", |
| 46 | + name: "isDisabled", |
| 47 | + label: "Is Disabled?", |
| 48 | + allowedTypes: ["boolean"], |
| 49 | + }, |
| 50 | + { type: "action-effect", name: "doOneLoop", label: "Do One Loop" }, |
| 51 | + { type: "action-trigger", name: "runNextLoop", label: "Run next loop" }, |
| 52 | + ]); |
| 53 | + |
| 54 | + const config: ForEachConfig_t = useConfig() as ForEachConfig_t; |
| 55 | + |
| 56 | + // Extract the variables from config panel |
| 57 | + const [indexVar] = useVariable(config.index); |
| 58 | + const [endIndexVar] = useVariable(config.endIndex); |
| 59 | + const [isDisabledVar] = useVariable(config.isDisabled); |
| 60 | + |
| 61 | + |
| 62 | + const index = useMemo(() => { |
| 63 | + const value = indexVar?.defaultValue as ActualVariable | undefined; |
| 64 | + return value?.type === "number" && value.value !== null |
| 65 | + ? value.value |
| 66 | + : 0; |
| 67 | + }, [indexVar]); |
| 68 | + |
| 69 | + const endIndex = useMemo(() => { |
| 70 | + const value = endIndexVar?.defaultValue as ActualVariable | undefined; |
| 71 | + return value?.type === "number" && value.value !== null |
| 72 | + ? value.value |
| 73 | + : 0; |
| 74 | + }, [endIndexVar]); |
| 75 | + |
| 76 | + const isDisabled = useMemo(() => { |
| 77 | + const value = isDisabledVar?.defaultValue as ActualVariable | undefined; |
| 78 | + return value?.type === "boolean" && value.value !== null |
| 79 | + ? value.value |
| 80 | + : true; |
| 81 | + }, [isDisabledVar]); |
| 82 | + |
| 83 | + // Set up action trigger (sending data out) |
| 84 | + const runNextLoop = useActionTrigger(config.runNextLoop); |
| 85 | + // On receiving effect (input to plugin), do the trigger (output from plugin) |
| 86 | + useActionEffect(config.doOneLoop, () => { |
| 87 | + runNextLoop(); |
| 88 | + }); |
| 89 | + |
| 90 | + // Set up the interval |
| 91 | + useEffect(() => { |
| 92 | + if (isDisabled) { |
| 93 | + return; |
| 94 | + } |
| 95 | + if (index >= endIndex) { |
| 96 | + return; |
| 97 | + } |
| 98 | + runNextLoop(); |
| 99 | + }, [isDisabled, runNextLoop, index, endIndex]); |
| 100 | + |
| 101 | + return ( |
| 102 | + <div style={pluginContainerStyles}> |
| 103 | + <div style={pluginHeaderStyles}> |
| 104 | + <h2 style={pluginTitleStyles}>ForEach Plugin</h2> |
| 105 | + </div> |
| 106 | + <div style={pluginContentStyles}> |
| 107 | + <div style={pluginStatusItemStyles}> |
| 108 | + <span style={pluginLabelStyles}>Status:</span> |
| 109 | + <span |
| 110 | + style={{ |
| 111 | + ...pluginValueStyles, |
| 112 | + color: isDisabled ? statusColors.stopped : statusColors.running, |
| 113 | + }} |
| 114 | + > |
| 115 | + {isDisabled ? "Disabled" : "Running"} |
| 116 | + </span> |
| 117 | + </div> |
| 118 | + <div style={pluginStatusItemStyles}> |
| 119 | + <span style={pluginLabelStyles}>Current Index:</span> |
| 120 | + <span style={pluginValueStyles}>{index}</span> |
| 121 | + </div> |
| 122 | + <div style={pluginStatusItemStyles}> |
| 123 | + <span style={pluginLabelStyles}>End Index:</span> |
| 124 | + <span style={pluginValueStyles}>{endIndex}</span> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +export default ForEach; |
0 commit comments