Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Oct 15, 2024
1 parent 880fac9 commit 5941554
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 27 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/editor/model/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import ComponentWrapper from '../../dom_components/model/ComponentWrapper';
import { CanvasSpotBuiltInTypes } from '../../canvas/model/CanvasSpot';
import DataSourceManager from '../../data_sources';
import { ComponentsEvents } from '../../dom_components/types';
import { InitEditorConfig } from '../..';

Backbone.$ = $;

Expand Down Expand Up @@ -113,7 +114,7 @@ export default class EditorModel extends Model {
__skip = false;
defaultRunning = false;
destroyed = false;
_config: EditorConfig;
_config: InitEditorConfig;
_storageTimeout?: ReturnType<typeof setTimeout>;
attrsOrig: any;
timedInterval?: ReturnType<typeof setTimeout>;
Expand Down Expand Up @@ -307,6 +308,10 @@ export default class EditorModel extends Model {
return this._config;
}

get version() {
return this.config.grapesjs?.version || '';
}

/**
* Get configurations
* @param {string} [prop] Property name
Expand Down
21 changes: 6 additions & 15 deletions packages/core/src/editor/view/EditorView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,26 @@ export default class EditorView extends View<EditorModel> {
}

private async sendTelemetryData() {
const hostName = getHostName();
const domain = getHostName();

if (hostName === 'localhost' || hostName.includes('localhost')) {
if (domain === 'localhost' || domain.includes('localhost')) {
// Don't send telemetry data for localhost
return;
}

const sessionKeyPrefix = 'gjs_telemetry_sent_';

// @ts-ignore
const version = __GJS_VERSION__;
const { version } = this.model;
const sessionKey = `${sessionKeyPrefix}${version}`;

if (sessionStorage.getItem(sessionKey)) {
// Telemetry already sent for version this session
return;
}

// @ts-ignore
const url = __STUDIO_URL__;
const path = '/api/gjs/telemetry/collect';

const response = await fetch(`${url}${path}`, {
const url = 'https://app.grapesjs.com';
const response = await fetch(`${url}/api/gjs/telemetry/collect`, {
method: 'POST',
body: JSON.stringify({
domain: hostName,
version,
url,
}),
body: JSON.stringify({ domain, version }),
});

if (!response.ok) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PluginManager, { Plugin, getPlugin, logPluginWarn } from './plugin_manage
import $ from './utils/cash-dom';
import polyfills from './utils/polyfills';

interface InitEditorConfig extends EditorConfig {
export interface InitEditorConfig extends EditorConfig {
grapesjs?: typeof grapesjs;
}

Expand Down Expand Up @@ -36,7 +36,7 @@ export const grapesjs = {
usePlugin,

// @ts-ignore Will be replaced on build
version: __GJS_VERSION__,
version: __GJS_VERSION__ as string,

/**
* Initialize the editor with passed options
Expand Down
1 change: 0 additions & 1 deletion packages/core/test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const localStorage = {

global._ = _;
global.__GJS_VERSION__ = '';
global.__STUDIO_URL__ = '';
global.grapesjs = require('./../src').default;
global.$ = global.grapesjs.$;
global.localStorage = localStorage;
9 changes: 2 additions & 7 deletions packages/core/test/specs/editor/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as hostUtil from '../../../src/utils/host-name';
jest.mock('../../../src/utils/host-name');

describe('Editor telemetry', () => {
const version = '1.0.0';
let fixture: HTMLElement;
let editorName = '';
let htmlString = '';
Expand All @@ -17,6 +18,7 @@ describe('Editor telemetry', () => {
let fetchMock: jest.Mock;

const initTestEditor = (config: Partial<EditorConfig>) => {
grapesjs.version = version;
const editor = grapesjs.init({
...config,
plugins: [fixJsDom, ...(config.plugins || [])],
Expand Down Expand Up @@ -88,7 +90,6 @@ describe('Editor telemetry', () => {
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toMatchObject({
domain: expect.any(String),
version: expect.any(String),
url: expect.any(String),
});
});

Expand All @@ -103,9 +104,6 @@ describe('Editor telemetry', () => {
});

test('Telemetry is not sent twice in the same session', async () => {
const version = '1.0.0';
(global as any).__GJS_VERSION__ = version;

window.sessionStorage.getItem = jest.fn(() => 'true');

const editor = initTestEditor({
Expand All @@ -132,9 +130,6 @@ describe('Editor telemetry', () => {
});

test('Telemetry cleans up old version keys', async () => {
const version = '1.0.0';
(global as any).__GJS_VERSION__ = version;

const sessionStorageMock = {
getItem: jest.fn(() => null),
setItem: jest.fn(),
Expand Down
1 change: 0 additions & 1 deletion packages/core/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ module.exports = ({ config, pkg, webpack }) => {
plugins: [
new webpack.DefinePlugin({
__GJS_VERSION__: `'${pkg.version}'`,
__STUDIO_URL__: `'${process.env.STUDIO_URL || 'http://localhost:3000'}'`,
}),
...config.plugins,
],
Expand Down

0 comments on commit 5941554

Please sign in to comment.