mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-25 18:26:05 +08:00
feat(core): create and open cloud workspace if not exists after logged in (#6511)
Only execute when `initCloud=true` is specified in the URL search params.
This commit is contained in:
@@ -130,10 +130,13 @@ export function useNavigateHelper() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const jumpToIndex = useCallback(
|
const jumpToIndex = useCallback(
|
||||||
(logic: RouteLogic = RouteLogic.PUSH) => {
|
(logic: RouteLogic = RouteLogic.PUSH, opt?: { search?: string }) => {
|
||||||
return navigate('/', {
|
return navigate(
|
||||||
replace: logic === RouteLogic.REPLACE,
|
{ pathname: '/', search: opt?.search },
|
||||||
});
|
{
|
||||||
|
replace: logic === RouteLogic.REPLACE,
|
||||||
|
}
|
||||||
|
);
|
||||||
},
|
},
|
||||||
[navigate]
|
[navigate]
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,16 +1,26 @@
|
|||||||
import { Menu } from '@affine/component/ui/menu';
|
import { Menu } from '@affine/component/ui/menu';
|
||||||
|
import { WorkspaceFlavour } from '@affine/env/workspace';
|
||||||
import {
|
import {
|
||||||
|
initEmptyPage,
|
||||||
useLiveData,
|
useLiveData,
|
||||||
useService,
|
useService,
|
||||||
WorkspaceListService,
|
WorkspaceListService,
|
||||||
WorkspaceManager,
|
WorkspaceManager,
|
||||||
} from '@toeverything/infra';
|
} from '@toeverything/infra';
|
||||||
import { lazy, useEffect, useLayoutEffect, useState } from 'react';
|
import {
|
||||||
import type { LoaderFunction } from 'react-router-dom';
|
lazy,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useLayoutEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from 'react';
|
||||||
|
import { type LoaderFunction, useSearchParams } from 'react-router-dom';
|
||||||
|
|
||||||
import { createFirstAppData } from '../bootstrap/first-app-data';
|
import { createFirstAppData } from '../bootstrap/first-app-data';
|
||||||
import { UserWithWorkspaceList } from '../components/pure/workspace-slider-bar/user-with-workspace-list';
|
import { UserWithWorkspaceList } from '../components/pure/workspace-slider-bar/user-with-workspace-list';
|
||||||
import { WorkspaceFallback } from '../components/workspace';
|
import { WorkspaceFallback } from '../components/workspace';
|
||||||
|
import { useSession } from '../hooks/affine/use-current-user';
|
||||||
import { useNavigateHelper } from '../hooks/use-navigate-helper';
|
import { useNavigateHelper } from '../hooks/use-navigate-helper';
|
||||||
import { WorkspaceSubPath } from '../shared';
|
import { WorkspaceSubPath } from '../shared';
|
||||||
|
|
||||||
@@ -28,12 +38,48 @@ export const Component = () => {
|
|||||||
// navigating and creating may be slow, to avoid flickering, we show workspace fallback
|
// navigating and creating may be slow, to avoid flickering, we show workspace fallback
|
||||||
const [navigating, setNavigating] = useState(false);
|
const [navigating, setNavigating] = useState(false);
|
||||||
const [creating, setCreating] = useState(false);
|
const [creating, setCreating] = useState(false);
|
||||||
|
const { status } = useSession();
|
||||||
|
const workspaceManager = useService(WorkspaceManager);
|
||||||
|
|
||||||
const list = useLiveData(useService(WorkspaceListService).workspaceList$);
|
const workspaceListService = useService(WorkspaceListService);
|
||||||
|
const list = useLiveData(workspaceListService.workspaceList$);
|
||||||
|
const workspaceListStatus = useLiveData(workspaceListService.status$);
|
||||||
|
|
||||||
const { openPage } = useNavigateHelper();
|
const { openPage } = useNavigateHelper();
|
||||||
|
const [searchParams] = useSearchParams();
|
||||||
|
|
||||||
|
const createOnceRef = useRef(false);
|
||||||
|
|
||||||
|
const createCloudWorkspace = useCallback(() => {
|
||||||
|
if (createOnceRef.current) return;
|
||||||
|
createOnceRef.current = true;
|
||||||
|
workspaceManager
|
||||||
|
.createWorkspace(WorkspaceFlavour.AFFINE_CLOUD, async workspace => {
|
||||||
|
workspace.meta.setName('AFFiNE Cloud');
|
||||||
|
const page = workspace.createDoc();
|
||||||
|
initEmptyPage(page);
|
||||||
|
})
|
||||||
|
.then(workspace => openPage(workspace.id, WorkspaceSubPath.ALL))
|
||||||
|
.catch(err => console.error('Failed to create cloud workspace', err));
|
||||||
|
}, [openPage, workspaceManager]);
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
|
if (workspaceListStatus.loading) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check is user logged in && has cloud workspace
|
||||||
|
if (
|
||||||
|
searchParams.get('initCloud') === 'true' &&
|
||||||
|
status === 'authenticated'
|
||||||
|
) {
|
||||||
|
searchParams.delete('initCloud');
|
||||||
|
if (list.every(w => w.flavour !== WorkspaceFlavour.AFFINE_CLOUD)) {
|
||||||
|
createCloudWorkspace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (list.length === 0) {
|
if (list.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -44,9 +90,14 @@ export const Component = () => {
|
|||||||
const openWorkspace = list.find(w => w.id === lastId) ?? list[0];
|
const openWorkspace = list.find(w => w.id === lastId) ?? list[0];
|
||||||
openPage(openWorkspace.id, WorkspaceSubPath.ALL);
|
openPage(openWorkspace.id, WorkspaceSubPath.ALL);
|
||||||
setNavigating(true);
|
setNavigating(true);
|
||||||
}, [list, openPage]);
|
}, [
|
||||||
|
createCloudWorkspace,
|
||||||
const workspaceManager = useService(WorkspaceManager);
|
list,
|
||||||
|
openPage,
|
||||||
|
searchParams,
|
||||||
|
status,
|
||||||
|
workspaceListStatus.loading,
|
||||||
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setCreating(true);
|
setCreating(true);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { SignInPageContainer } from '@affine/component/auth-components';
|
|||||||
import { useAtom } from 'jotai';
|
import { useAtom } from 'jotai';
|
||||||
import { useCallback, useEffect, useRef } from 'react';
|
import { useCallback, useEffect, useRef } from 'react';
|
||||||
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
||||||
import { useLocation, useNavigate } from 'react-router-dom';
|
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
|
||||||
|
|
||||||
import { authAtom } from '../atoms';
|
import { authAtom } from '../atoms';
|
||||||
import type { AuthProps } from '../components/affine/auth';
|
import type { AuthProps } from '../components/affine/auth';
|
||||||
@@ -27,6 +27,7 @@ export const SignIn = () => {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { jumpToIndex } = useNavigateHelper();
|
const { jumpToIndex } = useNavigateHelper();
|
||||||
const subscriptionData = useSubscriptionSearch();
|
const subscriptionData = useSubscriptionSearch();
|
||||||
|
const [searchParams] = useSearchParams();
|
||||||
|
|
||||||
const isLoggedIn = loginStatus === 'authenticated';
|
const isLoggedIn = loginStatus === 'authenticated';
|
||||||
|
|
||||||
@@ -49,7 +50,9 @@ export const SignIn = () => {
|
|||||||
replace: true,
|
replace: true,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
jumpToIndex(RouteLogic.REPLACE);
|
jumpToIndex(RouteLogic.REPLACE, {
|
||||||
|
search: searchParams.toString(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
@@ -59,6 +62,7 @@ export const SignIn = () => {
|
|||||||
setAuthAtom,
|
setAuthAtom,
|
||||||
subscriptionData,
|
subscriptionData,
|
||||||
isLoggedIn,
|
isLoggedIn,
|
||||||
|
searchParams,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const onSetEmailType = useCallback(
|
const onSetEmailType = useCallback(
|
||||||
|
|||||||
Reference in New Issue
Block a user