feat(core): append to today's joruanl via CMDK (#5692)

This commit is contained in:
Cats Juice
2024-01-30 23:08:25 +08:00
committed by GitHub
parent 329fc19852
commit 6b350b1735
4 changed files with 47 additions and 3 deletions

View File

@@ -5,7 +5,12 @@ import {
} from '@affine/core/hooks/use-block-suite-page-meta';
import type { Collection } from '@affine/env/filter';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { EdgelessIcon, PageIcon, ViewLayersIcon } from '@blocksuite/icons';
import {
EdgelessIcon,
PageIcon,
TodayIcon,
ViewLayersIcon,
} from '@blocksuite/icons';
import type { PageMeta } from '@blocksuite/store';
import { Workspace } from '@toeverything/infra';
import { getCurrentStore } from '@toeverything/infra/atom';
@@ -23,6 +28,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
import { pageSettingsAtom, recentPageIdsBaseAtom } from '../../../atoms';
import { currentPageIdAtom } from '../../../atoms/mode';
import { useJournalHelper } from '../../../hooks/use-journal';
import { useNavigateHelper } from '../../../hooks/use-navigate-helper';
import { CollectionService } from '../../../modules/collection';
import { WorkspaceSubPath } from '../../../shared';
@@ -161,6 +167,7 @@ export const usePageCommands = () => {
const pageMetaHelper = usePageMetaHelper(workspace.blockSuiteWorkspace);
const query = useAtomValue(cmdkQueryAtom);
const navigationHelper = useNavigateHelper();
const journalHelper = useJournalHelper(workspace.blockSuiteWorkspace);
const t = useAFFiNEI18N();
const [searchTime, setSearchTime] = useState<number>(0);
@@ -253,6 +260,24 @@ export const usePageCommands = () => {
// check if the pages have exact match. if not, we should show the "create page" command
if (results.every(command => command.originalValue !== query)) {
results.push({
id: 'affine:pages:append-to-journal',
label: t['com.affine.journal.cmdk.append-to-today'](),
value: 'affine::append-journal' + query, // hack to make the page always showing in the search result
category: 'affine:creation',
run: async () => {
const appendRes = await journalHelper.appendContentToToday(query);
if (!appendRes) return;
const { page, blockId } = appendRes;
navigationHelper.jumpToPageBlock(
page.workspace.id,
page.id,
blockId
);
},
icon: <TodayIcon />,
});
results.push({
id: 'affine:pages:create-page',
label: t['com.affine.cmdk.affine.create-new-page-as']({
@@ -294,6 +319,7 @@ export const usePageCommands = () => {
t,
workspace,
pages,
journalHelper,
pageHelper,
pageMetaHelper,
]);

View File

@@ -7,9 +7,9 @@ import * as styles from './not-found.css';
export const NotFoundGroup = () => {
const query = useAtomValue(cmdkQueryAtom);
// hack: we know that the filtered count is 2 when there is no result (create page & edgeless)
// hack: we know that the filtered count is 3 when there is no result (create page & edgeless & append to journal)
const renderNoResult =
useCommandState(state => state.filtered.count === 2) || false;
useCommandState(state => state.filtered.count === 3) || false;
if (!renderNoResult) {
return null;

View File

@@ -104,6 +104,21 @@ export const useJournalHelper = (workspace: BlockSuiteWorkspace) => {
[getJournalDateString]
);
const appendContentToToday = useCallback(
async (content: string) => {
if (!content) return;
const page = getJournalByDate(dayjs().format(JOURNAL_DATE_FORMAT));
if (!page) return;
const blockId = page.addBlock(
'affine:paragraph',
{ text: new page.Text(content) },
page.getBlockByFlavour('affine:note')[0].id
);
return { page, blockId };
},
[getJournalByDate]
);
return useMemo(
() => ({
getJournalsByDate,
@@ -112,6 +127,7 @@ export const useJournalHelper = (workspace: BlockSuiteWorkspace) => {
getLocalizedJournalDateString,
isPageJournal,
isPageTodayJournal,
appendContentToToday,
}),
[
getJournalByDate,
@@ -120,6 +136,7 @@ export const useJournalHelper = (workspace: BlockSuiteWorkspace) => {
getLocalizedJournalDateString,
isPageJournal,
isPageTodayJournal,
appendContentToToday,
]
);
};

View File

@@ -1069,5 +1069,6 @@
"com.affine.journal.daily-count-updated-empty-tips": "You haven't updated anything yet",
"com.affine.journal.conflict-show-more": "{{count}} more articles",
"com.affine.journal.app-sidebar-title": "Journals",
"com.affine.journal.cmdk.append-to-today": "Append to Journal",
"com.affine.editor.reference-not-found": "Linked page not found"
}