mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-24 13:17:10 +08:00
fix(core): add loading for insert ai template (#7369)
This commit is contained in:
@@ -107,7 +107,6 @@ export function fromPromise<T>(
|
|||||||
.catch(error => {
|
.catch(error => {
|
||||||
subscriber.error(error);
|
subscriber.error(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
return () => abortController.abort('Aborted');
|
return () => abortController.abort('Aborted');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,9 @@
|
|||||||
|
import { toast } from '@affine/component';
|
||||||
|
import {
|
||||||
|
pushGlobalLoadingEventAtom,
|
||||||
|
resolveGlobalLoadingEventAtom,
|
||||||
|
} from '@affine/component/global-loading';
|
||||||
|
import { useI18n } from '@affine/i18n';
|
||||||
import { ZipTransformer } from '@blocksuite/blocks';
|
import { ZipTransformer } from '@blocksuite/blocks';
|
||||||
import { assertExists } from '@blocksuite/global/utils';
|
import { assertExists } from '@blocksuite/global/utils';
|
||||||
import {
|
import {
|
||||||
@@ -9,8 +15,13 @@ import {
|
|||||||
useSensors,
|
useSensors,
|
||||||
} from '@dnd-kit/core';
|
} from '@dnd-kit/core';
|
||||||
import {
|
import {
|
||||||
|
type DocMode,
|
||||||
DocsService,
|
DocsService,
|
||||||
|
effect,
|
||||||
|
fromPromise,
|
||||||
GlobalContextService,
|
GlobalContextService,
|
||||||
|
onStart,
|
||||||
|
throwIfAborted,
|
||||||
useLiveData,
|
useLiveData,
|
||||||
useService,
|
useService,
|
||||||
WorkspaceService,
|
WorkspaceService,
|
||||||
@@ -19,6 +30,14 @@ import { useAtomValue, useSetAtom } from 'jotai';
|
|||||||
import type { PropsWithChildren, ReactNode } from 'react';
|
import type { PropsWithChildren, ReactNode } from 'react';
|
||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import { createPortal } from 'react-dom';
|
import { createPortal } from 'react-dom';
|
||||||
|
import {
|
||||||
|
catchError,
|
||||||
|
EMPTY,
|
||||||
|
finalize,
|
||||||
|
mergeMap,
|
||||||
|
switchMap,
|
||||||
|
timeout,
|
||||||
|
} from 'rxjs';
|
||||||
import { Map as YMap } from 'yjs';
|
import { Map as YMap } from 'yjs';
|
||||||
|
|
||||||
import { openSettingModalAtom } from '../atoms';
|
import { openSettingModalAtom } from '../atoms';
|
||||||
@@ -103,6 +122,9 @@ export const WorkspaceLayout = function WorkspaceLayout({
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const WorkspaceLayoutInner = ({ children }: PropsWithChildren) => {
|
export const WorkspaceLayoutInner = ({ children }: PropsWithChildren) => {
|
||||||
|
const t = useI18n();
|
||||||
|
const pushGlobalLoadingEvent = useSetAtom(pushGlobalLoadingEventAtom);
|
||||||
|
const resolveGlobalLoadingEvent = useSetAtom(resolveGlobalLoadingEventAtom);
|
||||||
const currentWorkspace = useService(WorkspaceService).workspace;
|
const currentWorkspace = useService(WorkspaceService).workspace;
|
||||||
const docsList = useService(DocsService).list;
|
const docsList = useService(DocsService).list;
|
||||||
const { openPage } = useNavigateHelper();
|
const { openPage } = useNavigateHelper();
|
||||||
@@ -120,26 +142,60 @@ export const WorkspaceLayoutInner = ({ children }: PropsWithChildren) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const disposable = AIProvider.slots.requestInsertTemplate.on(
|
const insertTemplate = effect(
|
||||||
({ template, mode }) => {
|
switchMap(({ template, mode }: { template: string; mode: string }) => {
|
||||||
(async () => {
|
return fromPromise(async abort => {
|
||||||
const templateZip = await fetch(template);
|
const templateZip = await fetch(template, { signal: abort });
|
||||||
const templateBlob = await templateZip.blob();
|
const templateBlob = await templateZip.blob();
|
||||||
|
throwIfAborted(abort);
|
||||||
const [doc] = await ZipTransformer.importDocs(
|
const [doc] = await ZipTransformer.importDocs(
|
||||||
currentWorkspace.docCollection,
|
currentWorkspace.docCollection,
|
||||||
templateBlob
|
templateBlob
|
||||||
);
|
);
|
||||||
doc.resetHistory();
|
doc.resetHistory();
|
||||||
|
|
||||||
docsList.setMode(doc.id, mode);
|
return { doc, mode };
|
||||||
workbench.openPage(doc.id);
|
}).pipe(
|
||||||
})().catch(err => {
|
timeout(10000 /* 10s */),
|
||||||
console.error(err);
|
mergeMap(({ mode, doc }) => {
|
||||||
});
|
docsList.setMode(doc.id, mode as DocMode);
|
||||||
|
workbench.openPage(doc.id);
|
||||||
|
return EMPTY;
|
||||||
|
}),
|
||||||
|
onStart(() => {
|
||||||
|
pushGlobalLoadingEvent({
|
||||||
|
key: 'insert-template',
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
catchError(err => {
|
||||||
|
console.error(err);
|
||||||
|
toast(t['com.affine.ai.template-insert.failed']());
|
||||||
|
return EMPTY;
|
||||||
|
}),
|
||||||
|
finalize(() => {
|
||||||
|
resolveGlobalLoadingEvent('insert-template');
|
||||||
|
})
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const disposable = AIProvider.slots.requestInsertTemplate.on(
|
||||||
|
({ template, mode }) => {
|
||||||
|
insertTemplate({ template, mode });
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return () => disposable.dispose();
|
return () => {
|
||||||
}, [currentWorkspace.docCollection, docsList, workbench]);
|
disposable.dispose();
|
||||||
|
insertTemplate.unsubscribe();
|
||||||
|
};
|
||||||
|
}, [
|
||||||
|
currentWorkspace.docCollection,
|
||||||
|
docsList,
|
||||||
|
pushGlobalLoadingEvent,
|
||||||
|
resolveGlobalLoadingEvent,
|
||||||
|
t,
|
||||||
|
workbench,
|
||||||
|
]);
|
||||||
|
|
||||||
useRegisterWorkspaceCommands();
|
useRegisterWorkspaceCommands();
|
||||||
useRegisterNavigationCommands();
|
useRegisterNavigationCommands();
|
||||||
|
|||||||
@@ -1342,5 +1342,6 @@
|
|||||||
"will be moved to Trash": "{{title}} will be moved to Trash",
|
"will be moved to Trash": "{{title}} will be moved to Trash",
|
||||||
"will delete member": "will delete member",
|
"will delete member": "will delete member",
|
||||||
"com.affine.user-info.usage.ai": "AI Usage",
|
"com.affine.user-info.usage.ai": "AI Usage",
|
||||||
"com.affine.user-info.usage.cloud": "Cloud Storage"
|
"com.affine.user-info.usage.cloud": "Cloud Storage",
|
||||||
|
"com.affine.ai.template-insert.failed": "Failed to insert template, please try again."
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user