Skip to content

Commit

Permalink
feat: optimize import export event tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
donteatfriedrice committed Oct 31, 2024
1 parent f40578a commit 6646581
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Button, IconButton, Modal } from '@affine/component';
import { UrlService } from '@affine/core/modules/url';
import { WorkbenchService } from '@affine/core/modules/workbench';
import { useI18n } from '@affine/i18n';
import track from '@affine/track';
import {
MarkdownTransformer,
NotionHtmlTransformer,
Expand Down Expand Up @@ -294,19 +295,35 @@ export const ImportModal = ({ ...modalProps }) => {
}

setStatus('importing');
track.$.importModal.$.import({
type,
status: 'importing',
});

const pageIds = await importConfig.importFunction(docCollection, file);

setPageIds(pageIds);
setStatus('success');
track.$.importModal.$.import({
type,
status: 'success',
result: {
docCount: pageIds.length,
},
});
} catch (error) {
setImportError(
error instanceof Error ? error.message : 'Unknown error occurred'
);
setStatus('error');
track.$.importModal.$.import({
type,
status: 'failed',
error: importError || undefined,
});
}
},
[docCollection, t]
[docCollection, t, importError]
);

const handleComplete = useCallback(() => {
Expand All @@ -318,6 +335,10 @@ export const ImportModal = ({ ...modalProps }) => {
setOpenImportModalAtom(false);
}, [pageIds, workbench, setOpenImportModalAtom]);

const handleOpenChange = (open: boolean) => {
track.$.importModal.$[open ? 'open' : 'close']();
};

const handleRetry = () => {
setStatus('idle');
};
Expand Down Expand Up @@ -349,6 +370,7 @@ export const ImportModal = ({ ...modalProps }) => {
}}
withoutCloseButton={status === 'importing'}
persistent={status === 'importing'}
onOpenChange={handleOpenChange}
{...modalProps}
>
<div className={style.importModalContainer}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hoo
import { useSystemOnline } from '@affine/core/components/hooks/use-system-online';
import { DesktopApiService } from '@affine/core/modules/desktop-api/service';
import { useI18n } from '@affine/i18n';
import track from '@affine/track';
import {
useService,
type Workspace,
Expand Down Expand Up @@ -33,6 +34,9 @@ export const DesktopExportPanel = ({
}
setSaving(true);
try {
track.$.settingsPanel.workspace.export({
type: 'workspace',
});
if (isOnline) {
await workspace.engine.waitForDocSynced();
await workspace.engine.blob.sync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export const PageHeaderMenuButton = ({
}, [duplicate, pageId]);

const handleOpenImportModal = useCallback(() => {
track.$.header.docOptions.import();
track.$.header.importModal.open();
setOpenImportModalAtom(true);
}, [setOpenImportModalAtom]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export const Snapshot = ({ className }: SnapshotProps) => {
const file = await openFileOrFiles({ acceptType: 'Zip' });
if (!file) return null;

track.$.header.snapshot.import({
type: 'snapshot',
status: 'importing',
});

const importedDocs = (
await ZipTransformer.importDocs(docCollection, file)
).filter(doc => doc !== undefined);
Expand All @@ -122,13 +127,25 @@ export const Snapshot = ({ className }: SnapshotProps) => {
title: 'Imported Snapshot Successfully',
message: `Imported ${importedDocs.length} doc(s)`,
});
track.$.header.snapshot.import({
type: 'snapshot',
status: 'success',
result: {
docCount: importedDocs.length,
},
});
return importedDocs;
} catch (error) {
console.error('Error importing snapshot:', error);
notify.error({
title: 'Import Snapshot Failed',
message: 'Failed to import snapshot. Please try again.',
});
track.$.header.snapshot.import({
type: 'snapshot',
status: 'failed',
error: error instanceof Error ? error.message : undefined,
});
return null;
}
}, [docCollection]);
Expand Down Expand Up @@ -165,6 +182,9 @@ export const Snapshot = ({ className }: SnapshotProps) => {
case 'import':
return handleImportSnapshot();
case 'export':
track.$.header.snapshot.export({
type: 'snapshot',
});
return exportHandler('snapshot');
case 'disable':
return disableSnapshotActionOption();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const RootAppSidebar = (): ReactElement => {
}, [setOpenSettingModalAtom]);

const onOpenImportModal = useCallback(() => {
track.$.navigationPanel.importModal.open();
setOpenImportModalAtom(true);
}, [setOpenImportModalAtom]);

Expand Down
12 changes: 6 additions & 6 deletions packages/frontend/i18n/src/i18n-completenesses.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"ar": 84,
"ar": 82,
"ca": 6,
"da": 6,
"de": 31,
"en": 100,
"es-AR": 15,
"es-CL": 17,
"es": 15,
"fr": 74,
"fr": 73,
"hi": 2,
"it": 1,
"ja": 99,
"ko": 88,
"ja": 98,
"ko": 87,
"pl": 0,
"pt-BR": 96,
"ru": 82,
"pt-BR": 94,
"ru": 80,
"sv-SE": 5,
"ur": 3,
"zh-Hans": 98,
Expand Down
22 changes: 19 additions & 3 deletions packages/frontend/track/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ type UserEvents =
| AuthEvents
| AccountEvents
| PaymentEvents;

interface PageDivision {
[page: string]: {
[segment: string]: {
Expand Down Expand Up @@ -152,7 +151,7 @@ const PageEvents = {
},
settingsPanel: {
menu: ['openSettings'],
workspace: ['viewPlans'],
workspace: ['viewPlans', 'export'],
profileAndBadge: ['viewPlans'],
accountUsage: ['viewPlans'],
accountSettings: ['uploadAvatar', 'removeAvatar', 'updateUserName'],
Expand Down Expand Up @@ -214,7 +213,8 @@ const PageEvents = {
'openChangelog',
'dismissChangelog',
],
others: ['navigate', 'import'],
others: ['navigate'],
importModal: ['open'],
workspaceList: [
'open',
'signIn',
Expand All @@ -231,6 +231,9 @@ const PageEvents = {
docHistory: {
$: ['open', 'close', 'switchPageMode', 'viewPlans'],
},
importModal: {
$: ['open', 'close', 'import'],
},
paywall: {
storage: ['viewPlans'],
aiAction: ['viewPlans'],
Expand Down Expand Up @@ -259,6 +262,8 @@ const PageEvents = {
],
history: ['open'],
pageInfo: ['open'],
importModal: ['open'],
snapshot: ['import', 'export'],
},
},
doc: {
Expand Down Expand Up @@ -354,6 +359,16 @@ type AuthArgs = {
provider?: string;
};

type ImportStatus = 'importing' | 'failed' | 'success';
type ImportArgs = {
type: string;
status?: ImportStatus;
error?: string;
result?: {
docCount: number;
};
};

export type EventArgs = {
createWorkspace: { flavour: string };
signIn: AuthArgs;
Expand Down Expand Up @@ -393,6 +408,7 @@ export type EventArgs = {
copyShareLink: {
type: 'default' | 'doc' | 'whiteboard' | 'block' | 'element';
};
import: ImportArgs;
export: { type: string };
copyBlockToLink: {
type: string;
Expand Down

0 comments on commit 6646581

Please sign in to comment.