fix(core): display user name dynamically (#4248)
This commit is contained in:
@@ -6,6 +6,8 @@ import type {
|
||||
import { initEmptyPage } from '@toeverything/infra/blocksuite';
|
||||
import { lazy, useCallback } from 'react';
|
||||
|
||||
import type { OnLoadEditor } from '../../components/page-detail-editor';
|
||||
import { useCurrentUser } from '../../hooks/affine/use-current-user';
|
||||
import { useIsWorkspaceOwner } from '../../hooks/affine/use-is-workspace-owner';
|
||||
import { useWorkspace } from '../../hooks/use-workspace';
|
||||
import {
|
||||
@@ -32,12 +34,28 @@ export const UI = {
|
||||
if (!page) {
|
||||
throw new PageNotFoundError(workspace.blockSuiteWorkspace, currentPageId);
|
||||
}
|
||||
// this should be safe because we are under cloud workspace adapter
|
||||
const currentUser = useCurrentUser();
|
||||
const onLoad = useCallback<OnLoadEditor>(
|
||||
(...args) => {
|
||||
const dispose = onLoadEditor(...args);
|
||||
workspace.blockSuiteWorkspace.awarenessStore.awareness.setLocalStateField(
|
||||
'user',
|
||||
{
|
||||
name: currentUser.name,
|
||||
}
|
||||
);
|
||||
return dispose;
|
||||
},
|
||||
[currentUser, workspace, onLoadEditor]
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageDetailEditor
|
||||
pageId={currentPageId}
|
||||
onInit={useCallback(async page => initEmptyPage(page), [])}
|
||||
onLoad={onLoadEditor}
|
||||
onLoad={onLoad}
|
||||
workspace={workspace.blockSuiteWorkspace}
|
||||
/>
|
||||
</>
|
||||
|
||||
@@ -35,12 +35,14 @@ import * as styles from './page-detail-editor.css';
|
||||
import { pluginContainer } from './page-detail-editor.css';
|
||||
import { TrashButtonGroup } from './pure/trash-button-group';
|
||||
|
||||
export type OnLoadEditor = (page: Page, editor: EditorContainer) => () => void;
|
||||
|
||||
export interface PageDetailEditorProps {
|
||||
isPublic?: boolean;
|
||||
workspace: Workspace;
|
||||
pageId: string;
|
||||
onInit: (page: Page, editor: Readonly<EditorContainer>) => void;
|
||||
onLoad?: (page: Page, editor: EditorContainer) => () => void;
|
||||
onLoad?: OnLoadEditor;
|
||||
}
|
||||
|
||||
const EditorWrapper = memo(function EditorWrapper({
|
||||
|
||||
@@ -16,7 +16,6 @@ import {
|
||||
getCurrentStore,
|
||||
} from '@toeverything/infra/atom';
|
||||
import { useAtomValue, useSetAtom } from 'jotai';
|
||||
import { getSession } from 'next-auth/react';
|
||||
import { type ReactElement, useCallback } from 'react';
|
||||
import type { LoaderFunction } from 'react-router-dom';
|
||||
import { redirect } from 'react-router-dom';
|
||||
@@ -139,11 +138,6 @@ export const loader: LoaderFunction = async args => {
|
||||
} else {
|
||||
return redirect('/404');
|
||||
}
|
||||
const session = await getSession();
|
||||
const name = session?.user.name;
|
||||
currentWorkspace.awarenessStore.awareness.setLocalStateField('user', {
|
||||
name: name ?? 'Anonymous User',
|
||||
});
|
||||
return null;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user