refactor: workspace manager (#5060)

This commit is contained in:
EYHN
2023-12-15 07:20:50 +00:00
parent af15aa06d4
commit fe2851d3e9
217 changed files with 3605 additions and 4244 deletions
@@ -12,7 +12,7 @@ import { expect, test } from 'vitest';
import { createDefaultFilter, vars } from '../filter/vars';
import {
type CollectionsCRUDAtom,
type CollectionsCRUD,
useCollectionManager,
} from '../use-collection-manager';
@@ -27,18 +27,18 @@ const baseAtom = atomWithObservable<Collection[]>(
}
);
const mockAtom: CollectionsCRUDAtom = atom(get => {
const mockAtom = atom(get => {
return {
collections: get(baseAtom),
addCollection: async (...collections) => {
addCollection: (...collections) => {
const prev = collectionsSubject.value;
collectionsSubject.next([...collections, ...prev]);
},
deleteCollection: async (...ids) => {
deleteCollection: (...ids) => {
const prev = collectionsSubject.value;
collectionsSubject.next(prev.filter(v => !ids.includes(v.id)));
},
updateCollection: async (id, updater) => {
updateCollection: (id, updater) => {
const prev = collectionsSubject.value;
collectionsSubject.next(
prev.map(v => {
@@ -49,14 +49,14 @@ const mockAtom: CollectionsCRUDAtom = atom(get => {
})
);
},
};
} satisfies CollectionsCRUD;
});
test('useAllPageSetting', async () => {
const settingHook = renderHook(() => useCollectionManager(mockAtom));
const prevCollection = settingHook.result.current.currentCollection;
expect(settingHook.result.current.savedCollections).toEqual([]);
await settingHook.result.current.updateCollection({
settingHook.result.current.updateCollection({
...settingHook.result.current.currentCollection,
filterList: [createDefaultFilter(vars[0], defaultMeta)],
});
@@ -66,7 +66,7 @@ test('useAllPageSetting', async () => {
expect(nextCollection.filterList).toEqual([
createDefaultFilter(vars[0], defaultMeta),
]);
await settingHook.result.current.createCollection({
settingHook.result.current.createCollection({
...settingHook.result.current.currentCollection,
id: '1',
});
@@ -33,22 +33,21 @@ export const currentCollectionAtom = atomWithReset<string>(NIL);
export type Updater<T> = (value: T) => T;
export type CollectionUpdater = Updater<Collection>;
export type CollectionsCRUD = {
addCollection: (...collections: Collection[]) => Promise<void>;
addCollection: (...collections: Collection[]) => void;
collections: Collection[];
updateCollection: (id: string, updater: CollectionUpdater) => Promise<void>;
deleteCollection: (
info: DeleteCollectionInfo,
...ids: string[]
) => Promise<void>;
updateCollection: (id: string, updater: CollectionUpdater) => void;
deleteCollection: (info: DeleteCollectionInfo, ...ids: string[]) => void;
};
export type CollectionsCRUDAtom = Atom<CollectionsCRUD>;
export type CollectionsCRUDAtom = Atom<
Promise<CollectionsCRUD> | CollectionsCRUD
>;
export const useSavedCollections = (collectionAtom: CollectionsCRUDAtom) => {
const [{ collections, addCollection, deleteCollection, updateCollection }] =
useAtom(collectionAtom);
const addPage = useCallback(
async (collectionId: string, pageId: string) => {
await updateCollection(collectionId, old => {
(collectionId: string, pageId: string) => {
updateCollection(collectionId, old => {
return {
...old,
allowList: [pageId, ...(old.allowList ?? [])],
@@ -79,11 +78,11 @@ export const useCollectionManager = (collectionsAtom: CollectionsCRUDAtom) => {
defaultCollectionAtom
);
const update = useCallback(
async (collection: Collection) => {
(collection: Collection) => {
if (collection.id === NIL) {
updateDefaultCollection(collection);
} else {
await updateCollection(collection.id, () => collection);
updateCollection(collection.id, () => collection);
}
},
[updateDefaultCollection, updateCollection]
@@ -35,14 +35,10 @@ export const CollectionList = ({
const [collection, setCollection] = useState<Collection>();
const onChange = useCallback(
(filterList: Filter[]) => {
setting
.updateCollection({
...setting.currentCollection,
filterList,
})
.catch(err => {
console.error(err);
});
setting.updateCollection({
...setting.currentCollection,
filterList,
});
},
[setting]
);
@@ -53,8 +49,8 @@ export const CollectionList = ({
}, []);
const onConfirm = useCallback(
async (view: Collection) => {
await setting.updateCollection(view);
(view: Collection) => {
setting.updateCollection(view);
closeUpdateCollectionModal(false);
},
[closeUpdateCollectionModal, setting]
@@ -107,9 +107,7 @@ export const CollectionOperations = ({
),
name: t['Delete'](),
click: () => {
setting.deleteCollection(info, collection.id).catch(err => {
console.error(err);
});
setting.deleteCollection(info, collection.id);
},
type: 'danger',
},
@@ -10,7 +10,7 @@ export interface CreateCollectionModalProps {
title?: string;
onConfirmText?: string;
init: string;
onConfirm: (title: string) => Promise<void>;
onConfirm: (title: string) => void;
open: boolean;
showTips?: boolean;
onOpenChange: (open: boolean) => void;
@@ -27,13 +27,8 @@ export const CreateCollectionModal = ({
const t = useAFFiNEI18N();
const onConfirmTitle = useCallback(
(title: string) => {
onConfirm(title)
.then(() => {
onOpenChange(false);
})
.catch(err => {
console.error(err);
});
onConfirm(title);
onOpenChange(false);
},
[onConfirm, onOpenChange]
);
@@ -19,7 +19,7 @@ export interface EditCollectionModalProps {
open: boolean;
mode?: EditCollectionMode;
onOpenChange: (open: boolean) => void;
onConfirm: (view: Collection) => Promise<void>;
onConfirm: (view: Collection) => void;
allPageListConfig: AllPageListConfig;
}
@@ -45,13 +45,7 @@ export const EditCollectionModal = ({
const t = useAFFiNEI18N();
const onConfirmOnCollection = useCallback(
(view: Collection) => {
onConfirm(view)
.then(() => {
onOpenChange(false);
})
.catch(err => {
console.error(err);
});
onConfirm(view);
onOpenChange(false);
},
[onConfirm, onOpenChange]
@@ -9,7 +9,7 @@ import { createEmptyCollection } from '../use-collection-manager';
import { useEditCollectionName } from './use-edit-collection';
interface SaveAsCollectionButtonProps {
onConfirm: (collection: Collection) => Promise<void>;
onConfirm: (collection: Collection) => void;
}
export const SaveAsCollectionButton = ({
@@ -40,9 +40,7 @@ export const useActions = ({
name: 'delete',
tooltip: t['com.affine.collection-bar.action.tooltip.delete'](),
click: () => {
setting.deleteCollection(info, collection.id).catch(err => {
console.error(err);
});
setting.deleteCollection(info, collection.id);
},
},
];
@@ -12,7 +12,7 @@ export const useEditCollection = (config: AllPageListConfig) => {
const [data, setData] = useState<{
collection: Collection;
mode?: 'page' | 'rule';
onConfirm: (collection: Collection) => Promise<void>;
onConfirm: (collection: Collection) => void;
}>();
const close = useCallback(() => setData(undefined), []);
@@ -35,7 +35,7 @@ export const useEditCollection = (config: AllPageListConfig) => {
setData({
collection,
mode,
onConfirm: async collection => {
onConfirm: collection => {
res(collection);
},
});
@@ -52,7 +52,7 @@ export const useEditCollectionName = ({
}) => {
const [data, setData] = useState<{
name: string;
onConfirm: (name: string) => Promise<void>;
onConfirm: (name: string) => void;
}>();
const close = useCallback(() => setData(undefined), []);
@@ -71,7 +71,7 @@ export const useEditCollectionName = ({
new Promise<string>(res => {
setData({
name,
onConfirm: async collection => {
onConfirm: collection => {
res(collection);
},
});