Files
windmill/frontend/src/lib/components/apps/editor/inlineScriptsPanel/utils.ts
T
2023-02-03 02:11:06 +01:00

44 lines
1.2 KiB
TypeScript

import type { Runnable } from "../../inputType"
import type { GridItem } from "../../types"
import type { AppComponent } from "../Component.svelte";
export interface AppScriptsList {
inline: { name: string; id: string }[],
imported: { name: string; id: string }[]
}
export function getAppScripts(grid: GridItem[]) {
return grid.reduce((acc, gridComponent) => {
const component: AppComponent = gridComponent.data
const componentInput = component.componentInput
if (component.type === 'tablecomponent') {
component.actionButtons.forEach((actionButton) => {
if (actionButton.componentInput?.type !== 'runnable') { return }
processRunnable(
actionButton.componentInput.runnable,
actionButton.id,
acc
)
})
}
if (componentInput?.type === 'runnable') {
processRunnable(
componentInput.runnable,
gridComponent.id,
acc
)
}
return acc
}, { inline: [], imported: [] } as AppScriptsList)
}
function processRunnable(runnable: Runnable, id: string, list: AppScriptsList) {
if (runnable?.type === undefined) { return }
const type: keyof AppScriptsList = runnable.type === 'runnableByPath' ? 'imported' : 'inline'
list[type].push({
name: runnable[runnable.type === 'runnableByPath' ? 'path' : 'name'],
id
})
}