update multi fetch

This commit is contained in:
AceDataCloud committed 2023-11-05 14:29:11 +08:00
1 parent 5d28ab219f
commit e4bd995e6c
6 files changed
+31 -17

No files matched your search

@@ -0,0 +1,96 @@
<template>
<div class="tasks">
<div v-for="(task, taskKey) in tasks" :key="taskKey" class="task">
<task-preview :model-value="task" @custom="$emit('custom', $event)" />
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import TaskPreview from './TaskPreview.vue';
import {
IApplication,
IMidjourneyImagineTask,
MidjourneyImagineState,
apiUsageOperator,
midjourneyOperator
} from '@/operators';
interface IData {
historyTasks: IMidjourneyImagineTask[];
}
export default defineComponent({
name: 'TaskBriefList',
components: {
TaskPreview
},
props: {
activeTask: {
type: Object as () => IMidjourneyImagineTask | undefined,
required: true
},
applications: {
type: Object as () => IApplication[],
required: true
}
},
emits: ['update:modelValue', 'custom', 'update:activeTask'],
data(): IData {
return {
historyTasks: []
};
},
computed: {
tasks() {
return [...(this.activeTask ? [this.activeTask] : []), ...this.historyTasks];
}
},
watch: {
applications(val) {
if (val) {
this.getHistoryTasks();
}
},
activeTask(val: IMidjourneyImagineTask | undefined, oldVal: IMidjourneyImagineTask | undefined) {
if (
(val?.state === MidjourneyImagineState.FINISHED && oldVal?.state !== MidjourneyImagineState.FINISHED) ||
(val?.state === MidjourneyImagineState.FAILED && oldVal?.state !== MidjourneyImagineState.FAILED)
) {
console.log('need to refresh history tasks');
this.getHistoryTasks();
this.$emit('update:activeTask', undefined);
}
}
},
methods: {
async getHistoryTasks() {
const {
data: { items: apiUsages }
} = await apiUsageOperator.getAll({
user_id: this.$store.state.user.id,
application_id: this.applications?.map((application) => application.id),
offset: 0,
limit: 5,
ordering: '-created_at'
});
const tasks = (await midjourneyOperator.tasks(apiUsages.map((apiUsage) => apiUsage.metadata?.task_id as string)))
.data;
this.historyTasks = tasks.filter((task) => task && task?.response);
}
}
});
</script>
<style lang="scss" scoped>
.tasks {
padding-top: 20px;
height: 100%;
overflow-y: scroll;
border-left: 1px solid var(--el-border-color);
display: flex;
flex-direction: column;
align-items: center;
}
</style>
@@ -0,0 +1,172 @@
<template>
<div class="preview">
<div class="info mb-4">
<div class="left">
<el-tag v-if="modelValue?.response?.success === true" type="success">成功</el-tag>
<el-tag v-if="modelValue?.response?.success === false" type="danger">失败</el-tag>
</div>
<div class="right"></div>
</div>
<div v-if="modelValue?.state === midjourneyImagineState.PENDING" class="content">
<el-image class="image">
<template #error>
<div class="image-slot">Loading...</div>
</template>
</el-image>
</div>
<div v-else-if="modelValue?.state === midjourneyImagineState.FAILED" class="content">
<el-image class="image">
<template #error>
<div class="image-slot">{{ modelValue?.response?.detail }}</div>
</template>
</el-image>
</div>
<div v-else class="content">
<el-image
v-if="modelValue?.response?.image_url"
:src="modelValue?.response?.image_url"
:preview-src-list="[modelValue?.response?.raw_image_url as string]"
fit="fill"
class="image"
/>
<p v-if="modelValue?.response?.progress !== undefined && modelValue?.response?.progress !== 100" class="progress">
{{ modelValue?.response?.progress }}%
</p>
</div>
<div class="operations">
<el-button
v-for="(action, actionKey) in modelValue?.response?.actions"
:key="actionKey"
type="info"
size="small"
class="btn-action"
@click="onCustom(action)"
>
{{ actionMapping[action] }}
</el-button>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { ElImage, ElTag, ElButton } from 'element-plus';
import { IMidjourneyImagineTask, MidjourneyImagineAction, MidjourneyImagineState } from '@/operators';
interface IData {
midjourneyImagineState: typeof MidjourneyImagineState;
actionMapping: Record<MidjourneyImagineAction, string>;
}
export default defineComponent({
name: 'TaskPreview',
components: {
ElImage,
ElTag,
ElButton
},
props: {
modelValue: {
type: Object as () => IMidjourneyImagineTask | undefined,
required: true
}
},
emits: ['custom'],
data(): IData {
return {
midjourneyImagineState: MidjourneyImagineState,
actionMapping: {
[MidjourneyImagineAction.GENERATE]: 'Generate',
[MidjourneyImagineAction.UPSAMPLE1]: 'U1',
[MidjourneyImagineAction.UPSAMPLE2]: 'U2',
[MidjourneyImagineAction.UPSAMPLE3]: 'U3',
[MidjourneyImagineAction.UPSAMPLE4]: 'U4',
[MidjourneyImagineAction.VARIATION1]: 'V1',
[MidjourneyImagineAction.VARIATION2]: 'V2',
[MidjourneyImagineAction.VARIATION3]: 'V3',
[MidjourneyImagineAction.VARIATION4]: 'V4',
[MidjourneyImagineAction.HIGH_VARIATION]: 'Vary (Strong)',
[MidjourneyImagineAction.LOW_VARIATION]: 'Vary (Subtle)',
[MidjourneyImagineAction.ZOOM_OUT_2X]: 'Zoom Out 2x',
[MidjourneyImagineAction.ZOOM_OUT_1_5X]: 'Zoom Out 1.5x',
[MidjourneyImagineAction.SQUARE]: 'Make Square',
[MidjourneyImagineAction.PAN_LEFT]: '⬅️',
[MidjourneyImagineAction.PAN_UP]: '⬆️',
[MidjourneyImagineAction.PAN_DOWN]: '⬇️',
[MidjourneyImagineAction.PAN_RIGHT]: '➡️',
[MidjourneyImagineAction.REROLL]: '🔄'
}
};
},
methods: {
onCustom(action: string) {
this.$emit('custom', {
action,
image_id: this.modelValue?.response?.image_id
});
}
}
});
</script>
<style lang="scss" scoped>
.preview {
width: 350px;
padding: 15px;
display: flex;
flex-direction: column;
align-items: center;
border-radius: 20px;
margin-bottom: 20px;
border: 1px solid var(--el-border-color);
.info {
display: flex;
flex-direction: row;
justify-content: stretch;
width: 100%;
}
.content {
width: 100%;
position: relative;
margin-bottom: 10px;
.image {
width: 100%;
min-height: 200px;
.image-slot {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
background: var(--el-fill-color-light);
color: var(--el-text-color-secondary);
// font-size: 30px;
}
}
.progress {
position: absolute;
top: 49%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 20px;
font-weight: bold;
color: white;
text-shadow: 0 0 5px black;
}
}
.operations {
display: flex;
justify-content: center;
flex-direction: row;
width: 100%;
align-items: baseline;
flex-wrap: wrap;
.btn-action {
margin-bottom: 10px;
}
}
}
</style>