Merge branch 'feat/datacenter-dev' of https://github.com/toeverything/AFFiNE into feat/datacenter-dev

This commit is contained in:
DiamondThree
2023-01-11 17:14:05 +08:00
20 changed files with 54 additions and 119 deletions

View File

@@ -4,6 +4,20 @@ import { Tooltip } from '@/ui/tooltip';
import { ArrowDownIcon } from '@blocksuite/icons';
import { useModal } from '@/providers/GlobalModalProvider';
import { useTranslation } from '@affine/i18n';
import { styled } from '@/styles';
const StyledIconButtonWithAnimate = styled(IconButton)(() => {
return {
svg: {
transition: 'transform 0.15s ease-in-out',
},
':hover': {
svg: {
transform: 'translateY(3px)',
},
},
};
});
export const QuickSearchButton = ({
onClick,
...props
@@ -12,7 +26,7 @@ export const QuickSearchButton = ({
const { t } = useTranslation();
return (
<Tooltip content={t('Switch to')} placement="bottom">
<IconButton
<StyledIconButtonWithAnimate
data-testid="header-quickSearchButton"
{...props}
onClick={e => {
@@ -21,7 +35,7 @@ export const QuickSearchButton = ({
}}
>
<ArrowDownIcon />
</IconButton>
</StyledIconButtonWithAnimate>
</Tooltip>
);
};

View File

@@ -8,7 +8,7 @@ import { useTranslation } from '@affine/i18n';
export const TrashButtonGroup = () => {
const { permanentlyDeletePage } = usePageHelper();
const { currentWorkspaceId } = useAppState();
const { currentWorkspace } = useAppState();
const { toggleDeletePage } = usePageHelper();
const { confirm } = useConfirm();
const router = useRouter();
@@ -38,7 +38,7 @@ export const TrashButtonGroup = () => {
confirmType: 'danger',
}).then(confirm => {
if (confirm) {
router.push(`/workspace/${currentWorkspaceId}/all`);
router.push(`/workspace/${currentWorkspace?.id}/all`);
permanentlyDeletePage(id);
}
});

View File

@@ -73,7 +73,7 @@ export const PageList = ({
isTrash?: boolean;
}) => {
const router = useRouter();
const { currentWorkspaceId } = useAppState();
const { currentWorkspace } = useAppState();
const { t } = useTranslation();
if (pageList.length === 0) {
return <Empty />;
@@ -99,7 +99,7 @@ export const PageList = ({
key={`${pageMeta.id}-${index}`}
onClick={() => {
router.push(
`/workspace/${currentWorkspaceId}/${pageMeta.id}`
`/workspace/${currentWorkspace?.id}/${pageMeta.id}`
);
}}
>

View File

@@ -17,10 +17,8 @@ export const Input = (props: {
const [isComposition, setIsComposition] = useState(false);
const [inputValue, setInputValue] = useState('');
const inputRef = useRef<HTMLInputElement>(null);
const { currentWorkspaceId, workspaceList, currentWorkspace } = useAppState();
const isPublish = workspaceList.find(
meta => String(meta.id) === String(currentWorkspaceId)
)?.published;
const { currentWorkspace } = useAppState();
useEffect(() => {
inputRef.current?.addEventListener(
'blur',
@@ -79,7 +77,7 @@ export const Input = (props: {
}
}}
placeholder={
isPublish
currentWorkspace?.isPublish
? `Search in ${currentWorkspace?.blocksuiteWorkspace?.meta.name}`
: 'Quick Search...'
}

View File

@@ -22,9 +22,9 @@ export const Results = (props: {
const { triggerQuickSearchModal } = useModal();
const { openPage } = usePageHelper();
const router = useRouter();
const { currentWorkspaceId, pageList } = useAppState();
const { currentWorkspace, pageList } = useAppState();
const { search } = usePageHelper();
const List = useSwitchToConfig(currentWorkspaceId);
const List = useSwitchToConfig(currentWorkspace?.id);
const [results, setResults] = useState(new Map<string, string | undefined>());
const { t } = useTranslation();
useEffect(() => {

View File

@@ -1,15 +1,16 @@
import { FC, SVGProps } from 'react';
import { AllPagesIcon, FavouritesIcon, TrashIcon } from '@blocksuite/icons';
import { useTranslation } from '@affine/i18n';
export const useSwitchToConfig = (
currentWorkspaceId: string
currentWorkspaceId?: string
): {
title: string;
href: string;
icon: React.FC<React.SVGProps<SVGSVGElement>>;
icon: FC<SVGProps<SVGSVGElement>>;
}[] => {
const { t } = useTranslation();
const List = [
return [
{
title: t('All pages'),
href: currentWorkspaceId ? `/workspace/${currentWorkspaceId}/all` : '',
@@ -28,5 +29,4 @@ export const useSwitchToConfig = (
icon: TrashIcon,
},
];
return List;
};

View File

@@ -22,15 +22,13 @@ const isMac = () => {
return getUaHelper().isMacOs;
};
export const QuickSearch = ({ open, onClose }: TransitionsModalProps) => {
const { currentMetaWorkSpace } = useAppState();
const { currentWorkspace } = useAppState();
const [query, setQuery] = useState('');
const [loading, setLoading] = useState(true);
const [showCreatePage, setShowCreatePage] = useState(true);
const { triggerQuickSearchModal } = useModal();
const isPublish = currentMetaWorkSpace?.published;
// Add ‘⌘+K shortcut keys as switches
useEffect(() => {
const down = (e: KeyboardEvent) => {
@@ -96,7 +94,7 @@ export const QuickSearch = ({ open, onClose }: TransitionsModalProps) => {
setShowCreatePage={setShowCreatePage}
/>
</StyledContent>
{isPublish ? (
{currentWorkspace?.isPublish ? (
<></>
) : showCreatePage ? (
<>

View File

@@ -34,8 +34,6 @@ import { useConfirm } from '@/providers/ConfirmProvider';
export const MembersPage = ({ workspace }: { workspace: WorkspaceUnit }) => {
const [isInviteModalShow, setIsInviteModalShow] = useState(false);
// const { currentMetaWorkSpace, currentWorkspace, dataCenter } = useAppState();
const [members, setMembers] = useState<[{ name: string; email: string }?]>(
[]
);

View File

@@ -41,7 +41,6 @@ export const GeneralPage = ({ workspace }: { workspace: WorkspaceUnit }) => {
setShowLeave(false);
};
const handleUpdateWorkspaceName = () => {
console.log('currentWorkspace: ', currentWorkspace);
currentWorkspace &&
updateWorkspace({ name: workspaceName }, currentWorkspace);
};

View File

@@ -5,7 +5,7 @@ import { WorkspaceAvatar } from '@/components/workspace-avatar';
import { useAppState } from '@/providers/app-state-provider';
export const WorkspaceSelector = () => {
const [workspaceListShow, setWorkspaceListShow] = useState(false);
const { currentMetaWorkSpace, workspaceList } = useAppState();
const { currentWorkspace, workspaceList } = useAppState();
useEffect(() => {
if (workspaceList.length === 0) {
@@ -23,7 +23,7 @@ export const WorkspaceSelector = () => {
<Avatar
alt="Affine"
data-testid="workspace-avatar"
src={currentMetaWorkSpace?.avatar}
src={currentWorkspace?.avatar}
>
<div
style={{
@@ -32,13 +32,13 @@ export const WorkspaceSelector = () => {
>
<WorkspaceAvatar
size={28}
name={currentMetaWorkSpace?.name ?? 'AFFiNE'}
avatar={currentMetaWorkSpace?.avatar ?? ''}
name={currentWorkspace?.name ?? 'AFFiNE'}
avatar={currentWorkspace?.avatar ?? ''}
/>
</div>
</Avatar>
<WorkspaceName data-testid="workspace-name">
{currentMetaWorkSpace?.name ?? 'AFFiNE'}
{currentWorkspace?.name ?? 'AFFiNE'}
</WorkspaceName>
</SelectorWrapper>
<WorkspaceModal

View File

@@ -67,13 +67,13 @@ const FavoriteList = ({ showList }: { showList: boolean }) => {
export const WorkSpaceSliderBar = () => {
const { triggerQuickSearchModal, triggerImportModal } = useModal();
const [showSubFavorite, setShowSubFavorite] = useState(true);
const { currentWorkspaceId } = useAppState();
const { currentWorkspace } = useAppState();
const { openPage, createPage } = usePageHelper();
const router = useRouter();
const { t } = useTranslation();
const [showTip, setShowTip] = useState(false);
const [show, setShow] = useLocalStorage('AFFiNE_SLIDE_BAR', false, true);
const currentWorkspaceId = currentWorkspace?.id;
const paths = {
all: currentWorkspaceId ? `/workspace/${currentWorkspaceId}/all` : '',
favorite: currentWorkspaceId

View File

@@ -1,34 +0,0 @@
import { useRouter } from 'next/router';
import { useAppState } from '@/providers/app-state-provider';
import { useEffect, useRef, useState } from 'react';
export const useInitWorkspace = (disabled?: boolean) => {
const [loading, setLoading] = useState(true);
// Do not set as a constant, or it will trigger a hell of re-rendering
const defaultOutLineWorkspaceId = useRef(new Date().getTime().toString());
const router = useRouter();
const { workspaceList, loadWorkspace, currentWorkspace, currentWorkspaceId } =
useAppState();
const workspaceId =
(router.query.workspaceId as string) ||
workspaceList?.[0]?.id ||
defaultOutLineWorkspaceId.current;
useEffect(() => {
if (disabled) {
setLoading(false);
return;
}
setLoading(true);
loadWorkspace(workspaceId).finally(() => {
setLoading(false);
});
}, [workspaceId, disabled, loadWorkspace]);
return {
workspaceId,
workspace: workspaceId === currentWorkspaceId ? currentWorkspace : null,
loading,
};
};

View File

@@ -36,7 +36,7 @@ const getPageMeta = (workspace: WorkspaceUnit | null, pageId: string) => {
export const usePageHelper = (): EditorHandlers => {
const router = useRouter();
const changePageMeta = useChangePageMeta();
const { currentWorkspace, editor, currentWorkspaceId } = useAppState();
const { currentWorkspace, editor } = useAppState();
return {
createPage: ({
@@ -109,11 +109,11 @@ export const usePageHelper = (): EditorHandlers => {
pageId = pageId.replace('space:', '');
if (newTab) {
window.open(`/workspace/${currentWorkspaceId}/${pageId}`, '_blank');
window.open(`/workspace/${currentWorkspace?.id}/${pageId}`, '_blank');
return Promise.resolve(true);
}
return router.push({
pathname: `/workspace/${currentWorkspaceId}/${pageId}`,
pathname: `/workspace/${currentWorkspace?.id}/${pageId}`,
query,
});
},

View File

@@ -6,8 +6,7 @@ import router from 'next/router';
export const useWorkspaceHelper = () => {
const { confirm } = useConfirm();
const { dataCenter, currentWorkspace, user, login, currentMetaWorkSpace } =
useAppState();
const { dataCenter, currentWorkspace, user, login } = useAppState();
const createWorkspace = async (name: string) => {
const workspaceInfo = await dataCenter.createWorkspace({
name: name,
@@ -58,8 +57,8 @@ export const useWorkspaceHelper = () => {
};
const inviteMember = async (email: string) => {
currentMetaWorkSpace &&
(await dataCenter.inviteMember(currentMetaWorkSpace?.id, email));
currentWorkspace &&
(await dataCenter.inviteMember(currentWorkspace?.id, email));
};
return {

View File

@@ -7,7 +7,7 @@ import usePageHelper from '@/hooks/use-page-helper';
const WorkspaceIndex = () => {
const router = useRouter();
const { currentWorkspaceId, currentWorkspace } = useAppState();
const { currentWorkspace } = useAppState();
const { createPage } = usePageHelper();
const { workspaceLoaded, activeWorkspaceId } = useEnsureWorkspace();
@@ -29,7 +29,6 @@ const WorkspaceIndex = () => {
initPage();
}, [
currentWorkspace,
currentWorkspaceId,
createPage,
router,
workspaceLoaded,

View File

@@ -60,7 +60,7 @@ const tabMap: {
];
const WorkspaceSetting = () => {
const { currentMetaWorkSpace } = useAppState();
const { currentWorkspace } = useAppState();
const [activeTab, setActiveTab] = useState<TabNames>(tabMap[0].name);
const handleTabChange = (tab: TabNames) => {
@@ -98,7 +98,7 @@ const WorkspaceSetting = () => {
</StyledSettingSidebar>
<StyledSettingContent>
{currentMetaWorkSpace && activeTabPanelRender?.(currentMetaWorkSpace)}
{currentWorkspace && activeTabPanelRender?.(currentWorkspace)}
</StyledSettingContent>
</StyledSettingContainer>
);

View File

@@ -6,14 +6,14 @@ import { PageLoading } from '@/components/loading';
export const WorkspaceIndex = () => {
const router = useRouter();
const { currentWorkspaceId } = useAppState();
const { currentWorkspace } = useAppState();
const { workspaceLoaded } = useEnsureWorkspace();
useEffect(() => {
if (workspaceLoaded) {
router.push(`/workspace/${currentWorkspaceId}`);
router.push(`/workspace/${currentWorkspace?.id}`);
}
}, [currentWorkspaceId, router, workspaceLoaded]);
}, [currentWorkspace, router, workspaceLoaded]);
return <PageLoading />;
};

View File

@@ -8,7 +8,7 @@ import {
PageMeta,
} from './interface';
import { createDefaultWorkspace } from './utils';
import { WorkspaceUnit, User } from '@affine/datacenter';
import { User } from '@affine/datacenter';
type AppStateContextProps = PropsWithChildren<Record<string, unknown>>;
@@ -34,13 +34,11 @@ export const AppStateProvider = ({
dataCenter,
user: (await dataCenter.getUserInfo()) || null,
workspaceList: dataCenter.workspaces,
currentWorkspaceId: '',
currentWorkspace: null,
pageList: [],
currentPage: null,
editor: null,
synced: true,
currentMetaWorkSpace: null,
});
};
@@ -95,27 +93,19 @@ export const AppStateProvider = ({
const loadWorkspace = useRef<AppStateFunction['loadWorkspace']>();
loadWorkspace.current = async (workspaceId: string) => {
const { dataCenter, workspaceList, currentWorkspaceId, currentWorkspace } =
appState;
const { dataCenter, workspaceList, currentWorkspace } = appState;
if (!workspaceList.find(v => v.id.toString() === workspaceId)) {
return null;
}
if (workspaceId === currentWorkspaceId) {
if (workspaceId === currentWorkspace?.id) {
return currentWorkspace;
}
const workspace = await dataCenter.loadWorkspace(workspaceId);
const currentMetaWorkSpace = dataCenter.workspaces.find(
(item: WorkspaceUnit) => {
return item.id.toString() === workspace.id;
}
);
const workspace = (await dataCenter.loadWorkspace(workspaceId)) ?? null;
const pageList =
(workspace?.blocksuiteWorkspace?.meta.pageMetas as PageMeta[]) ?? [];
setAppState({
...appState,
currentWorkspace: workspace,
currentWorkspaceId: workspaceId,
currentMetaWorkSpace: currentMetaWorkSpace ?? null,
pageList: pageList,
currentPage: null,
editor: null,

View File

@@ -19,8 +19,6 @@ export type AppStateValue = {
user: User | null;
workspaceList: WorkspaceUnit[];
currentWorkspace: WorkspaceUnit | null;
currentMetaWorkSpace: WorkspaceUnit | null;
currentWorkspaceId: string;
pageList: PageMeta[];
currentPage: StorePage | null;
editor?: EditorContainer | null;