1 parent
36f5dc04d1
commit
0e93a2b945
141 files changed
+7410
-1485
No files matched your search
@@ -0,0 +1,164 @@
|
||||
<template>
|
||||
<layout>
|
||||
<template #config>
|
||||
<config-panel @generate="onGenerate" />
|
||||
</template>
|
||||
<template #result>
|
||||
<recent-panel @reach-top="onReachTop" />
|
||||
</template>
|
||||
</layout>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue';
|
||||
import Layout from '@/layouts/Sora.vue';
|
||||
import ConfigPanel from '@/components/sora/ConfigPanel.vue';
|
||||
import { soraOperator } from '@/operators';
|
||||
import { ISoraGenerateRequest, Status } from '@/models';
|
||||
import { ElMessage } from 'element-plus';
|
||||
import { ERROR_CODE_USED_UP } from '@/constants';
|
||||
import RecentPanel from '@/components/sora/RecentPanel.vue';
|
||||
import { ISoraTask } from '@/models';
|
||||
|
||||
const CALLBACK_URL = 'https://webhook.acedata.cloud/sora';
|
||||
|
||||
interface IData {
|
||||
task: ISoraTask | undefined;
|
||||
job: number;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: 'SoraIndex',
|
||||
components: {
|
||||
ConfigPanel,
|
||||
Layout,
|
||||
RecentPanel
|
||||
},
|
||||
inject: ['initialized'],
|
||||
data(): IData {
|
||||
return {
|
||||
task: undefined,
|
||||
job: 0
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
loading() {
|
||||
return this.$store.state.sora?.status?.getApplications === Status.Request;
|
||||
},
|
||||
credential() {
|
||||
return this.$store.state.sora.credential;
|
||||
},
|
||||
config() {
|
||||
return this.$store.state.sora.config;
|
||||
},
|
||||
tasks() {
|
||||
return this.$store.state.sora.tasks;
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
tasks: {
|
||||
handler(value, oldValue) {
|
||||
// scroll down if new tasks are added
|
||||
if (value?.items?.length > oldValue?.items?.length) {
|
||||
console.debug('new tasks detected');
|
||||
// this.onScrollDown();
|
||||
}
|
||||
},
|
||||
deep: true
|
||||
},
|
||||
initialized: {
|
||||
async handler(newValue) {
|
||||
if (newValue) {
|
||||
console.debug('layout initialized');
|
||||
await this.onGetTasks();
|
||||
await this.onScrollDown();
|
||||
this.job = setInterval(() => {
|
||||
this.onGetTasks();
|
||||
}, 5000);
|
||||
}
|
||||
},
|
||||
immediate: true
|
||||
}
|
||||
},
|
||||
async mounted() {
|
||||
await this.onGetService();
|
||||
},
|
||||
async unmounted() {
|
||||
clearInterval(this.job);
|
||||
},
|
||||
methods: {
|
||||
async onReachTop() {
|
||||
console.debug('reached top');
|
||||
await this.onGetTasks({
|
||||
createdAtMax: this.tasks?.items?.[0]?.created_at
|
||||
});
|
||||
},
|
||||
async onGetService() {
|
||||
console.debug('start onGetService');
|
||||
await this.$store.dispatch('sora/getService');
|
||||
console.debug('end onGetService');
|
||||
},
|
||||
async onGetApplication() {
|
||||
console.debug('start onGetApplication');
|
||||
await this.$store.dispatch('sora/getApplications');
|
||||
console.debug('end onGetApplication');
|
||||
await this.onGetTasks();
|
||||
},
|
||||
async onScrollDown() {
|
||||
setTimeout(() => {
|
||||
const el = document.querySelector('.tasks');
|
||||
if (el) {
|
||||
el.scrollTop = el.scrollHeight;
|
||||
}
|
||||
}, 500);
|
||||
},
|
||||
async onGetTasks(payload?: { limit?: number; createdAtMin?: number; createdAtMax?: number }) {
|
||||
if (this.loading) {
|
||||
console.debug('loading');
|
||||
return;
|
||||
}
|
||||
console.debug('start onGetTasks', payload);
|
||||
const { limit = 5, createdAtMin, createdAtMax } = payload || {};
|
||||
console.debug('limit', limit, 'createdAtMin', createdAtMin, 'createdAtMax', createdAtMax);
|
||||
await this.$store.dispatch('sora/getTasks', {
|
||||
limit,
|
||||
createdAtMin,
|
||||
createdAtMax
|
||||
});
|
||||
},
|
||||
async onGenerate() {
|
||||
const request = {
|
||||
...this.config,
|
||||
callback_url: CALLBACK_URL
|
||||
} as ISoraGenerateRequest;
|
||||
const token = this.credential?.token;
|
||||
if (!token) {
|
||||
console.error('no token specified');
|
||||
return;
|
||||
}
|
||||
ElMessage.info(this.$t('sora.message.startingTask'));
|
||||
soraOperator
|
||||
.generate(request, {
|
||||
token
|
||||
})
|
||||
.then(() => {
|
||||
ElMessage.success(this.$t('sora.message.startTaskSuccess'));
|
||||
})
|
||||
.catch((error) => {
|
||||
const response = error?.response?.data;
|
||||
if (response?.error?.code === ERROR_CODE_USED_UP) {
|
||||
ElMessage.error(this.$t('sora.message.usedUp'));
|
||||
} else {
|
||||
ElMessage.error(this.$t('sora.message.startTaskFailed'));
|
||||
}
|
||||
})
|
||||
.finally(async () => {
|
||||
setTimeout(async () => {
|
||||
await this.onGetTasks();
|
||||
await this.onScrollDown();
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
Reference in new issue
Block a user