diff --git a/packages/client/src/data/document.ts b/packages/client/src/data/document.ts index 72cfe11..78d0621 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 3e45049..bf7b7a8 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 a4bae09..dfc5edc 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, + })); } }