Merge pull request #94 from toeverything/fixbug-issue76-mitsuha-dev

fixbug: [issue76]when click activities button, throw error;
This commit is contained in:
mitsuhatu 2022-08-04 19:07:53 +08:00 committed by GitHub
commit 1f109d62bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 36 deletions

View File

@ -33,30 +33,8 @@ export function Page(props: PageProps) {
const { page_id } = useParams();
const { showSpaceSidebar, fixedDisplay, setSpaceSidebarVisible } =
useShowSpaceSidebar();
const { user } = useUserAndSpaces();
const dailyNotesFlag = useFlag('BooleanDailyNotes', false);
useEffect(() => {
if (!user?.id || !page_id) return;
const updateRecentPages = async () => {
// TODO: deal with it temporarily
await services.api.editorBlock.getWorkspaceDbBlock(
props.workspace,
{
userId: user.id,
}
);
await services.api.userConfig.addRecentPage(
props.workspace,
user.id,
page_id
);
await services.api.editorBlock.clearUndoRedo(props.workspace);
};
updateRecentPages();
}, [user, props.workspace, page_id]);
return (
<LigoApp>
<LigoLeftContainer style={{ width: fixedDisplay ? '300px' : 0 }}>

View File

@ -64,30 +64,32 @@ export const Activities = () => {
const [recentPages, setRecentPages] = useState([]);
const userId = user?.id;
/* temporarily remove:show recently viewed documents */
const fetchRecentPages = useCallback(async () => {
/* show recently edit documents */
const getRecentEditPages = useCallback(async () => {
if (!userId || !currentSpaceId) {
return;
}
const recent_pages = await services.api.userConfig.getRecentPages(
currentSpaceId,
userId
);
setRecentPages(recent_pages);
}, [userId, currentSpaceId]);
const recentEditPages =
(await services.api.userConfig.getRecentEditedPages(
currentSpaceId
)) || [];
setRecentPages(recentEditPages);
}, [currentSpaceId, userId]);
useEffect(() => {
(async () => {
await fetchRecentPages();
await getRecentEditPages();
})();
}, [fetchRecentPages]);
}, [getRecentEditPages]);
useEffect(() => {
let unobserve: () => void;
const observe = async () => {
unobserve = await services.api.userConfig.observe(
{ workspace: currentSpaceId },
fetchRecentPages
getRecentEditPages
);
};
observe();
@ -95,12 +97,13 @@ export const Activities = () => {
return () => {
unobserve?.();
};
}, [currentSpaceId, fetchRecentPages]);
}, [currentSpaceId, getRecentEditPages]);
return (
<StyledWrapper>
<List style={{ padding: '0px' }}>
{recentPages.map(({ id, title, lastOpenTime }) => {
{recentPages.map(item => {
const { id, title, updated } = item;
return (
<ListItem className="item" key={id}>
<StyledItemContent
@ -114,7 +117,7 @@ export const Activities = () => {
/>
<ListItemText
className="itemRight"
primary={formatDistanceToNow(lastOpenTime, {
primary={formatDistanceToNow(updated, {
includeSeconds: true,
})}
/>

View File

@ -3,6 +3,7 @@ import { ServiceBaseClass } from '../base';
import { ObserveCallback, ReturnUnobserve } from '../database';
import { PageTree } from './page-tree';
import { PageConfigItem } from './types';
import type { QueryIndexMetadata } from '@toeverything/datasource/jwt';
/** Operate the user configuration at the workspace level */
export class UserConfig extends ServiceBaseClass {
@ -122,4 +123,20 @@ export class UserConfig extends ServiceBaseClass {
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
workspaceDbBlock.setDecoration(WORKSPACE_CONFIG, workspaceName);
}
async getRecentEditedPages(workspace: string) {
const db = await this.database.getDatabase(workspace);
const recentEditedPages =
(await db.queryBlocks({
$sort: 'lastUpdated',
$desc: false /* sort rule: true(default)(ASC), or false(DESC) */,
$limit: 4,
flavor: 'page',
} as QueryIndexMetadata)) || [];
return recentEditedPages.map(item => {
item['title'] = item.content || 'Untitled';
return item;
});
}
}