Add midjourney pagination (#172)

This commit is contained in:
HCode authored and GitHub committed 2025-02-10 02:32:36 +08:00
1 parent 49b2a5846c
commit 31fc4b4472
10 files changed
+137 -32

No files matched your search

@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "add mj pagination",
"packageName": "@acedatacloud/nexior",
"email": "1348977728@qq.com",
"dependentChangeType": "patch"
}
+12 -2
View File
@@ -21,7 +21,7 @@
<div v-else-if="tasks && tasks?.length === 0"> <div v-else-if="tasks && tasks?.length === 0">
<p class="p-5 description">{{ $t('midjourney.message.noTasks') }}</p> <p class="p-5 description">{{ $t('midjourney.message.noTasks') }}</p>
</div> </div>
<div v-else-if="tasks.length > 0" class="tasks"> <div v-else-if="tasks.length > 0" ref="panel" class="tasks" @scroll="onHandleScroll">
<div v-for="(task, taskKey) in tasks" :key="taskKey" class="task"> <div v-for="(task, taskKey) in tasks" :key="taskKey" class="task">
<task-item :full="false" :model-value="task" @custom="$emit('custom', $event)" /> <task-item :full="false" :model-value="task" @custom="$emit('custom', $event)" />
</div> </div>
@@ -41,7 +41,7 @@ export default defineComponent({
ElSkeleton, ElSkeleton,
ElSkeletonItem ElSkeletonItem
}, },
emits: ['update:modelValue', 'custom', 'refresh'], emits: ['update:modelValue', 'custom', 'refresh', 'reach-top'],
data() { data() {
return { return {
job: 0 job: 0
@@ -55,6 +55,16 @@ export default defineComponent({
application() { application() {
return this.$store.state.midjourney.application; return this.$store.state.midjourney.application;
} }
},
methods: {
onHandleScroll() {
const el = this.$refs.panel as HTMLElement;
console.log('onHandleScroll ', el.scrollTop);
if (el.scrollTop === 0) {
console.log('reach-top reach-top');
this.$emit('reach-top');
}
}
} }
}); });
</script> </script>
+1
View File
@@ -66,6 +66,7 @@ export default defineComponent({
methods: { methods: {
onHandleScroll() { onHandleScroll() {
const el = this.$refs.panel as HTMLElement; const el = this.$refs.panel as HTMLElement;
console.log('reach-top reach-top reach-top');
if (el.scrollTop === 0) { if (el.scrollTop === 0) {
this.$emit('reach-top'); this.$emit('reach-top');
} }
+2 -2
View File
@@ -91,7 +91,7 @@ export interface IMidjourneyDescribeResponse {
export interface IMidjourneyImagineTask { export interface IMidjourneyImagineTask {
id: string; id: string;
type: 'imagine'; type: 'imagine';
created_at?: string; created_at?: number;
request?: IMidjourneyImagineRequest; request?: IMidjourneyImagineRequest;
response?: IMidjourneyImagineResponse; response?: IMidjourneyImagineResponse;
state?: MidjourneyImagineState; state?: MidjourneyImagineState;
@@ -100,7 +100,7 @@ export interface IMidjourneyImagineTask {
export interface IMidjourneyDescribeTask { export interface IMidjourneyDescribeTask {
id: string; id: string;
type: 'describe'; type: 'describe';
created_at?: string; created_at?: number;
request?: IMidjourneyDescribeRequest; request?: IMidjourneyDescribeRequest;
response?: IMidjourneyDescribeResponse; response?: IMidjourneyDescribeResponse;
} }
+25 -1
View File
@@ -28,7 +28,16 @@ class MidjourneyOperator {
} }
async tasks( async tasks(
filter: { ids?: string[]; applicationId?: string; userId?: string; limit?: number; offset?: number }, filter: {
ids?: string[];
applicationId?: string;
userId?: string;
type?: string;
limit?: number;
offset?: number;
createdAtMax?: number;
createdAtMin?: number;
},
options: { token: string } options: { token: string }
): Promise<AxiosResponse<IMidjourneyTasksResponse>> { ): Promise<AxiosResponse<IMidjourneyTasksResponse>> {
return await axios.post( return await axios.post(
@@ -50,6 +59,11 @@ class MidjourneyOperator {
user_id: filter.userId user_id: filter.userId
} }
: {}), : {}),
...(filter.type
? {
type: filter.type
}
: {}),
...(filter.limit !== undefined ...(filter.limit !== undefined
? { ? {
limit: filter.limit limit: filter.limit
@@ -59,6 +73,16 @@ class MidjourneyOperator {
? { ? {
offset: filter.offset offset: filter.offset
} }
: {}),
...(filter.createdAtMax !== undefined
? {
created_at_max: filter.createdAtMax
}
: {}),
...(filter.createdAtMin !== undefined
? {
created_at_min: filter.createdAtMin
}
: {}) : {})
}, },
{ {
+37 -11
View File
@@ -12,7 +12,7 @@
:need-apply="needApply" :need-apply="needApply"
@refresh="onGetApplication" @refresh="onGetApplication"
/> />
<task-list @custom="onCustom" /> <task-list @custom="onCustom" @reach-top="onReachTop" />
</div> </div>
</template> </template>
</layout> </layout>
@@ -41,6 +41,7 @@ import {
interface IData { interface IData {
operating: boolean; operating: boolean;
job: number; job: number;
timer: NodeJS.Timer;
} }
const CALLBACK_URL = 'https://webhook.acedata.cloud/midjourney'; const CALLBACK_URL = 'https://webhook.acedata.cloud/midjourney';
@@ -56,7 +57,9 @@ export default defineComponent({
data(): IData { data(): IData {
return { return {
operating: false, operating: false,
job: 0 job: 0,
// @ts-ignore
timer: undefined
}; };
}, },
computed: { computed: {
@@ -159,9 +162,11 @@ export default defineComponent({
}, },
watch: { watch: {
tasks: { tasks: {
handler(val, oldVal) { handler(value, oldValue) {
if (oldVal === undefined && val) { // scroll down if new tasks are added
this.onScrollDown(); if (value?.items?.length > oldValue?.items?.length) {
console.debug('new tasks detected');
// this.onScrollDown();
} }
}, },
deep: true deep: true
@@ -172,7 +177,7 @@ export default defineComponent({
await this.onGetApplication(); await this.onGetApplication();
await this.onScrollDown(); await this.onScrollDown();
await this.onGetTasks(); await this.onGetTasks();
await this.onScrollDown(); // await this.onScrollDown();
// @ts-ignore // @ts-ignore
this.job = setInterval(() => { this.job = setInterval(() => {
this.onGetTasks(); this.onGetTasks();
@@ -180,8 +185,15 @@ export default defineComponent({
}, },
async unmounted() { async unmounted() {
clearInterval(this.job); clearInterval(this.job);
clearInterval(this.timer);
}, },
methods: { methods: {
async onReachTop() {
console.debug('ddasdasdreached top');
await this.onGetTasks({
createdAtMax: this.tasks?.items?.[0]?.created_at
});
},
async onGetService() { async onGetService() {
console.debug('start onGetService'); console.debug('start onGetService');
await this.$store.dispatch('midjourney/getService'); await this.$store.dispatch('midjourney/getService');
@@ -232,8 +244,12 @@ export default defineComponent({
} }
}) })
.finally(async () => { .finally(async () => {
await this.onGetTasks(); setTimeout(async () => {
await this.onScrollDown(); await this.onGetTasks();
await this.onScrollDown();
}, 1000);
// await this.onGetTasks();
// await this.onScrollDown();
}); });
}, },
async onCustom(payload: { image_id: string; action: MidjourneyImagineAction }) { async onCustom(payload: { image_id: string; action: MidjourneyImagineAction }) {
@@ -256,23 +272,33 @@ export default defineComponent({
await this.onStartTask(request); await this.onStartTask(request);
}, },
async onScrollDown() { async onScrollDown() {
await this.$nextTick(); // 确保 DOM 更新完成后再执行滚动操作
setTimeout(() => { setTimeout(() => {
// scroll to bottom for `.tasks` // scroll to bottom for `.tasks`
const el = document.querySelector('.tasks'); const el = document.querySelector('.tasks');
if (el) { if (el) {
el.scrollTop = el.scrollHeight; el.scrollTop = el.scrollHeight;
} }
}, 500); }, 500);
}, },
async onGetTasks() { async onGetTasks(payload?: { limit?: number; createdAtMin?: number; createdAtMax?: number }) {
if (this.loading) { if (this.loading) {
console.debug('loading'); console.debug('loading');
return; return;
} }
console.debug('start onGetTasks', payload);
const { limit = 5, createdAtMin, createdAtMax } = payload || {};
console.debug('limit', limit, 'createdAtMin', createdAtMin, 'createdAtMax', createdAtMax);
await this.$store.dispatch('midjourney/getTasks', { await this.$store.dispatch('midjourney/getTasks', {
limit: 30, limit,
offset: 0 createdAtMin,
createdAtMax
}); });
// await this.$store.dispatch('midjourney/getTasks', {
// limit: 30,
// offset: 0
// });
} }
} }
}); });
+2
View File
@@ -149,8 +149,10 @@ export default defineComponent({
setTimeout(() => { setTimeout(() => {
// scroll to bottom for `.recent` // scroll to bottom for `.recent`
const el = document.querySelector('.recent'); const el = document.querySelector('.recent');
console.log('onScrollDown ', el, el.scrollTop, el.scrollHeight);
if (el) { if (el) {
el.scrollTop = el.scrollHeight; el.scrollTop = el.scrollHeight;
console.log('onScrollDown ', el.scrollTop, el.scrollHeight);
} }
}, 1000); }, 1000);
}, },
+31 -8
View File
@@ -13,11 +13,20 @@ import {
} from '@/models'; } from '@/models';
import { Status } from '@/models/common'; import { Status } from '@/models/common';
import { MIDJOURNEY_SERVICE_ID } from '@/constants'; import { MIDJOURNEY_SERVICE_ID } from '@/constants';
import { mergeAndSortLists } from '@/utils/merge';
export const resetAll = ({ commit }: ActionContext<IMidjourneyState, IRootState>): void => { export const resetAll = ({ commit }: ActionContext<IMidjourneyState, IRootState>): void => {
commit('resetAll'); commit('resetAll');
}; };
export const setTasksItems = ({ commit }: any, payload: IMidjourneyTask[]) => {
commit('setTasksItems', payload);
};
export const setTasksTotal = ({ commit }: any, payload: number) => {
commit('setTasksTotal', payload);
};
export const setCredential = async ({ commit }: any, payload: ICredential): Promise<void> => { export const setCredential = async ({ commit }: any, payload: ICredential): Promise<void> => {
console.debug(setCredential, 'set credential', payload); console.debug(setCredential, 'set credential', payload);
commit('setCredential', payload); commit('setCredential', payload);
@@ -113,10 +122,15 @@ export const getService = async ({ commit, state }: ActionContext<IMidjourneySta
export const getTasks = async ( export const getTasks = async (
{ commit, state, rootState }: ActionContext<IMidjourneyState, IRootState>, { commit, state, rootState }: ActionContext<IMidjourneyState, IRootState>,
{ offset, limit }: { offset?: number; limit?: number } {
): Promise<IMidjourneyTasksResponse> => { offset,
limit,
createdAtMin,
createdAtMax
}: { offset?: number; limit?: number; createdAtMin?: number; createdAtMax?: number }
): Promise<IMidjourneyTask[]> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.debug('start to get tasks', offset, limit); console.debug('start to get tasks', offset, limit, createdAtMax, createdAtMin);
const credential = state.credential; const credential = state.credential;
const token = credential?.token; const token = credential?.token;
if (!token) { if (!token) {
@@ -126,17 +140,26 @@ export const getTasks = async (
.tasks( .tasks(
{ {
userId: rootState?.user?.id, userId: rootState?.user?.id,
offset, createdAtMin,
limit createdAtMax,
type: 'imagine'
}, },
{ {
token token
} }
) )
.then((response) => { .then((response) => {
console.debug('get tasks success', response.data); console.debug('get imagine tasks success', response.data.items);
commit('setTasks', response.data); // merge with existing tasks
resolve(response.data); const existingItems = state?.tasks?.items || [];
console.debug('existing items', existingItems);
const newItems = response.data.items || [];
console.debug('new items', newItems);
// sort and de-duplicate using created_at
const mergedItems = mergeAndSortLists(existingItems, newItems);
commit('setTasksItems', mergedItems);
commit('setTasksTotal', response.data.count);
resolve(response.data.items);
}) })
.catch((error) => { .catch((error) => {
return reject(error); return reject(error);
+19 -1
View File
@@ -1,4 +1,4 @@
import { IApplication, ICredential, IMidjourneyConfig, IService } from '@/models'; import { IApplication, ICredential, IMidjourneyConfig, IService, IMidjourneyTask } from '@/models';
import { IMidjourneyState } from './models'; import { IMidjourneyState } from './models';
export const resetAll = (state: IMidjourneyState): void => { export const resetAll = (state: IMidjourneyState): void => {
@@ -11,6 +11,22 @@ export const resetAll = (state: IMidjourneyState): void => {
}; };
}; };
export const setTasksItems = (state: IMidjourneyState, payload: IMidjourneyTask[]): void => {
const newPayload = {
...state.tasks,
items: payload
} as typeof state.tasks;
state.tasks = newPayload;
};
export const setTasksTotal = (state: IMidjourneyState, payload: number): void => {
const newPayload = {
...state.tasks,
total: payload
} as typeof state.tasks;
state.tasks = newPayload;
};
export const setService = (state: IMidjourneyState, payload: IService): void => { export const setService = (state: IMidjourneyState, payload: IService): void => {
state.service = payload; state.service = payload;
}; };
@@ -39,6 +55,8 @@ export default {
setApplication, setApplication,
setApplications, setApplications,
setConfig, setConfig,
setTasksItems,
setTasksTotal,
setCredential, setCredential,
setService, setService,
setTasks, setTasks,
+1 -7
View File
@@ -1,7 +1 @@
export default [ export default ['midjourney.config', 'midjourney.credential', 'midjourney.mode', 'midjourney.application'];
'midjourney.config',
'midjourney.credential',
'midjourney.mode',
'midjourney.application',
'midjourney.tasks'
];