fix: fix recent visited records

This commit is contained in:
fantasticit 2022-05-21 22:34:07 +08:00
parent 27bc13646f
commit 498b73608a
4 changed files with 23 additions and 6 deletions

View File

@ -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<IDocument[]>('/document/recent', (url) => HttpClient.get(url));
const { data, error, mutate } = useSWR<Array<IDocument & { visitedAt: string }>>('/document/recent', (url) =>
HttpClient.get(url)
);
const loading = !data && !error;
return { data, error, loading, refresh: mutate };
};

View File

@ -63,6 +63,7 @@ const RecentDocs = () => {
);
}}
/>,
<Column title="访问时间" dataIndex="visitedAt" key="visitedAt" render={(date) => <LocaleTime date={date} />} />,
<Column title="更新时间" dataIndex="updatedAt" key="updatedAt" render={(date) => <LocaleTime date={date} />} />,
<Column
title="操作"

View File

@ -606,15 +606,19 @@ export class DocumentService {
* @returns
*/
public async getRecentDocuments(user: OutUser) {
const documentIds = await this.viewService.getUserRecentVisitedDocuments(user.id);
const records = await this.viewService.getUserRecentVisitedDocuments(user.id);
const documentIds = records.map((r) => 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 };
})
);

View File

@ -55,11 +55,20 @@ export class ViewService {
return { data, total };
}
async getUserRecentVisitedDocuments(userId: IUser['id']): Promise<Array<IDocument['id']>> {
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,
}));
}
}