Skip to content

Commit

Permalink
chore(core): add event tracking for attachments (#9198)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 20, 2024
1 parent 7ff24e9 commit d7983c5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Scroller,
ScrollSeekPlaceholder,
} from '@affine/core/modules/pdf/views';
import track from '@affine/track';
import type { AttachmentBlockModel } from '@blocksuite/affine/blocks';
import { CollapseIcon, ExpandIcon } from '@blocksuite/icons/rc';
import { useLiveData, useService } from '@toeverything/infra';
Expand Down Expand Up @@ -202,6 +203,12 @@ const PDFViewerInner = ({ pdf, state }: PDFViewerInnerProps) => {
function PDFViewerStatus({ pdf }: { pdf: PDF }) {
const state = useLiveData(pdf.state$);

useEffect(() => {
if (state.status !== PDFStatus.Error) return;

track.$.attachment.$.openPDFRendererFail();
}, [state]);

if (state?.status !== PDFStatus.Opened) {
return <LoadingSvg />;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type ImagePeekViewInfo = {

export type AttachmentPeekViewInfo = {
type: 'attachment';
docRef: DocReferenceInfo;
docRef: DocReferenceInfo & { filetype?: string };
};

export type AIChatBlockPeekViewInfo = {
Expand Down Expand Up @@ -173,6 +173,7 @@ function resolvePeekInfoFromPeekTarget(
docRef: {
docId: blockModel.doc.id,
blockIds: [blockModel.id],
filetype: blockModel.type,
},
};
} else if (isImageBlockModel(blockModel)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IconButton } from '@affine/component';
import { useI18n } from '@affine/i18n';
import track from '@affine/track';
import type { DocMode } from '@blocksuite/affine/blocks';
import {
CloseIcon,
Expand All @@ -16,12 +17,16 @@ import {
type ReactElement,
type SVGAttributes,
useCallback,
useEffect,
useMemo,
} from 'react';

import { WorkspaceDialogService } from '../../dialogs';
import { WorkbenchService } from '../../workbench';
import type { DocReferenceInfo } from '../entities/peek-view';
import type {
AttachmentPeekViewInfo,
DocReferenceInfo,
} from '../entities/peek-view';
import { PeekViewService } from '../services/peek-view';
import * as styles from './peek-view-controls.css';

Expand Down Expand Up @@ -154,45 +159,55 @@ export const DocPeekViewControls = ({
);
};

type AttachmentPeekViewControls = HTMLAttributes<HTMLDivElement> & {
mode?: DocMode;
docRef: AttachmentPeekViewInfo['docRef'];
};

export const AttachmentPeekViewControls = ({
docRef,
className,
...rest
}: DocPeekViewControlsProps) => {
}: AttachmentPeekViewControls) => {
const { docId, blockIds: [blockId] = [], filetype: type } = docRef;
const peekView = useService(PeekViewService).peekView;
const workbench = useService(WorkbenchService).workbench;
const t = useI18n();

const controls = useMemo(() => {
return [
const controls = [
{
icon: <CloseIcon />,
nameKey: 'close',
name: t['com.affine.peek-view-controls.close'](),
onClick: () => peekView.close(),
},
];
if (!type) return controls;

return [
...controls,
// TODO(@fundon): needs to be implemented on mobile
BUILD_CONFIG.isDesktopEdition && {
icon: <ExpandFullIcon />,
name: t['com.affine.peek-view-controls.open-attachment'](),
nameKey: 'open',
onClick: () => {
const { docId, blockIds: [blockId] = [] } = docRef;
if (docId && blockId) {
workbench.openAttachment(docId, blockId);
}
workbench.openAttachment(docId, blockId);
peekView.close(false);

track.$.attachment.$.openAttachmentInFullscreen({ type });
},
},
{
icon: <OpenInNewIcon />,
nameKey: 'new-tab',
name: t['com.affine.peek-view-controls.open-attachment-in-new-tab'](),
onClick: () => {
const { docId, blockIds: [blockId] = [] } = docRef;
if (docId && blockId) {
workbench.openAttachment(docId, blockId, { at: 'new-tab' });
}
workbench.openAttachment(docId, blockId, { at: 'new-tab' });
peekView.close(false);

track.$.attachment.$.openAttachmentInNewTab({ type });
},
},
BUILD_CONFIG.isElectron && {
Expand All @@ -202,15 +217,21 @@ export const AttachmentPeekViewControls = ({
'com.affine.peek-view-controls.open-attachment-in-split-view'
](),
onClick: () => {
const { docId, blockIds: [blockId] = [] } = docRef;
if (docId && blockId) {
workbench.openAttachment(docId, blockId, { at: 'beside' });
}
workbench.openAttachment(docId, blockId, { at: 'beside' });
peekView.close(false);

track.$.attachment.$.openAttachmentInSplitView({ type });
},
},
].filter((opt): opt is ControlButtonProps => Boolean(opt));
}, [t, peekView, workbench, docRef]);
}, [t, peekView, workbench, docId, blockId, type]);

useEffect(() => {
if (type === undefined) return;

track.$.attachment.$.openAttachmentInPeekView({ type });
}, [type]);

return (
<div {...rest} className={clsx(styles.root, className)}>
{controls.map(option => (
Expand Down
29 changes: 28 additions & 1 deletion packages/frontend/track/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ type PaymentEvents =
| 'confirmResumingSubscription';
// END SECTION

// SECTION: attachment
type AttachmentEvents =
| 'openAttachmentInFullscreen'
| 'openAttachmentInNewTab'
| 'openAttachmentInPeekView'
| 'openAttachmentInSplitView'
| 'openPDFRendererFail';
// END SECTION

type UserEvents =
| GeneralEvents
| AppEvents
Expand All @@ -130,7 +139,8 @@ type UserEvents =
| AuthEvents
| AccountEvents
| PaymentEvents
| DNDEvents;
| DNDEvents
| AttachmentEvents;
interface PageDivision {
[page: string]: {
[segment: string]: {
Expand Down Expand Up @@ -284,6 +294,15 @@ const PageEvents = {
importModal: ['open'],
snapshot: ['import', 'export'],
},
attachment: {
$: [
'openAttachmentInFullscreen',
'openAttachmentInNewTab',
'openAttachmentInPeekView',
'openAttachmentInSplitView',
'openPDFRendererFail',
],
},
},
doc: {
editor: {
Expand Down Expand Up @@ -353,6 +372,10 @@ type PaymentEventArgs = {
recurring: string;
};

type AttachmentEventArgs = {
type: string; // file type
};

type TabActionControlType =
| 'click'
| 'dnd'
Expand Down Expand Up @@ -435,6 +458,10 @@ export type EventArgs = {
linkDoc: { type: string; journal: boolean };
drop: { type: string };
dragStart: { type: string };
openAttachmentInFullscreen: AttachmentEventArgs;
openAttachmentInNewTab: AttachmentEventArgs;
openAttachmentInPeekView: AttachmentEventArgs;
openAttachmentInSplitView: AttachmentEventArgs;
};

// for type checking
Expand Down

0 comments on commit d7983c5

Please sign in to comment.