Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom behavior after harbor upload #119

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions packages/page-spy-plugin-data-harbor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ interface DataHarborConfig {
filename?: () => string;

// Custom download behavior.
onDownload?: ((data: CacheMessageItem[]) => void) | null;
onDownload?: (data: CacheMessageItem[]) => void;

// Custom behavior after upload.
onAfterUpload?: (replayUrl: string, remark: string) => void;
}

const defaultConfig: Required<DataHarborConfig> = {
Expand All @@ -61,7 +64,8 @@ const defaultConfig: Required<DataHarborConfig> = {
filename: () => {
return new Date().toLocaleString();
},
onDownload: null,
onDownload: () => {},
onAfterUpload: () => {},
};

export default class DataHarborPlugin implements PageSpyPlugin {
Expand Down Expand Up @@ -263,29 +267,24 @@ export default class DataHarborPlugin implements PageSpyPlugin {
remark: '',
},
): Promise<void | string> {
let result;
if (type === 'upload' || type === 'download') {
const args: any = await this.getParams(type as any, params);
result =
type === 'upload' ? await startUpload(args) : await startDownload(args);

if ((params as WholeActionParams).clearCache === true) {
this.clearAndNotify();
}
if (!isPeriodActionParams(params) || params.startTime > params.endTime) {
throw new Error(t.invalidParams);
}
if (type === 'upload-periods' || type === 'download-periods') {
if (!isPeriodActionParams(params) || params.startTime > params.endTime) {
throw new Error(t.invalidParams);
}
const args: any = await this.getParams(type as any, params);
result =
type === 'upload-periods'
? await startUpload(args)
: await startDownload(args);

const args: any = await this.getParams(type as any, params);
const isUpload = ['upload', 'upload-periods'].includes(type);
const result = isUpload
? await startUpload(args)
: await startDownload(args);

if ((params as WholeActionParams).clearCache === true) {
this.clearAndNotify();
}

if (result) {
return this.getDebugUrl(result);
const url = this.getDebugUrl(result);
this.$harborConfig.onAfterUpload(url, args.remark);
return url;
}
}

Expand Down
15 changes: 10 additions & 5 deletions packages/page-spy-plugin-mp-data-harbor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
/* eslint-disable consistent-return */
import type {
SpyMessage,
OnInitParams,
PageSpyPlugin,
PluginOrder,
InitConfigBase,
} from '@huolala-tech/page-spy-types';
import { removeEndSlash } from '@huolala-tech/page-spy-base/dist/utils';
import { MemoryHarbor } from './harbor/memoryHarbor';
import { startUpload } from './utils/upload';
import { buildSearchParams, getDeviceId, makeData, makeFile } from './utils';
import { DataType, WholeActionParams } from './harbor/base';
import {
getMPSDK,
type Client,
Expand All @@ -19,6 +14,10 @@ import {
MPPluginInitParams,
setMPSDK,
} from '@huolala-tech/page-spy-mp-base';
import { MemoryHarbor } from './harbor/memoryHarbor';
import { startUpload } from './utils/upload';
import { buildSearchParams, getDeviceId, makeData, makeFile } from './utils';
import { DataType, WholeActionParams } from './harbor/base';

interface DataHarborConfig {
// Specify which types of data to collect.
Expand All @@ -27,6 +26,9 @@ interface DataHarborConfig {
// Custom uploaded filename by this.
// Default value is `new Date().toLocaleString()`.
filename?: () => string;

// Custom behavior after upload
onAfterUpload?: (replayUrl: string) => void;
}

const defaultConfig: DataHarborConfig = {
Expand All @@ -40,6 +42,7 @@ const defaultConfig: DataHarborConfig = {
filename: () => {
return new Date().toLocaleString();
},
onAfterUpload: () => {},
};

export default class MPDataHarborPlugin implements PageSpyPlugin {
Expand Down Expand Up @@ -120,6 +123,8 @@ export default class MPDataHarborPlugin implements PageSpyPlugin {
const result = await this.upload();
mp.hideLoading();
if (result) {
this.$harborConfig.onAfterUpload(result);

if (mp.setClipboardData) {
mp.showModal({
title: '上传成功',
Expand Down
Loading