Skip to content

Commit

Permalink
Add midjourney pagination (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf-github-user authored Feb 9, 2025
1 parent 49b2a58 commit 31fc4b4
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "add mj pagination",
"packageName": "@acedatacloud/nexior",
"email": "1348977728@qq.com",
"dependentChangeType": "patch"
}
14 changes: 12 additions & 2 deletions src/components/midjourney/tasks/TaskList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div v-else-if="tasks && tasks?.length === 0">
<p class="p-5 description">{{ $t('midjourney.message.noTasks') }}</p>
</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">
<task-item :full="false" :model-value="task" @custom="$emit('custom', $event)" />
</div>
Expand All @@ -41,7 +41,7 @@ export default defineComponent({
ElSkeleton,
ElSkeletonItem
},
emits: ['update:modelValue', 'custom', 'refresh'],
emits: ['update:modelValue', 'custom', 'refresh', 'reach-top'],
data() {
return {
job: 0
Expand All @@ -55,6 +55,16 @@ export default defineComponent({
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>
Expand Down
1 change: 1 addition & 0 deletions src/components/suno/RecentPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default defineComponent({
methods: {
onHandleScroll() {
const el = this.$refs.panel as HTMLElement;
console.log('reach-top reach-top reach-top');
if (el.scrollTop === 0) {
this.$emit('reach-top');
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface IMidjourneyDescribeResponse {
export interface IMidjourneyImagineTask {
id: string;
type: 'imagine';
created_at?: string;
created_at?: number;
request?: IMidjourneyImagineRequest;
response?: IMidjourneyImagineResponse;
state?: MidjourneyImagineState;
Expand All @@ -100,7 +100,7 @@ export interface IMidjourneyImagineTask {
export interface IMidjourneyDescribeTask {
id: string;
type: 'describe';
created_at?: string;
created_at?: number;
request?: IMidjourneyDescribeRequest;
response?: IMidjourneyDescribeResponse;
}
Expand Down
26 changes: 25 additions & 1 deletion src/operators/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ class MidjourneyOperator {
}

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 }
): Promise<AxiosResponse<IMidjourneyTasksResponse>> {
return await axios.post(
Expand All @@ -50,6 +59,11 @@ class MidjourneyOperator {
user_id: filter.userId
}
: {}),
...(filter.type
? {
type: filter.type
}
: {}),
...(filter.limit !== undefined
? {
limit: filter.limit
Expand All @@ -59,6 +73,16 @@ class MidjourneyOperator {
? {
offset: filter.offset
}
: {}),
...(filter.createdAtMax !== undefined
? {
created_at_max: filter.createdAtMax
}
: {}),
...(filter.createdAtMin !== undefined
? {
created_at_min: filter.createdAtMin
}
: {})
},
{
Expand Down
48 changes: 37 additions & 11 deletions src/pages/midjourney/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
:need-apply="needApply"
@refresh="onGetApplication"
/>
<task-list @custom="onCustom" />
<task-list @custom="onCustom" @reach-top="onReachTop" />
</div>
</template>
</layout>
Expand Down Expand Up @@ -41,6 +41,7 @@ import {
interface IData {
operating: boolean;
job: number;
timer: NodeJS.Timer;
}
const CALLBACK_URL = 'https://webhook.acedata.cloud/midjourney';
Expand All @@ -56,7 +57,9 @@ export default defineComponent({
data(): IData {
return {
operating: false,
job: 0
job: 0,
// @ts-ignore
timer: undefined
};
},
computed: {
Expand Down Expand Up @@ -159,9 +162,11 @@ export default defineComponent({
},
watch: {
tasks: {
handler(val, oldVal) {
if (oldVal === undefined && val) {
this.onScrollDown();
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
Expand All @@ -172,16 +177,23 @@ export default defineComponent({
await this.onGetApplication();
await this.onScrollDown();
await this.onGetTasks();
await this.onScrollDown();
// await this.onScrollDown();
// @ts-ignore
this.job = setInterval(() => {
this.onGetTasks();
}, 5000);
},
async unmounted() {
clearInterval(this.job);
clearInterval(this.timer);
},
methods: {
async onReachTop() {
console.debug('ddasdasdreached top');
await this.onGetTasks({
createdAtMax: this.tasks?.items?.[0]?.created_at
});
},
async onGetService() {
console.debug('start onGetService');
await this.$store.dispatch('midjourney/getService');
Expand Down Expand Up @@ -232,8 +244,12 @@ export default defineComponent({
}
})
.finally(async () => {
await this.onGetTasks();
await this.onScrollDown();
setTimeout(async () => {
await this.onGetTasks();
await this.onScrollDown();
}, 1000);
// await this.onGetTasks();
// await this.onScrollDown();
});
},
async onCustom(payload: { image_id: string; action: MidjourneyImagineAction }) {
Expand All @@ -256,23 +272,33 @@ export default defineComponent({
await this.onStartTask(request);
},
async onScrollDown() {
await this.$nextTick(); // 确保 DOM 更新完成后再执行滚动操作
setTimeout(() => {
// scroll to bottom for `.tasks`
const el = document.querySelector('.tasks');
if (el) {
el.scrollTop = el.scrollHeight;
}
}, 500);
},
async onGetTasks() {
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('midjourney/getTasks', {
limit: 30,
offset: 0
limit,
createdAtMin,
createdAtMax
});
// await this.$store.dispatch('midjourney/getTasks', {
// limit: 30,
// offset: 0
// });
}
}
});
Expand Down
2 changes: 2 additions & 0 deletions src/pages/suno/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ export default defineComponent({
setTimeout(() => {
// scroll to bottom for `.recent`
const el = document.querySelector('.recent');
console.log('onScrollDown ', el, el.scrollTop, el.scrollHeight);
if (el) {
el.scrollTop = el.scrollHeight;
console.log('onScrollDown ', el.scrollTop, el.scrollHeight);
}
}, 1000);
},
Expand Down
39 changes: 31 additions & 8 deletions src/store/midjourney/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ import {
} from '@/models';
import { Status } from '@/models/common';
import { MIDJOURNEY_SERVICE_ID } from '@/constants';
import { mergeAndSortLists } from '@/utils/merge';

export const resetAll = ({ commit }: ActionContext<IMidjourneyState, IRootState>): void => {
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> => {
console.debug(setCredential, 'set credential', payload);
commit('setCredential', payload);
Expand Down Expand Up @@ -113,10 +122,15 @@ export const getService = async ({ commit, state }: ActionContext<IMidjourneySta

export const getTasks = async (
{ 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) => {
console.debug('start to get tasks', offset, limit);
console.debug('start to get tasks', offset, limit, createdAtMax, createdAtMin);
const credential = state.credential;
const token = credential?.token;
if (!token) {
Expand All @@ -126,17 +140,26 @@ export const getTasks = async (
.tasks(
{
userId: rootState?.user?.id,
offset,
limit
createdAtMin,
createdAtMax,
type: 'imagine'
},
{
token
}
)
.then((response) => {
console.debug('get tasks success', response.data);
commit('setTasks', response.data);
resolve(response.data);
console.debug('get imagine tasks success', response.data.items);
// merge with existing tasks
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) => {
return reject(error);
Expand Down
20 changes: 19 additions & 1 deletion src/store/midjourney/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IApplication, ICredential, IMidjourneyConfig, IService } from '@/models';
import { IApplication, ICredential, IMidjourneyConfig, IService, IMidjourneyTask } from '@/models';
import { IMidjourneyState } from './models';

export const resetAll = (state: IMidjourneyState): void => {
Expand All @@ -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 => {
state.service = payload;
};
Expand Down Expand Up @@ -39,6 +55,8 @@ export default {
setApplication,
setApplications,
setConfig,
setTasksItems,
setTasksTotal,
setCredential,
setService,
setTasks,
Expand Down
8 changes: 1 addition & 7 deletions src/store/midjourney/persist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
export default [
'midjourney.config',
'midjourney.credential',
'midjourney.mode',
'midjourney.application',
'midjourney.tasks'
];
export default ['midjourney.config', 'midjourney.credential', 'midjourney.mode', 'midjourney.application'];

0 comments on commit 31fc4b4

Please sign in to comment.