add pagination for start chat record table

This commit is contained in:
geekgeekrun
2024-03-29 09:37:52 +08:00
parent 03ebfe41c4
commit aace874580
5 changed files with 81 additions and 24 deletions
@@ -0,0 +1,9 @@
export interface PageReq {
pageNo: number
pageSize: number
}
export interface PagedRes<T = unknown> {
data: T[]
pageNo: number
totalItemCount: number
}
@@ -17,9 +17,10 @@ import { getAnyAvailablePuppeteerExecutable } from '../../../flow/CHECK_AND_DOWN
import { sleep } from '@geekgeekrun/utils/sleep.mjs' import { sleep } from '@geekgeekrun/utils/sleep.mjs'
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../../common/enums/auto-start-chat' import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../../common/enums/auto-start-chat'
import { mainWindow } from '../../../window/mainWindow' 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) => { ipcMain.on('open-external-link', (_, link) => {
shell.openExternal(link, { shell.openExternal(link, {
activate: true activate: true
@@ -259,8 +260,8 @@ export default function initIpc () {
return checkCookieListFormat(cookies) return checkCookieListFormat(cookies)
}) })
ipcMain.handle('get-auto-start-chat-record', async () => { ipcMain.handle('get-auto-start-chat-record', async (ev, payload: PageReq) => {
const a = await getAutoStartChatRecord() const a = await getAutoStartChatRecord(payload)
return a return a
}) })
} }
@@ -1,6 +1,7 @@
import createDbWorker from './worker/index?nodeWorker&url' import createDbWorker from './worker/index?nodeWorker&url'
import { type Worker } from 'node:worker_threads' import { type Worker } from 'node:worker_threads'
import { randomUUID } from 'node:crypto' import { randomUUID } from 'node:crypto'
import { PageReq } from '../../../../../common/types/pagination'
let worker: Worker | null = null let worker: Worker | null = null
let workerExitCode: number | 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({ const res = await createWorkerPromise({
type: 'getAutoStartChatRecord' type: 'getAutoStartChatRecord',
pageNo,
pageSize
}) })
return res return res
} }
@@ -5,6 +5,7 @@ import { type DataSource } from 'typeorm'
import { getPublicDbFilePath } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' import { getPublicDbFilePath } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs'
import { VChatStartupLog } from '@geekgeekrun/sqlite-plugin/dist/entity/VChatStartupLog' import { VChatStartupLog } from '@geekgeekrun/sqlite-plugin/dist/entity/VChatStartupLog'
import { measureExecutionTime } from '../../../../../../common/utils/performance' import { measureExecutionTime } from '../../../../../../common/utils/performance'
import { PageReq, PagedRes } from '../../../../../../common/types/pagination'
const dbInitPromise = initDb(getPublicDbFilePath()) const dbInitPromise = initDb(getPublicDbFilePath())
let dataSource: DataSource | null = null let dataSource: DataSource | null = null
@@ -27,16 +28,28 @@ dbInitPromise.then(
) )
const payloadHandler = { const payloadHandler = {
async getAutoStartChatRecord(payload) { async getAutoStartChatRecord({ pageNo, pageSize }: Partial<PageReq> = {}): Promise<
const result = await measureExecutionTime( PagedRes<VChatStartupLog>
dataSource! > {
.createQueryBuilder() if (!pageNo) {
.select('*') pageNo = 1
.from(VChatStartupLog, 'vChatStartupLog') }
.getRawMany() 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 {
return result data,
pageNo,
totalItemCount
}
} }
} }
@@ -1,9 +1,11 @@
<template> <template>
<div class="page-wrap flex flex-col of-hidden"> <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-0">
<div class="flex-1 of-hidden" v-loading="isTableLoading"> <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"> <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="companyName" label="公司" />
<ElTableColumn prop="jobName" label="职位名称" /> <ElTableColumn prop="jobName" label="职位名称" />
<ElTableColumn prop="positionName" label="职位分类" /> <ElTableColumn prop="positionName" label="职位分类" />
@@ -22,31 +24,60 @@
</ElTable> </ElTable>
</div> </div>
</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> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, onUnmounted, onBeforeUnmount } from 'vue' import { ref, onMounted, onBeforeUnmount } from 'vue'
import { ElTable, ElTableColumn, ElButton } from 'element-plus' import { ElTable, ElTableColumn, ElButton, ElPagination } from 'element-plus'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import { type VChatStartupLog } from '@geekgeekrun/sqlite-plugin/src/entity/VChatStartupLog' import { type VChatStartupLog } from '@geekgeekrun/sqlite-plugin/src/entity/VChatStartupLog'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { PageReq, PagedRes } from '../../../../common/types/pagination'
const router = useRouter() const router = useRouter()
const tableData = ref<VChatStartupLog[]>([]) 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) const isTableLoading = ref(false)
async function getAutoStartChatRecord() { async function getAutoStartChatRecord() {
try { try {
isTableLoading.value = true isTableLoading.value = true
const res = (await electron.ipcRenderer.invoke('get-auto-start-chat-record')) as { const { data: res } = (await electron.ipcRenderer.invoke('get-auto-start-chat-record', {
data: VChatStartupLog[] pageNo: pagination.value.pageNo,
} pageSize: pagination.value.pageSize
})) as { data: PagedRes<VChatStartupLog> }
tableData.value = res.data tableData.value = res.data
pagination.value = {
totalItemCount: res.totalItemCount,
pageNo: res.pageNo,
pageSize: pagination.value.pageSize
}
} catch (err) { } catch (err) {
console.log(err) console.log(err)
tableData.value = [] tableData.value = []
} finally { } finally {
tableRef.value?.setScrollTop(0)
isTableLoading.value = false isTableLoading.value = false
} }
} }