feat: switch workspace

This commit is contained in:
DiamondThree 2023-01-09 20:15:05 +08:00
parent bcb8b9f211
commit 9ef0fe9d06
4 changed files with 18 additions and 15 deletions

View File

@ -13,19 +13,19 @@ import {
import { useConfirm } from '@/providers/ConfirmProvider';
import { toast } from '@/ui/toast';
import { WorkspaceAvatar } from '@/components/workspace-avatar';
// import { useTemporaryHelper } from '@/providers/temporary-helper-provider';
import { useAppState } from '@/providers/app-state-provider';
import { useRouter } from 'next/router';
interface LoginModalProps {
interface WorkspaceModalProps {
open: boolean;
onClose: () => void;
}
export const WorkspaceModal = ({ open, onClose }: LoginModalProps) => {
export const WorkspaceModal = ({ open, onClose }: WorkspaceModalProps) => {
const [createWorkspaceOpen, setCreateWorkspaceOpen] = useState(false);
const { confirm } = useConfirm();
const { user, workspaceList, loadWorkspace } = useAppState();
const { user, workspaceList } = useAppState();
const router = useRouter();
return (
<div>
<Modal open={open} onClose={onClose}>
@ -50,9 +50,7 @@ export const WorkspaceModal = ({ open, onClose }: LoginModalProps) => {
return (
<WorkspaceItem
onClick={() => {
// setActiveWorkspace(item);
console.log(item);
loadWorkspace(item.id);
router.replace(`/workspace/${item.id}`);
onClose();
}}
key={index}

View File

@ -2,7 +2,6 @@ import { Avatar, WorkspaceName, SelectorWrapper } from './styles';
import { useEffect, useState } from 'react';
import { WorkspaceModal } from '@/components/workspace-modal';
import { WorkspaceAvatar } from '@/components/workspace-avatar';
import { useTemporaryHelper } from '@/providers/temporary-helper-provider';
import { useAppState } from '@/providers/app-state-provider';
export const WorkspaceSelector = () => {
const [workspaceListShow, setWorkspaceListShow] = useState(false);

View File

@ -43,10 +43,12 @@ export const AppStateProvider = ({
if (dataCenter.workspaces.length === 0) {
await createDefaultWorkspace(dataCenter);
}
const currentWorkspace = await dataCenter.loadWorkspace(
let currentWorkspace = appState.currentWorkspace;
if (!currentWorkspace) {
currentWorkspace = await dataCenter.loadWorkspace(
dataCenter.workspaces[0].id
);
}
setAppState({
dataCenter,
@ -104,6 +106,7 @@ export const AppStateProvider = ({
};
const loadWorkspace = async (workspaceId: string) => {
console.log('workspaceId: ', workspaceId);
const { dataCenter, workspaceList, currentWorkspaceId } = appState;
if (!workspaceList.find(v => v.id === workspaceId)) {
return;
@ -111,6 +114,8 @@ export const AppStateProvider = ({
if (workspaceId === currentWorkspaceId) {
return;
}
const workspace = await dataCenter.loadWorkspace(workspaceId);
console.log('workspace: ', workspace);
setAppState({
...appState,

View File

@ -25,8 +25,9 @@ export class LocalProvider extends BaseProvider {
public override async linkLocal(workspace: Workspace) {
assert(workspace.room);
let idb = this._idbMap.get(workspace.room);
idb?.destroy();
if (!idb) {
idb = new IndexedDBProvider(workspace.room, workspace.doc);
}
this._idbMap.set(workspace.room, idb);
this._logger('Local data loaded');
return workspace;