budibase/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowItemHeader.svelte

261 lines
6.7 KiB
Svelte
Raw Normal View History

<script>
import { automationStore, selectedAutomation } from "builderStore"
import { Icon, Body, StatusLight, AbsTooltip } from "@budibase/bbui"
import { externalActions } from "./ExternalActions"
Undo/Redo for Design and Automate sections + automations refactor (#9714) * Add full undo/redo support for screens * Add loading states to disable spamming undo/redo * Add keyboard shortcuts for undo and redo * Fix modals not closing in design section when escape is pressed * Remove log * Add smart metadata saving to undo/redo * Add error handling to undo/redo * Add active state to hoverable icons * Fix screen deletion * Always attempt to get latest doc version before deleting in case rev has changed * Move undo listener top level, hide controls when on certain tabs, and improve selection state * Add tooltips to undo/redo control * Update automation section nav to match other sections * Fix automation list padding * Fix some styles in create automation modal * Improve automation section styles and add undo/redo * Update styles in add action modal * Fix button size when creating admin user * Fix styles in add automation step modal * Fix issue selecting disabled automation steps * Reset automation history store when changing app * Reduce spammy unnecessary API calls when editing cron trigger * WIP automation refactor * Rewrite most automation state * Rewrite most of the rest of automation state * Finish refactor of automation state * Fix selection state when selecting new doc after history recreates it * Prune nullish or empty block inputs from automations and avoid sending API requests when no changes have been made * Fix animation issues with automations * Sort automations and refetch list when adding or deleting * Fix formatting * Add back in ability to swap between values and bindings for block inputs * Lint * Format * Fix potential issue in design section when selected screen is unset * Fix automation arrow directions everywhere, tidy up logic and fix crash when using invalid looping * Lint * Fix more cases of automation errors * Fix implicity any TS error * Respect _id specified when creating automations * Fix crash in history store when reverting a change on a doc whose ID has changed * Lint * Ensure cloneDeep helper doesn't crash when a nullish value is passed in * Remove deprecated frontend automation test --------- Co-authored-by: Rory Powell <rory.codes@gmail.com>
2023-02-23 14:55:18 +01:00
import { createEventDispatcher } from "svelte"
2023-10-24 16:17:22 +02:00
import { Features } from "constants/backend/automations"
export let block
Undo/Redo for Design and Automate sections + automations refactor (#9714) * Add full undo/redo support for screens * Add loading states to disable spamming undo/redo * Add keyboard shortcuts for undo and redo * Fix modals not closing in design section when escape is pressed * Remove log * Add smart metadata saving to undo/redo * Add error handling to undo/redo * Add active state to hoverable icons * Fix screen deletion * Always attempt to get latest doc version before deleting in case rev has changed * Move undo listener top level, hide controls when on certain tabs, and improve selection state * Add tooltips to undo/redo control * Update automation section nav to match other sections * Fix automation list padding * Fix some styles in create automation modal * Improve automation section styles and add undo/redo * Update styles in add action modal * Fix button size when creating admin user * Fix styles in add automation step modal * Fix issue selecting disabled automation steps * Reset automation history store when changing app * Reduce spammy unnecessary API calls when editing cron trigger * WIP automation refactor * Rewrite most automation state * Rewrite most of the rest of automation state * Finish refactor of automation state * Fix selection state when selecting new doc after history recreates it * Prune nullish or empty block inputs from automations and avoid sending API requests when no changes have been made * Fix animation issues with automations * Sort automations and refetch list when adding or deleting * Fix formatting * Add back in ability to swap between values and bindings for block inputs * Lint * Format * Fix potential issue in design section when selected screen is unset * Fix automation arrow directions everywhere, tidy up logic and fix crash when using invalid looping * Lint * Fix more cases of automation errors * Fix implicity any TS error * Respect _id specified when creating automations * Fix crash in history store when reverting a change on a doc whose ID has changed * Lint * Ensure cloneDeep helper doesn't crash when a nullish value is passed in * Remove deprecated frontend automation test --------- Co-authored-by: Rory Powell <rory.codes@gmail.com>
2023-02-23 14:55:18 +01:00
export let open
export let showTestStatus = false
export let testResult
export let isTrigger
export let idx
2023-10-24 16:17:22 +02:00
export let addLooping
export let deleteStep
let validRegex = /^[A-Za-z0-9_\s]+$/
let typing = false
Undo/Redo for Design and Automate sections + automations refactor (#9714) * Add full undo/redo support for screens * Add loading states to disable spamming undo/redo * Add keyboard shortcuts for undo and redo * Fix modals not closing in design section when escape is pressed * Remove log * Add smart metadata saving to undo/redo * Add error handling to undo/redo * Add active state to hoverable icons * Fix screen deletion * Always attempt to get latest doc version before deleting in case rev has changed * Move undo listener top level, hide controls when on certain tabs, and improve selection state * Add tooltips to undo/redo control * Update automation section nav to match other sections * Fix automation list padding * Fix some styles in create automation modal * Improve automation section styles and add undo/redo * Update styles in add action modal * Fix button size when creating admin user * Fix styles in add automation step modal * Fix issue selecting disabled automation steps * Reset automation history store when changing app * Reduce spammy unnecessary API calls when editing cron trigger * WIP automation refactor * Rewrite most automation state * Rewrite most of the rest of automation state * Finish refactor of automation state * Fix selection state when selecting new doc after history recreates it * Prune nullish or empty block inputs from automations and avoid sending API requests when no changes have been made * Fix animation issues with automations * Sort automations and refetch list when adding or deleting * Fix formatting * Add back in ability to swap between values and bindings for block inputs * Lint * Format * Fix potential issue in design section when selected screen is unset * Fix automation arrow directions everywhere, tidy up logic and fix crash when using invalid looping * Lint * Fix more cases of automation errors * Fix implicity any TS error * Respect _id specified when creating automations * Fix crash in history store when reverting a change on a doc whose ID has changed * Lint * Ensure cloneDeep helper doesn't crash when a nullish value is passed in * Remove deprecated frontend automation test --------- Co-authored-by: Rory Powell <rory.codes@gmail.com>
2023-02-23 14:55:18 +01:00
const dispatch = createEventDispatcher()
2023-10-24 16:17:22 +02:00
$: stepNames = $selectedAutomation.definition.stepNames
2023-10-26 12:27:20 +02:00
$: automationName = stepNames?.[block.id] || block?.name || ""
2023-10-24 16:17:22 +02:00
$: automationNameError = getAutomationNameError(automationName)
$: status = updateStatus(testResult, isTrigger)
$: isTrigger = isTrigger || block.type === "TRIGGER"
$: {
if (!testResult) {
Undo/Redo for Design and Automate sections + automations refactor (#9714) * Add full undo/redo support for screens * Add loading states to disable spamming undo/redo * Add keyboard shortcuts for undo and redo * Fix modals not closing in design section when escape is pressed * Remove log * Add smart metadata saving to undo/redo * Add error handling to undo/redo * Add active state to hoverable icons * Fix screen deletion * Always attempt to get latest doc version before deleting in case rev has changed * Move undo listener top level, hide controls when on certain tabs, and improve selection state * Add tooltips to undo/redo control * Update automation section nav to match other sections * Fix automation list padding * Fix some styles in create automation modal * Improve automation section styles and add undo/redo * Update styles in add action modal * Fix button size when creating admin user * Fix styles in add automation step modal * Fix issue selecting disabled automation steps * Reset automation history store when changing app * Reduce spammy unnecessary API calls when editing cron trigger * WIP automation refactor * Rewrite most automation state * Rewrite most of the rest of automation state * Finish refactor of automation state * Fix selection state when selecting new doc after history recreates it * Prune nullish or empty block inputs from automations and avoid sending API requests when no changes have been made * Fix animation issues with automations * Sort automations and refetch list when adding or deleting * Fix formatting * Add back in ability to swap between values and bindings for block inputs * Lint * Format * Fix potential issue in design section when selected screen is unset * Fix automation arrow directions everywhere, tidy up logic and fix crash when using invalid looping * Lint * Fix more cases of automation errors * Fix implicity any TS error * Respect _id specified when creating automations * Fix crash in history store when reverting a change on a doc whose ID has changed * Lint * Ensure cloneDeep helper doesn't crash when a nullish value is passed in * Remove deprecated frontend automation test --------- Co-authored-by: Rory Powell <rory.codes@gmail.com>
2023-02-23 14:55:18 +01:00
testResult = $automationStore.testResults?.steps?.filter(step =>
block.id ? step.id === block.id : step.stepId === block.stepId
)?.[0]
}
}
2023-10-26 17:05:15 +02:00
$: loopBlock = $selectedAutomation.definition.steps.find(
2023-10-26 12:27:20 +02:00
x => x.blockToLoop === block?.id
2023-10-24 16:17:22 +02:00
)
async function onSelect(block) {
await automationStore.update(state => {
state.selectedBlock = block
return state
})
}
function updateStatus(results, isTrigger) {
if (!results) {
return {}
}
const lcStatus = results.outputs?.status?.toLowerCase()
if (lcStatus === "stopped" || lcStatus === "stopped_error") {
return { yellow: true, message: "Stopped" }
} else if (results.outputs?.success || isTrigger) {
return { positive: true, message: "Success" }
} else {
return { negative: true, message: "Error" }
}
}
const getAutomationNameError = name => {
2023-10-26 17:05:15 +02:00
if (name !== block.name && name?.length > 0) {
let invalidRoleName = !validRegex.test(name)
if (invalidRoleName) {
return "Please enter a role name consisting of only alphanumeric symbols and underscores"
2023-10-24 16:17:22 +02:00
}
2023-10-26 12:27:20 +02:00
return null
}
2023-10-26 12:27:20 +02:00
}
const startTyping = async () => {
typing = true
}
2023-10-24 16:17:22 +02:00
const saveName = async () => {
2023-10-26 17:05:15 +02:00
if (automationNameError || block.name === automationName) {
2023-10-24 16:17:22 +02:00
return
}
if (automationName.length === 0) {
2023-10-26 17:05:15 +02:00
await automationStore.actions.deleteAutomationName(block.id)
2023-10-24 16:17:22 +02:00
} else {
2023-10-26 17:05:15 +02:00
await automationStore.actions.saveAutomationName(block.id, automationName)
2023-10-24 16:17:22 +02:00
}
}
</script>
<div
class:typing={typing && !automationNameError}
class:typing-error={automationNameError}
class="blockSection"
>
<div class="splitHeader">
<div class="center-items">
{#if externalActions[block.stepId]}
<img
alt={externalActions[block.stepId].name}
width="28px"
height="28px"
src={externalActions[block.stepId].icon}
/>
{:else}
<svg
width="28px"
height="28px"
class="spectrum-Icon"
style="color:var(--spectrum-global-color-gray-700);"
focusable="false"
>
<use xlink:href="#spectrum-icon-18-{block.icon}" />
</svg>
{/if}
<div class="iconAlign">
{#if isTrigger}
<Body size="XS"><b>Trigger</b></Body>
{:else}
2023-10-26 17:05:15 +02:00
<div style="margin-left: 2px;">
<Body size="XS"><b>Step {idx}</b></Body>
</div>
{/if}
<input
placeholder="Enter some text"
name="name"
2023-10-25 15:09:59 +02:00
autocomplete="off"
2023-10-24 16:17:22 +02:00
value={automationName}
on:input={e => {
2023-10-26 17:05:15 +02:00
automationName = e.target.value.trim()
}}
on:click={startTyping}
2023-10-24 16:17:22 +02:00
on:blur={async () => {
typing = false
if (automationNameError) {
2023-10-24 16:17:22 +02:00
automationName = stepNames[block.id]
} else {
await saveName()
}
}}
/>
</div>
</div>
<div class="blockTitle">
{#if showTestStatus && testResult}
<div style="float: right;">
<StatusLight
positive={status?.positive}
yellow={status?.yellow}
negative={status?.negative}
><Body size="XS">{status?.message}</Body></StatusLight
>
</div>
{/if}
<div
class="context-actions"
class:hide-context-actions={typing}
on:click={() => {
onSelect(block)
}}
>
{#if !showTestStatus}
2023-10-24 16:17:22 +02:00
{#if !isTrigger && !loopBlock && (block?.features?.[Features.LOOPING] || !block.features)}
<AbsTooltip type="info" text="Add looping">
<Icon on:click={addLooping} hoverable name="RotateCW" />
</AbsTooltip>
{/if}
<AbsTooltip type="negative" text="Delete step">
2023-10-24 16:17:22 +02:00
<Icon on:click={deleteStep} hoverable name="DeleteOutline" />
</AbsTooltip>
{/if}
<Icon
on:click={() => dispatch("toggle")}
hoverable
name={open ? "ChevronUp" : "ChevronDown"}
/>
</div>
{#if automationNameError}
<div class="error-container">
<AbsTooltip type="negative" text={automationNameError}>
<div class="error-icon">
<Icon size="S" name="Alert" />
</div>
</AbsTooltip>
</div>
{/if}
</div>
</div>
</div>
<style>
.context-actions {
display: flex;
gap: var(--spacing-l);
margin-left: 10px;
margin-bottom: var(--spacing-xs);
}
.center-items {
display: flex;
align-items: center;
}
.splitHeader {
display: flex;
justify-content: space-between;
align-items: center;
}
.iconAlign {
padding: 0 0 0 var(--spacing-m);
display: inline-block;
}
.blockSection {
padding: var(--spacing-xl);
}
.blockTitle {
display: flex;
align-items: center;
}
.hide-context-actions {
display: none;
}
input {
font-family: var(--font-sans);
color: var(--ink);
background-color: transparent;
2023-10-26 17:05:15 +02:00
border: 1px solid transparent;
font-size: var(--spectrum-alias-font-size-default);
width: 260px;
box-sizing: border-box;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
input:focus {
outline: none;
}
/* Hide arrows for number fields */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
.typing {
2023-10-26 17:05:15 +02:00
border: 1px solid var(--spectrum-global-color-static-blue-500);
border-radius: 4px 4px 4px 4px;
}
.typing-error {
2023-10-26 17:05:15 +02:00
border: 1px solid var(--spectrum-global-color-static-red-500);
border-radius: 4px 4px 4px 4px;
}
.error-icon :global(.spectrum-Icon) {
fill: var(--spectrum-global-color-red-400);
}
.error-container {
padding-top: var(--spacing-xl);
}
</style>