mirror of https://github.com/fantasticit/think.git
fix: fix recent visited records
This commit is contained in:
parent
27bc13646f
commit
498b73608a
|
@ -1,5 +1,6 @@
|
||||||
import type { IAuthority, IDocument, IUser, IWiki } from '@think/domains';
|
import type { IAuthority, IDocument, IUser, IWiki } from '@think/domains';
|
||||||
import { useAsyncLoading } from 'hooks/use-async-loading';
|
import { useAsyncLoading } from 'hooks/use-async-loading';
|
||||||
|
import { string } from 'lib0';
|
||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import { getPublicDocumentDetail } from 'services/document';
|
import { getPublicDocumentDetail } from 'services/document';
|
||||||
import { HttpClient } from 'services/http-client';
|
import { HttpClient } from 'services/http-client';
|
||||||
|
@ -94,7 +95,9 @@ export const useDocumentVersion = (documentId) => {
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const useRecentDocuments = () => {
|
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;
|
const loading = !data && !error;
|
||||||
return { data, error, loading, refresh: mutate };
|
return { data, error, loading, refresh: mutate };
|
||||||
};
|
};
|
||||||
|
|
|
@ -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="更新时间" dataIndex="updatedAt" key="updatedAt" render={(date) => <LocaleTime date={date} />} />,
|
||||||
<Column
|
<Column
|
||||||
title="操作"
|
title="操作"
|
||||||
|
|
|
@ -606,15 +606,19 @@ export class DocumentService {
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
public async getRecentDocuments(user: OutUser) {
|
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 docs = documents.filter((doc) => !doc.isWikiHome).map((doc) => instanceToPlain(doc));
|
||||||
|
|
||||||
const res = await Promise.all(
|
const res = await Promise.all(
|
||||||
docs.map(async (doc) => {
|
docs.map(async (doc) => {
|
||||||
const views = await this.viewService.getDocumentTotalViews(doc.id);
|
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 };
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -55,11 +55,20 @@ export class ViewService {
|
||||||
return { data, total };
|
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({
|
const [ret] = await this.viewRepo.findAndCount({
|
||||||
where: { userId },
|
where: { userId },
|
||||||
take: 20,
|
take: 20,
|
||||||
|
order: { createdAt: 'DESC' },
|
||||||
});
|
});
|
||||||
return ret.map((item) => item.documentId);
|
return ret.map((item) => ({
|
||||||
|
documentId: item.documentId,
|
||||||
|
visitedAt: item.createdAt,
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue