feat: change workspaceInfo type

This commit is contained in:
DiamondThree 2023-01-10 21:59:09 +08:00
parent c4a484bbce
commit 30679763ab
15 changed files with 35 additions and 158 deletions

View File

@ -20,7 +20,7 @@ export const Input = (props: {
const { currentWorkspaceId, workspaceList, currentWorkspace } = useAppState(); const { currentWorkspaceId, workspaceList, currentWorkspace } = useAppState();
const isPublish = workspaceList.find( const isPublish = workspaceList.find(
meta => String(meta.id) === String(currentWorkspaceId) meta => String(meta.id) === String(currentWorkspaceId)
)?.isPublish; )?.published;
useEffect(() => { useEffect(() => {
inputRef.current?.addEventListener( inputRef.current?.addEventListener(
'blur', 'blur',

View File

@ -29,7 +29,7 @@ export const QuickSearch = ({ open, onClose }: TransitionsModalProps) => {
const [showCreatePage, setShowCreatePage] = useState(true); const [showCreatePage, setShowCreatePage] = useState(true);
const { triggerQuickSearchModal } = useModal(); const { triggerQuickSearchModal } = useModal();
const isPublish = currentMetaWorkSpace?.isPublish; const isPublish = currentMetaWorkSpace?.published;
// Add ‘⌘+K shortcut keys as switches // Add ‘⌘+K shortcut keys as switches
useEffect(() => { useEffect(() => {

View File

@ -98,7 +98,7 @@ export const WorkspaceModal = ({ open, onClose }: WorkspaceModalProps) => {
{item.provider === 'affine' && ( {item.provider === 'affine' && (
<CloudInsyncIcon fontSize={24} /> <CloudInsyncIcon fontSize={24} />
)} )}
{item.isPublish && <UsersIcon fontSize={24} />} {item.published && <UsersIcon fontSize={24} />}
</span> </span>
{/* {item.isLocal ? 'isLocal' : ''}/ */} {/* {item.isLocal ? 'isLocal' : ''}/ */}
</WorkspaceItem> </WorkspaceItem>

View File

@ -1,5 +1,5 @@
import { styled } from '@/styles'; import { styled } from '@/styles';
import { WorkspaceInfo } from '@affine/datacenter'; import { WorkspaceUnit } from '@affine/datacenter';
export const ExportPageTitleContainer = styled('div')(() => { export const ExportPageTitleContainer = styled('div')(() => {
return { return {
@ -9,7 +9,7 @@ export const ExportPageTitleContainer = styled('div')(() => {
flex: 1, flex: 1,
}; };
}); });
export const ExportPage = ({ workspace }: { workspace: WorkspaceInfo }) => { export const ExportPage = ({ workspace }: { workspace: WorkspaceUnit }) => {
return ( return (
<ExportPageTitleContainer> <ExportPageTitleContainer>
Export Workspace{' '} Export Workspace{' '}

View File

@ -24,13 +24,13 @@ import { Empty } from '@/ui/empty';
// User, // User,
// Workspace, // Workspace,
// } from '@/hooks/mock-data/mock'; // } from '@/hooks/mock-data/mock';
import { WorkspaceInfo } from '@affine/datacenter'; import { WorkspaceUnit } from '@affine/datacenter';
import { useTemporaryHelper } from '@/providers/temporary-helper-provider'; import { useTemporaryHelper } from '@/providers/temporary-helper-provider';
import { StyledMemberWarp } from './general/style'; import { StyledMemberWarp } from './general/style';
import { useConfirm } from '@/providers/ConfirmProvider'; import { useConfirm } from '@/providers/ConfirmProvider';
// import { useAppState } from '@/providers/app-state-provider'; // import { useAppState } from '@/providers/app-state-provider';
export const MembersPage = ({ workspace }: { workspace: WorkspaceInfo }) => { export const MembersPage = ({ workspace }: { workspace: WorkspaceUnit }) => {
const [isInviteModalShow, setIsInviteModalShow] = useState(false); const [isInviteModalShow, setIsInviteModalShow] = useState(false);
const [members, setMembers] = useState<[{ name: string; email: string }]>([ const [members, setMembers] = useState<[{ name: string; email: string }]>([
{ name: 'affine', email: 'tttt' }, { name: 'affine', email: 'tttt' },

View File

@ -12,9 +12,9 @@ import { toast } from '@/ui/toast';
import { useConfirm } from '@/providers/ConfirmProvider'; import { useConfirm } from '@/providers/ConfirmProvider';
// import { useAppState } from '@/providers/app-state-provider3'; // import { useAppState } from '@/providers/app-state-provider3';
import { useWorkspaceHelper } from '@/hooks/use-workspace-helper'; import { useWorkspaceHelper } from '@/hooks/use-workspace-helper';
import { WorkspaceInfo } from '@affine/datacenter'; import { WorkspaceUnit } from '@affine/datacenter';
export const PublishPage = ({ workspace }: { workspace: WorkspaceInfo }) => { export const PublishPage = ({ workspace }: { workspace: WorkspaceUnit }) => {
const shareUrl = const shareUrl =
window.location.host + '/workspace/' + workspace.id + '?share=true'; window.location.host + '/workspace/' + workspace.id + '?share=true';
const { publishWorkspace } = useWorkspaceHelper(); const { publishWorkspace } = useWorkspaceHelper();
@ -53,7 +53,7 @@ export const PublishPage = ({ workspace }: { workspace: WorkspaceInfo }) => {
{workspace.provider === 'cloud' ? ( {workspace.provider === 'cloud' ? (
<div> <div>
<StyledPublishContent> <StyledPublishContent>
{workspace?.isPublish ? ( {workspace?.published ? (
<> <>
<StyledPublishExplanation> <StyledPublishExplanation>
Publishing to web requires AFFiNE Cloud service . Publishing to web requires AFFiNE Cloud service .
@ -75,7 +75,7 @@ export const PublishPage = ({ workspace }: { workspace: WorkspaceInfo }) => {
</StyledPublishExplanation> </StyledPublishExplanation>
)} )}
</StyledPublishContent> </StyledPublishContent>
{workspace.isPublish ? ( {workspace.published ? (
<Button <Button
onClick={() => { onClick={() => {
togglePublic(false); togglePublic(false);

View File

@ -6,13 +6,13 @@ import {
import { DownloadIcon } from '@blocksuite/icons'; import { DownloadIcon } from '@blocksuite/icons';
import { Button } from '@/ui/button'; import { Button } from '@/ui/button';
import { Menu, MenuItem } from '@/ui/menu'; import { Menu, MenuItem } from '@/ui/menu';
import { WorkspaceInfo } from '@affine/datacenter'; import { WorkspaceUnit } from '@affine/datacenter';
import { useWorkspaceHelper } from '@/hooks/use-workspace-helper'; import { useWorkspaceHelper } from '@/hooks/use-workspace-helper';
import { useAppState } from '@/providers/app-state-provider'; import { useAppState } from '@/providers/app-state-provider';
import { useConfirm } from '@/providers/ConfirmProvider'; import { useConfirm } from '@/providers/ConfirmProvider';
import { toast } from '@/ui/toast'; import { toast } from '@/ui/toast';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
export const SyncPage = ({ workspace }: { workspace: WorkspaceInfo }) => { export const SyncPage = ({ workspace }: { workspace: WorkspaceUnit }) => {
// console.log('workspace: ', workspace); // console.log('workspace: ', workspace);
const { enableWorkspace } = useWorkspaceHelper(); const { enableWorkspace } = useWorkspaceHelper();
const { currentWorkspace, user } = useAppState(); const { currentWorkspace, user } = useAppState();

View File

@ -14,9 +14,9 @@ import { WorkspaceDelete } from './delete';
import { WorkspaceLeave } from './leave'; import { WorkspaceLeave } from './leave';
import { Upload } from '@/components/file-upload'; import { Upload } from '@/components/file-upload';
import { WorkspaceAvatar } from '@/components/workspace-avatar'; import { WorkspaceAvatar } from '@/components/workspace-avatar';
import { WorkspaceInfo } from '@affine/datacenter'; import { WorkspaceUnit } from '@affine/datacenter';
import { useWorkspaceHelper } from '@/hooks/use-workspace-helper'; import { useWorkspaceHelper } from '@/hooks/use-workspace-helper';
export const GeneralPage = ({ workspace }: { workspace: WorkspaceInfo }) => { export const GeneralPage = ({ workspace }: { workspace: WorkspaceUnit }) => {
const [showDelete, setShowDelete] = useState<boolean>(false); const [showDelete, setShowDelete] = useState<boolean>(false);
const [showLeave, setShowLeave] = useState<boolean>(false); const [showLeave, setShowLeave] = useState<boolean>(false);
const [uploading, setUploading] = useState<boolean>(false); const [uploading, setUploading] = useState<boolean>(false);

View File

@ -17,12 +17,12 @@ import {
getWorkspaces, getWorkspaces,
// Workspace, // Workspace,
} from '@/hooks/mock-data/mock'; } from '@/hooks/mock-data/mock';
import { WorkspaceInfo } from '@affine/datacenter'; import { WorkspaceUnit } from '@affine/datacenter';
interface WorkspaceDeleteProps { interface WorkspaceDeleteProps {
open: boolean; open: boolean;
onClose: () => void; onClose: () => void;
workspace: WorkspaceInfo; workspace: WorkspaceUnit;
} }
export const WorkspaceDelete = ({ export const WorkspaceDelete = ({

View File

@ -23,14 +23,14 @@ import {
} from '@/components/workspace-setting'; } from '@/components/workspace-setting';
import { useAppState } from '@/providers/app-state-provider'; import { useAppState } from '@/providers/app-state-provider';
import WorkspaceLayout from '@/components/workspace-layout'; import WorkspaceLayout from '@/components/workspace-layout';
import { WorkspaceInfo } from '@affine/datacenter'; import { WorkspaceUnit } from '@affine/datacenter';
type TabNames = 'general' | 'members' | 'publish' | 'sync' | 'export'; type TabNames = 'general' | 'members' | 'publish' | 'sync' | 'export';
const tabMap: { const tabMap: {
name: TabNames; name: TabNames;
icon: ReactNode; icon: ReactNode;
panelRender: (workspace: WorkspaceInfo) => ReactNode; panelRender: (workspace: WorkspaceUnit) => ReactNode;
}[] = [ }[] = [
{ {
name: 'general', name: 'general',

View File

@ -8,7 +8,7 @@ import {
PageMeta, PageMeta,
} from './interface'; } from './interface';
import { createDefaultWorkspace } from './utils'; import { createDefaultWorkspace } from './utils';
import { WorkspaceInfo, User } from '@affine/datacenter'; import { WorkspaceUnit, User } from '@affine/datacenter';
type AppStateContextProps = PropsWithChildren<Record<string, unknown>>; type AppStateContextProps = PropsWithChildren<Record<string, unknown>>;
@ -66,12 +66,15 @@ export const AppStateProvider = ({
useEffect(() => { useEffect(() => {
const { dataCenter } = appState; const { dataCenter } = appState;
// FIXME: onWorkspacesChange should have dispose function // FIXME: onWorkspacesChange should have dispose function
dataCenter?.onWorkspacesChange(() => { dataCenter?.onWorkspacesChange(
setAppState({ () => {
...appState, setAppState({
workspaceList: dataCenter.workspaces, ...appState,
}); workspaceList: dataCenter.workspaces,
}); });
},
{ immediate: false }
);
}, [appState]); }, [appState]);
const loadPage = useRef<AppStateFunction['loadPage']>(); const loadPage = useRef<AppStateFunction['loadPage']>();
@ -100,7 +103,7 @@ export const AppStateProvider = ({
const workspace = await dataCenter.loadWorkspace(workspaceId); const workspace = await dataCenter.loadWorkspace(workspaceId);
const currentMetaWorkSpace = dataCenter.workspaces.find( const currentMetaWorkSpace = dataCenter.workspaces.find(
(item: WorkspaceInfo) => { (item: WorkspaceUnit) => {
return item.id === workspace.room; return item.id === workspace.room;
} }
); );

View File

@ -1,4 +1,4 @@
import { DataCenter, User, WorkspaceInfo } from '@affine/datacenter'; import { DataCenter, User, WorkspaceUnit } from '@affine/datacenter';
import type { EditorContainer } from '@blocksuite/editor'; import type { EditorContainer } from '@blocksuite/editor';
import type { import type {
@ -18,9 +18,9 @@ export interface PageMeta extends StorePageMeta {
export type AppStateValue = { export type AppStateValue = {
dataCenter: DataCenter; dataCenter: DataCenter;
user: User | null; user: User | null;
workspaceList: WorkspaceInfo[]; workspaceList: WorkspaceUnit[];
currentWorkspace: StoreWorkspace | null; currentWorkspace: StoreWorkspace | null;
currentMetaWorkSpace: WorkspaceInfo | null; currentMetaWorkSpace: WorkspaceUnit | null;
currentWorkspaceId: string; currentWorkspaceId: string;
pageList: PageMeta[]; pageList: PageMeta[];
currentPage: StorePage | null; currentPage: StorePage | null;

View File

@ -30,3 +30,4 @@ export type { AccessTokenMessage } from './provider/affine/apis';
export { WorkspaceUnit } from './workspace-unit'; export { WorkspaceUnit } from './workspace-unit';
export { getLogger } from './logger'; export { getLogger } from './logger';
export * from './message'; export * from './message';
export * from './types';

View File

@ -1,127 +0,0 @@
import { Observable } from 'lib0/observable';
import type { WorkspaceInfo, WorkspaceMeta } from './types';
export interface WorkspaceMetaCollectionScope {
get: (workspaceId: string) => WorkspaceInfo | undefined;
list: () => WorkspaceInfo[];
add: (workspace: WorkspaceInfo) => void;
remove: (workspaceId: string) => boolean;
clear: () => void;
update: (workspaceId: string, workspaceMeta: Partial<WorkspaceMeta>) => void;
}
export interface WorkspaceMetaCollectionChangeEvent {
added?: WorkspaceInfo;
deleted?: WorkspaceInfo;
updated?: WorkspaceInfo;
}
export class WorkspaceMetaCollection extends Observable<'change'> {
private _workspacesMap = new Map<string, WorkspaceInfo>();
get workspaces(): WorkspaceInfo[] {
return Array.from(this._workspacesMap.values());
}
find(workspaceId: string) {
return this._workspacesMap.get(workspaceId);
}
createScope(): WorkspaceMetaCollectionScope {
const scopedWorkspaceIds = new Set<string>();
const get = (workspaceId: string) => {
if (!scopedWorkspaceIds.has(workspaceId)) {
return;
}
return this._workspacesMap.get(workspaceId);
};
const add = (workspace: WorkspaceInfo) => {
if (this._workspacesMap.has(workspace.id)) {
throw new Error(`Duplicate workspace id.`);
}
this._workspacesMap.set(workspace.id.toString(), workspace);
scopedWorkspaceIds.add(workspace.id);
this.emit('change', [
{
added: workspace,
} as WorkspaceMetaCollectionChangeEvent,
]);
};
const remove = (workspaceId: string) => {
if (!scopedWorkspaceIds.has(workspaceId)) {
return true;
}
const workspace = this._workspacesMap.get(workspaceId);
if (workspace) {
const ret = this._workspacesMap.delete(workspaceId);
// If deletion failed, return.
if (!ret) {
return ret;
}
scopedWorkspaceIds.delete(workspaceId);
this.emit('change', [
{
deleted: workspace,
} as WorkspaceMetaCollectionChangeEvent,
]);
}
return true;
};
const clear = () => {
scopedWorkspaceIds.forEach(id => {
remove(id);
});
};
const update = (
workspaceId: string,
workspaceMeta: Partial<WorkspaceMeta>
) => {
if (!scopedWorkspaceIds.has(workspaceId)) {
return true;
}
const workspace = this._workspacesMap.get(workspaceId);
if (!workspace) {
return true;
}
this._workspacesMap.set(workspaceId, { ...workspace, ...workspaceMeta });
this.emit('change', [
{
updated: this._workspacesMap.get(workspaceId),
} as WorkspaceMetaCollectionChangeEvent,
]);
};
// TODO: need to optimize
const list = () => {
const workspaces: WorkspaceInfo[] = [];
scopedWorkspaceIds.forEach(id => {
const workspace = this._workspacesMap.get(id);
if (workspace) {
workspaces.push(workspace);
}
});
return workspaces;
};
return {
get,
list,
add,
remove,
clear,
update,
};
}
}

View File

@ -65,7 +65,7 @@ export class WorkspaceUnitCollection {
} }
const workspaceUnit = new WorkspaceUnit(workspace); const workspaceUnit = new WorkspaceUnit(workspace);
this._workspaceUnitMap.set(workspace.id, workspaceUnit); this._workspaceUnitMap.set(workspace.id.toString(), workspaceUnit);
scopedWorkspaceIds.add(workspace.id); scopedWorkspaceIds.add(workspace.id);