mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-19 02:56:23 +08:00
refactor: public workspace (#996)
Co-authored-by: himself65 <himself65@outlook.com>
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { getDataCenter, WorkspaceUnit } from '@affine/datacenter';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export function useLoadPublicWorkspace(workspaceId: string) {
|
||||
const router = useRouter();
|
||||
const [workspace, setWorkspace] = useState<WorkspaceUnit | null>();
|
||||
const [status, setStatus] = useState<'loading' | 'error' | 'success'>(
|
||||
'loading'
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setStatus('loading');
|
||||
|
||||
const init = async () => {
|
||||
const dataCenter = await getDataCenter();
|
||||
|
||||
dataCenter
|
||||
.loadPublicWorkspace(workspaceId)
|
||||
.then(data => {
|
||||
setWorkspace(data);
|
||||
setStatus('success');
|
||||
})
|
||||
.catch(() => {
|
||||
// if (!cancel) {
|
||||
// router.push('/404');
|
||||
// }
|
||||
setStatus('error');
|
||||
});
|
||||
};
|
||||
init();
|
||||
}, [router, workspaceId]);
|
||||
|
||||
return { status, workspace };
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import { useAppState } from '@/providers/app-state-provider';
|
||||
import { WorkspaceUnit } from '@affine/datacenter';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export function usePublicWorkspace(workspaceId: string) {
|
||||
const { dataCenter } = useAppState();
|
||||
const router = useRouter();
|
||||
const [workspace, setWorkspace] = useState<WorkspaceUnit>();
|
||||
|
||||
useEffect(() => {
|
||||
let cancel = false;
|
||||
dataCenter
|
||||
.loadPublicWorkspace(workspaceId)
|
||||
.then(data => {
|
||||
if (!cancel) {
|
||||
setWorkspace(data);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancel) {
|
||||
router.push('/404');
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
cancel = true;
|
||||
};
|
||||
}, [router, workspaceId, dataCenter]);
|
||||
|
||||
return workspace;
|
||||
}
|
||||
Reference in New Issue
Block a user