Skip to content

Commit

Permalink
feat: add telemetry event for drop linked doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Dec 19, 2024
1 parent e6706fb commit 56abf31
Showing 1 changed file with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
EMBED_CARD_HEIGHT,
EMBED_CARD_WIDTH,
} from '@blocksuite/affine-shared/consts';
import { DndApiExtensionIdentifier } from '@blocksuite/affine-shared/services';
import {
DndApiExtensionIdentifier,
DocModeProvider,
TelemetryProvider,
} from '@blocksuite/affine-shared/services';
import {
captureEventTarget,
getBlockComponentsExcludeSubtrees,
Expand Down Expand Up @@ -288,7 +292,7 @@ export class DragEventWatcher {
const id = first.id;

const std = this._std;
const job = this._job;
const job = this._getJob();
const snapshotWithoutNote = {
...snapshot,
content: first.children,
Expand Down Expand Up @@ -329,7 +333,7 @@ export class DragEventWatcher {
first.props.height = height;

const std = this._std;
const job = this._job;
const job = this._getJob();
job
.snapshotToSlice(snapshot, std.doc, edgelessRoot.surfaceBlockModel.id)
.catch(console.error);
Expand All @@ -351,6 +355,10 @@ export class DragEventWatcher {
);
if (!newBound) return;

if (first.flavour === 'affine:embed-linked-doc') {
this._trackLinkedDocCreated(first.id);
}

importToSurface(width, height, newBound);
return;
}
Expand Down Expand Up @@ -430,16 +438,26 @@ export class DragEventWatcher {
this._serializeData(slice, state);
};

private get _dndAPI() {
return this._std.get(DndApiExtensionIdentifier);
}
private _trackLinkedDocCreated = (id: string) => {
const isNewBlock = !this._std.doc.hasBlock(id);
if (!isNewBlock) {
return;
}

private get _job() {
const std = this._std;
return new Job({
collection: std.collection,
middlewares: [newIdCrossDoc(std), surfaceRefToEmbed(std)],
const mode =
this._std.getOptional(DocModeProvider)?.getEditorMode() ?? 'page';

const telemetryService = this._std.getOptional(TelemetryProvider);
telemetryService?.track('LinkedDocCreated', {
control: `drop on ${mode}`,
module: 'drag and drop',
type: 'doc',
other: 'new doc',
});
};

private get _dndAPI() {
return this._std.get(DndApiExtensionIdentifier);
}

private get _std() {
Expand All @@ -458,10 +476,16 @@ export class DragEventWatcher {
if (!dataTransfer) throw new Error('No data transfer');

const std = this._std;
const job = this._job;
const job = this._getJob();

const snapshot = this._deserializeSnapshot(state);
if (snapshot) {
if (snapshot.content.length === 1) {
const [first] = snapshot.content;
if (first.flavour === 'affine:embed-linked-doc') {
this._trackLinkedDocCreated(first.id);
}
}
// use snapshot
const slice = await job.snapshotToSlice(
snapshot,
Expand Down Expand Up @@ -512,11 +536,19 @@ export class DragEventWatcher {
}
}

private _getJob() {
const std = this._std;
return new Job({
collection: std.collection,
middlewares: [newIdCrossDoc(std), surfaceRefToEmbed(std)],
});
}

private _serializeData(slice: Slice, state: DndEventState) {
const dataTransfer = state.raw.dataTransfer;
if (!dataTransfer) return;

const job = this._job;
const job = this._getJob();

const snapshot = job.sliceToSnapshot(slice);
if (!snapshot) return;
Expand Down

0 comments on commit 56abf31

Please sign in to comment.