1
0
Fork 0
mirror of synced 2024-09-17 09:49:11 +12:00

recommit work for trigger binding suggestions

This commit is contained in:
Peter Clement 2024-01-04 14:30:18 +00:00
parent eabe74a2c1
commit a6c09e25e6

View file

@ -51,7 +51,7 @@
export let testData export let testData
export let schemaProperties export let schemaProperties
export let isTestModal = false export let isTestModal = false
$: console.log(inputData)
let webhookModal let webhookModal
let drawer let drawer
let fillWidth = true let fillWidth = true
@ -101,7 +101,6 @@
} }
} }
} }
const onChange = Utils.sequential(async (e, key) => { const onChange = Utils.sequential(async (e, key) => {
// We need to cache the schema as part of the definition because it is // We need to cache the schema as part of the definition because it is
// used in the server to detect relationships. It would be far better to // used in the server to detect relationships. It would be far better to
@ -145,34 +144,89 @@
if (!block || !automation) { if (!block || !automation) {
return [] return []
} }
// Find previous steps to the selected one
let allSteps = [...automation.steps]
if (automation.trigger) { let allSteps = automation.trigger
allSteps = [automation.trigger, ...allSteps] ? [automation.trigger, ...automation.steps]
} : [...automation.steps]
let blockIdx = allSteps.findIndex(step => step.id === block.id) let blockIdx = allSteps.findIndex(step => step.id === block.id)
// Extract all outputs from all previous steps as available bindingsx§x
let bindings = [] let bindings = []
let loopBlockCount = 0 let loopBlockCount = 0
const addBinding = (name, value, icon, idx, isLoopBlock, bindingName) => {
const runtimeBinding = determineRuntimeBinding(name, idx, isLoopBlock)
const categoryName = determineCategoryName(idx, isLoopBlock, bindingName)
bindings.push(
createBindingObject(
name,
value,
icon,
idx,
loopBlockCount,
isLoopBlock,
runtimeBinding,
categoryName,
bindingName
)
)
}
const determineRuntimeBinding = (name, idx, isLoopBlock) => {
if (idx === 0 && automation.trigger?.event === "app:trigger") {
return `trigger.fields.${name}`
}
return isLoopBlock
? `loop.${name}`
: `steps.${idx - loopBlockCount}.${name}`
}
const determineCategoryName = (idx, isLoopBlock, bindingName) => {
if (idx === 0) return "Trigger outputs"
if (isLoopBlock) return "Loop Outputs"
return bindingName
? `${bindingName} outputs`
: `Step ${idx - loopBlockCount} outputs`
}
const createBindingObject = (
name,
value,
icon,
idx,
loopBlockCount,
isLoopBlock,
runtimeBinding,
categoryName,
bindingName
) => {
return {
readableBinding: bindingName
? `${bindingName}.${name}`
: runtimeBinding,
runtimeBinding,
type: value.type,
description: value.description,
icon,
category: categoryName,
display: {
type: value.type,
name,
rank: isLoopBlock ? idx + 1 : idx - loopBlockCount,
},
}
}
for (let idx = 0; idx < blockIdx; idx++) { for (let idx = 0; idx < blockIdx; idx++) {
let wasLoopBlock = allSteps[idx - 1]?.stepId === ActionStepID.LOOP let wasLoopBlock = allSteps[idx - 1]?.stepId === ActionStepID.LOOP
let isLoopBlock = let isLoopBlock =
allSteps[idx]?.stepId === ActionStepID.LOOP && allSteps[idx]?.stepId === ActionStepID.LOOP &&
allSteps.find(x => x.blockToLoop === block.id) allSteps.some(x => x.blockToLoop === block.id)
// If the previous block was a loop block, decrement the index so the following
// steps are in the correct order
if (wasLoopBlock) {
loopBlockCount++
continue
}
let schema = allSteps[idx]?.schema?.outputs?.properties ?? {} let schema = allSteps[idx]?.schema?.outputs?.properties ?? {}
let bindingName =
automation.stepNames?.[allSteps[idx - loopBlockCount].id]
// If its a Loop Block, we need to add this custom schema
if (isLoopBlock) { if (isLoopBlock) {
// Reset schema to only include 'currentItem' for loop blocks
schema = { schema = {
currentItem: { currentItem: {
type: "string", type: "string",
@ -180,75 +234,46 @@
}, },
} }
} }
const outputs = Object.entries(schema)
let bindingIcon = ""
let bindingRank = 0
if (idx === 0) {
bindingIcon = automation.trigger.icon
} else if (isLoopBlock) {
bindingIcon = "Reuse"
bindingRank = idx + 1
} else {
bindingIcon = allSteps[idx].icon
bindingRank = idx - loopBlockCount
}
let bindingName =
automation.stepNames?.[allSteps[idx - loopBlockCount].id]
bindings = bindings.concat(
outputs.map(([name, value]) => {
let runtimeName = isLoopBlock
? `loop.${name}`
: block.name.startsWith("JS")
? `steps[${idx - loopBlockCount}].${name}`
: `steps.${idx - loopBlockCount}.${name}`
const runtime = idx === 0 ? `trigger.${name}` : runtimeName
let categoryName if (idx === 0 && automation.trigger?.event === "app:trigger") {
if (idx === 0) { schema = Object.fromEntries(
categoryName = "Trigger outputs" Object.keys(automation.trigger.inputs.fields).map(key => [
} else if (isLoopBlock) { key,
categoryName = "Loop Outputs" { type: automation.trigger.inputs.fields[key] },
} else if (bindingName) { ])
categoryName = `${bindingName} outputs` )
} else {
categoryName = `Step ${idx - loopBlockCount} outputs`
} }
return { let icon =
readableBinding: bindingName ? `${bindingName}.${name}` : runtime, idx === 0
runtimeBinding: runtime, ? automation.trigger.icon
type: value.type, : isLoopBlock
description: value.description, ? "Reuse"
icon: bindingIcon, : allSteps[idx].icon
category: categoryName,
display: { // Continue if the previous block was a loop block to skip bindings from the block that the loop is attached to
type: value.type, if (wasLoopBlock) {
name: name, loopBlockCount++
rank: bindingRank, continue
},
} }
})
Object.entries(schema).forEach(([name, value]) =>
addBinding(name, value, icon, idx, isLoopBlock, bindingName)
) )
} }
// Environment bindings // Environment bindings
if ($licensing.environmentVariablesEnabled) { if ($licensing.environmentVariablesEnabled) {
bindings = bindings.concat( bindings = bindings.concat(
getEnvironmentBindings().map(binding => { getEnvironmentBindings().map(binding => ({
return {
...binding, ...binding,
display: { display: { ...binding.display, rank: 98 },
...binding.display, }))
rank: 98,
},
}
})
) )
} }
return bindings return bindings
} }
function lookForFilters(properties) { function lookForFilters(properties) {
if (!properties) { if (!properties) {
return [] return []