add pagination for start chat record table

This commit is contained in:
geekgeekrun
2024-04-01 01:33:18 +08:00
parent 03ebfe41c4
commit aace874580
5 changed files with 81 additions and 24 deletions

View File

@@ -0,0 +1,9 @@
export interface PageReq {
pageNo: number
pageSize: number
}
export interface PagedRes<T = unknown> {
data: T[]
pageNo: number
totalItemCount: number
}

View File

@@ -17,9 +17,10 @@ import { getAnyAvailablePuppeteerExecutable } from '../../../flow/CHECK_AND_DOWN
import { sleep } from '@geekgeekrun/utils/sleep.mjs'
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../../common/enums/auto-start-chat'
import { mainWindow } from '../../../window/mainWindow'
import { getAutoStartChatRecord, initDbWorker } from '../utils/db/index'
import { getAutoStartChatRecord } from '../utils/db/index'
import { PageReq } from '../../../../common/types/pagination'
export default function initIpc () {
export default function initIpc() {
ipcMain.on('open-external-link', (_, link) => {
shell.openExternal(link, {
activate: true
@@ -259,8 +260,8 @@ export default function initIpc () {
return checkCookieListFormat(cookies)
})
ipcMain.handle('get-auto-start-chat-record', async () => {
const a = await getAutoStartChatRecord()
ipcMain.handle('get-auto-start-chat-record', async (ev, payload: PageReq) => {
const a = await getAutoStartChatRecord(payload)
return a
})
}

View File

@@ -1,6 +1,7 @@
import createDbWorker from './worker/index?nodeWorker&url'
import { type Worker } from 'node:worker_threads'
import { randomUUID } from 'node:crypto'
import { PageReq } from '../../../../../common/types/pagination'
let worker: Worker | null = null
let workerExitCode: number | null = null
@@ -49,9 +50,11 @@ const createWorkerPromise = async (data) => {
})
}
export const getAutoStartChatRecord = async () => {
export const getAutoStartChatRecord = async ({ pageNo, pageSize }: Partial<PageReq> = {}) => {
const res = await createWorkerPromise({
type: 'getAutoStartChatRecord'
type: 'getAutoStartChatRecord',
pageNo,
pageSize
})
return res
}

View File

@@ -5,6 +5,7 @@ import { type DataSource } from 'typeorm'
import { getPublicDbFilePath } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs'
import { VChatStartupLog } from '@geekgeekrun/sqlite-plugin/dist/entity/VChatStartupLog'
import { measureExecutionTime } from '../../../../../../common/utils/performance'
import { PageReq, PagedRes } from '../../../../../../common/types/pagination'
const dbInitPromise = initDb(getPublicDbFilePath())
let dataSource: DataSource | null = null
@@ -27,16 +28,28 @@ dbInitPromise.then(
)
const payloadHandler = {
async getAutoStartChatRecord(payload) {
const result = await measureExecutionTime(
dataSource!
.createQueryBuilder()
.select('*')
.from(VChatStartupLog, 'vChatStartupLog')
.getRawMany()
async getAutoStartChatRecord({ pageNo, pageSize }: Partial<PageReq> = {}): Promise<
PagedRes<VChatStartupLog>
> {
if (!pageNo) {
pageNo = 1
}
if (!pageSize) {
pageSize = 10
}
const userRepository = dataSource!.getRepository(VChatStartupLog)!
const [data, totalItemCount] = await measureExecutionTime(
userRepository.findAndCount({
skip: (pageNo - 1) * pageSize,
take: pageSize
})
)
console.log(result)
return result
return {
data,
pageNo,
totalItemCount
}
}
}

View File

@@ -1,9 +1,11 @@
<template>
<div class="page-wrap flex flex-col of-hidden">
<div class="flex-0"><el-button @click="getAutoStartChatRecord" :loading="isTableLoading">刷新</el-button></div>
<div class="flex-1 of-hidden" v-loading="isTableLoading">
<div class="flex-0">
<el-button :loading="isTableLoading" @click="getAutoStartChatRecord">刷新</el-button>
</div>
<div v-loading="isTableLoading" class="flex-1 of-hidden">
<div ref="tableContainerEl" class="h-100% of-hidden">
<ElTable :data="tableData" :max-height="tableMaxHeight">
<ElTable ref="tableRef" :data="tableData" :max-height="tableMaxHeight" :row-key="getRowKey">
<ElTableColumn prop="companyName" label="公司" />
<ElTableColumn prop="jobName" label="职位名称" />
<ElTableColumn prop="positionName" label="职位分类" />
@@ -22,31 +24,60 @@
</ElTable>
</div>
</div>
<ElPagination
v-model:current-page="pagination.pageNo"
v-model:page-size="pagination.pageSize"
class="flex-0 flex-justify-center pt10px pb10px"
:page-sizes="pageSizeList"
small
:disabled="isTableLoading"
layout="total, sizes, prev, pager, next, jumper"
:total="pagination.totalItemCount"
@size-change="getAutoStartChatRecord"
@current-change="getAutoStartChatRecord"
/>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted, onBeforeUnmount } from 'vue'
import { ElTable, ElTableColumn, ElButton } from 'element-plus'
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { ElTable, ElTableColumn, ElButton, ElPagination } from 'element-plus'
import { useRouter } from 'vue-router'
import { type VChatStartupLog } from '@geekgeekrun/sqlite-plugin/src/entity/VChatStartupLog'
import dayjs from 'dayjs'
import { PageReq, PagedRes } from '../../../../common/types/pagination'
const router = useRouter()
const tableData = ref<VChatStartupLog[]>([])
const pageSizeList = ref<number[]>([100, 200, 300, 400])
const pagination = ref<Omit<PageReq & PagedRes<unknown>, 'data'>>({
pageNo: 1,
pageSize: pageSizeList.value[0],
totalItemCount: 0
})
const getRowKey = (row: VChatStartupLog) => {
return `${row.encryptJobId}@${row.date}`
}
const tableRef = ref<InstanceType<typeof ElTable>>()
const isTableLoading = ref(false)
async function getAutoStartChatRecord() {
try {
isTableLoading.value = true
const res = (await electron.ipcRenderer.invoke('get-auto-start-chat-record')) as {
data: VChatStartupLog[]
}
const { data: res } = (await electron.ipcRenderer.invoke('get-auto-start-chat-record', {
pageNo: pagination.value.pageNo,
pageSize: pagination.value.pageSize
})) as { data: PagedRes<VChatStartupLog> }
tableData.value = res.data
pagination.value = {
totalItemCount: res.totalItemCount,
pageNo: res.pageNo,
pageSize: pagination.value.pageSize
}
} catch (err) {
console.log(err)
tableData.value = []
} finally {
tableRef.value?.setScrollTop(0)
isTableLoading.value = false
}
}