Skip to content

Commit 31d0f63

Browse files
committed
Modernize webhook wrappers, remove backbone dependency
1 parent c32097c commit 31d0f63

File tree

4 files changed

+49
-93
lines changed

4 files changed

+49
-93
lines changed
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
<script setup lang="ts">
2+
import { appendScriptStyle } from "@/utils/utils";
3+
import { loadWebhooks, pickWebhook } from "@/utils/webhooks";
24
import { onMounted, ref } from "vue";
35
4-
import Webhooks from "@/utils/webhooks";
5-
66
interface Props {
77
type: string;
88
toolId?: string;
9+
toolVersion?: string;
910
}
1011
1112
const props = withDefaults(defineProps<Props>(), {
12-
toolId: undefined,
13+
toolId: "",
14+
toolVersion: "",
1315
});
1416
15-
const webhook = ref(null);
17+
const container = ref<HTMLElement | null>(null);
18+
const webhookId = ref<string | null>(null);
19+
20+
onMounted(async () => {
21+
if (container.value) {
22+
container.value.setAttribute("tool_id", props.toolId);
23+
container.value.setAttribute("tool_version", props.toolVersion);
24+
}
1625
17-
onMounted(() => {
18-
new Webhooks.WebhookView({
19-
webhook,
20-
type: props.type,
21-
toolId: props.toolId,
22-
});
26+
const webhooks = await loadWebhooks();
27+
if (webhooks.length > 0) {
28+
const model = pickWebhook(webhooks);
29+
webhookId.value = model.id;
30+
appendScriptStyle(model);
31+
}
2332
});
2433
</script>
2534

2635
<template>
27-
<div id="webhook-view" ref="webhook" />
36+
<div ref="container">
37+
<div v-if="webhookId" :id="webhookId"></div>
38+
</div>
2839
</template>
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<script setup lang="ts">
2-
import { onMounted } from "vue";
2+
import { computed, onMounted } from "vue";
33
44
import type { WorkflowInvocation } from "@/api/invocations";
5-
import Webhooks from "@/utils/webhooks";
65
import { startWatchingHistory } from "@/watch/watchHistoryProvided";
76
7+
import Webhook from "@/components/Common/Webhook.vue";
88
import GridInvocation from "@/components/Grid/GridInvocation.vue";
99
import WorkflowInvocationState from "@/components/WorkflowInvocationState/WorkflowInvocationState.vue";
1010
@@ -14,20 +14,17 @@ const props = defineProps<{
1414
}>();
1515
1616
onMounted(() => {
17-
new Webhooks.WebhookView({
18-
type: "workflow",
19-
toolId: null,
20-
toolVersion: null,
21-
});
2217
startWatchingHistory();
2318
});
2419
25-
const targetHistories = props.invocations.reduce((histories, invocation) => {
26-
if (invocation.history_id && !histories.includes(invocation.history_id)) {
27-
histories.push(invocation.history_id);
28-
}
29-
return histories;
30-
}, [] as string[]);
20+
const targetHistories = computed(() =>
21+
props.invocations.reduce((histories, invocation) => {
22+
if (invocation.history_id && !histories.includes(invocation.history_id)) {
23+
histories.push(invocation.history_id);
24+
}
25+
return histories;
26+
}, [] as string[])
27+
);
3128
</script>
3229

3330
<template>
@@ -46,6 +43,6 @@ const targetHistories = props.invocations.reduce((histories, invocation) => {
4643
:invocation-id="props.invocations[0].id"
4744
is-full-page
4845
success />
49-
<div id="webhook-view"></div>
46+
<Webhook type="workflow" />
5047
</div>
5148
</template>
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import Utils from "utils/utils";
2-
import Webhooks from "utils/webhooks";
1+
import { appendScriptStyle } from "utils/utils";
2+
import { loadWebhooks } from "utils/webhooks";
33

4-
export function onloadWebhooks(Galaxy) {
4+
export async function onloadWebhooks(Galaxy) {
55
if (Galaxy.config.enable_webhooks) {
6-
Webhooks.load({
7-
type: "onload",
8-
callback: function (webhooks) {
9-
webhooks.forEach((webhook) => {
10-
if (webhook.activate && webhook.script) {
11-
Utils.appendScriptStyle(webhook);
12-
}
13-
});
14-
},
6+
const webhooks = await loadWebhooks("onload");
7+
webhooks.forEach((webhook) => {
8+
if (webhook.activate && webhook.script) {
9+
appendScriptStyle(webhook);
10+
}
1511
});
1612
}
1713
}

client/src/utils/webhooks.js

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import axios from "axios";
2-
import Backbone from "backbone";
32
import { getAppRoot } from "onload/loadConfig";
4-
import { appendScriptStyle } from "utils/utils";
53

64
import { rethrowSimple } from "@/utils/simple-error";
75

86
let webhookData = undefined;
97

10-
async function getWebHookData() {
8+
async function getWebhookData() {
119
if (webhookData === undefined) {
1210
try {
1311
const { data } = await axios.get(`${getAppRoot()}api/webhooks`);
@@ -19,57 +17,16 @@ async function getWebHookData() {
1917
return webhookData;
2018
}
2119

22-
const WebhookView = Backbone.View.extend({
23-
el: "#webhook-view",
24-
25-
initialize: function (options) {
26-
const toolId = options.toolId || "";
27-
const toolVersion = options.toolVersion || "";
28-
29-
this.$el.attr("tool_id", toolId);
30-
this.$el.attr("tool_version", toolVersion);
31-
32-
getWebHookData().then((data) => {
33-
const filteredData = filterData(data, options);
34-
if (filteredData.length > 0) {
35-
this.render(weightedRandomPick(filteredData));
36-
}
37-
});
38-
},
39-
40-
render: function (model) {
41-
this.$el.html(`<div id="${model.id}"></div>`);
42-
appendScriptStyle(model);
43-
return this;
44-
},
45-
});
46-
47-
function filterData(data, options) {
48-
let filteredData = data;
49-
if (options.type) {
50-
filteredData = filterType(data, options.type);
20+
export async function loadWebhooks(type) {
21+
const webhooks = await getWebhookData();
22+
if (type) {
23+
return webhooks.filter((item) => item.type && item.type.indexOf(type) !== -1);
24+
} else {
25+
return webhooks;
5126
}
52-
return filteredData;
53-
}
54-
55-
const load = (options) => {
56-
getWebHookData().then((data) => {
57-
options.callback(filterData(data, options));
58-
});
59-
};
60-
61-
function filterType(data, type) {
62-
return data.filter((item) => {
63-
const itype = item.type;
64-
if (itype) {
65-
return itype.indexOf(type) !== -1;
66-
} else {
67-
return false;
68-
}
69-
});
7027
}
7128

72-
function weightedRandomPick(data) {
29+
export function pickWebhook(data) {
7330
const weights = data.map((d) => d.weight);
7431
const sum = weights.reduce((a, b) => a + b);
7532

@@ -87,8 +44,3 @@ function weightedRandomPick(data) {
8744

8845
return data.at(table[Math.floor(Math.random() * table.length)]);
8946
}
90-
91-
export default {
92-
WebhookView: WebhookView,
93-
load: load,
94-
};

0 commit comments

Comments
 (0)