From 498b73608af065f8652a41f26d535170a7106940 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sat, 21 May 2022 22:34:07 +0800 Subject: [PATCH] fix: fix recent visited records --- packages/client/src/data/document.ts | 5 ++++- packages/client/src/pages/index.tsx | 1 + packages/server/src/services/document.service.ts | 10 +++++++--- packages/server/src/services/view.service.ts | 13 +++++++++++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/client/src/data/document.ts b/packages/client/src/data/document.ts index 72cfe11e..78d06217 100644 --- a/packages/client/src/data/document.ts +++ b/packages/client/src/data/document.ts @@ -1,5 +1,6 @@ import type { IAuthority, IDocument, IUser, IWiki } from '@think/domains'; import { useAsyncLoading } from 'hooks/use-async-loading'; +import { string } from 'lib0'; import { useCallback, useEffect, useState } from 'react'; import { getPublicDocumentDetail } from 'services/document'; import { HttpClient } from 'services/http-client'; @@ -94,7 +95,9 @@ export const useDocumentVersion = (documentId) => { * @returns */ export const useRecentDocuments = () => { - const { data, error, mutate } = useSWR('/document/recent', (url) => HttpClient.get(url)); + const { data, error, mutate } = useSWR>('/document/recent', (url) => + HttpClient.get(url) + ); const loading = !data && !error; return { data, error, loading, refresh: mutate }; }; diff --git a/packages/client/src/pages/index.tsx b/packages/client/src/pages/index.tsx index 3e450496..bf7b7a83 100644 --- a/packages/client/src/pages/index.tsx +++ b/packages/client/src/pages/index.tsx @@ -63,6 +63,7 @@ const RecentDocs = () => { ); }} />, + } />, } />, r.documentId); + const visitedAtMap = records.reduce((a, c) => { + return (a[c.documentId] = c.visitedAt); + }, {}); - const documents = await this.documentRepo.findByIds(documentIds, { order: { updatedAt: 'DESC' } }); + const documents = await this.documentRepo.findByIds(documentIds); const docs = documents.filter((doc) => !doc.isWikiHome).map((doc) => instanceToPlain(doc)); const res = await Promise.all( docs.map(async (doc) => { const views = await this.viewService.getDocumentTotalViews(doc.id); - return { ...doc, views } as IDocument & { views: number }; + return { ...doc, views, visitedAt: visitedAtMap[doc.id] } as IDocument & { views: number; visitedAt: Date }; }) ); diff --git a/packages/server/src/services/view.service.ts b/packages/server/src/services/view.service.ts index a4bae09c..dfc5edcb 100644 --- a/packages/server/src/services/view.service.ts +++ b/packages/server/src/services/view.service.ts @@ -55,11 +55,20 @@ export class ViewService { return { data, total }; } - async getUserRecentVisitedDocuments(userId: IUser['id']): Promise> { + async getUserRecentVisitedDocuments(userId: IUser['id']): Promise< + Array<{ + documentId: IDocument['id']; + visitedAt: Date; + }> + > { const [ret] = await this.viewRepo.findAndCount({ where: { userId }, take: 20, + order: { createdAt: 'DESC' }, }); - return ret.map((item) => item.documentId); + return ret.map((item) => ({ + documentId: item.documentId, + visitedAt: item.createdAt, + })); } }