Skip to content

Commit

Permalink
Refactor element-util.ts and common.ts
Browse files Browse the repository at this point in the history
- Import generateUUID from @dhruv-techapps/core-common
- Replace crypto.randomUUID() with generateUUID() for generating UUIDs
- Remove unused RANDOM_UUID type from common.ts
- Update imports in action-model.ts and config-model.ts to use generateUUID from @dhruv-techapps/core-common
- Update imports in notifications.service.ts to use generateUUID from @dhruv-techapps/core-common
- Update imports in google-analytics-background.ts and sandbox.ts to use generateUUID from @dhruv-techapps/core-common
  • Loading branch information
dharmesh-hemaram committed Oct 4, 2024
1 parent 5d58967 commit ee09fd7
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 19 deletions.
16 changes: 9 additions & 7 deletions apps/acf-extension/src/wizard/element-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GoogleAnalyticsService } from '@dhruv-techapps/google-analytics';
import { generateUUID } from '@dhruv-techapps/core-common';
import { BUTTON_FILE_SUBMIT_NODE_NAME, RADIO_CHECKBOX_NODE_NAME } from '../common/constant';
import { xPath } from './dom-path';
import { WizardAction } from './type';
Expand All @@ -7,6 +8,7 @@ type selectType = {
elementValue?: string;
optionValue?: string;
};

export const WizardElementUtil = (() => {
const radioCheckbox = (element: HTMLInputElement): boolean => element.checked;

Expand Down Expand Up @@ -56,7 +58,7 @@ export const WizardElementUtil = (() => {
// Input Element
if (RADIO_CHECKBOX_NODE_NAME.test(element.type)) {
const checked = radioCheckbox(element);
return { name: getName(element), id: crypto.randomUUID(), elementFinder, checked, elementValue: element.value, elementType: element.type };
return { name: getName(element), id: generateUUID(), elementFinder, checked, elementValue: element.value, elementType: element.type };
}

let value;
Expand All @@ -71,32 +73,32 @@ export const WizardElementUtil = (() => {
}

if (value !== null && value !== undefined) {
return { name: getName(element), id: crypto.randomUUID(), elementFinder, value, elementValue, elementType: element.type };
return { name: getName(element), id: generateUUID(), elementFinder, value, elementValue, elementType: element.type };
}
} else if (element instanceof HTMLButtonElement && listener) {
// Button
return { name: getName(element), id: crypto.randomUUID(), elementFinder, value: '', elementValue: element.innerText };
return { name: getName(element), id: generateUUID(), elementFinder, value: '', elementValue: element.innerText };
} else if (element instanceof HTMLSelectElement) {
// Select Dropdown
const selectType = listener ? await optionListener(element) : select(element);
if (selectType) {
return { name: getName(element), id: crypto.randomUUID(), elementFinder: elementFinder, value: selectType.elementValue, elementValue: selectType.elementValue };
return { name: getName(element), id: generateUUID(), elementFinder: elementFinder, value: selectType.elementValue, elementValue: selectType.elementValue };
}
} else if (element instanceof HTMLTextAreaElement) {
// Textarea && Editable Content
const value = listener ? await inputListener(element) : element.value;
if (value) {
return { name: getName(element), id: crypto.randomUUID(), elementFinder, value };
return { name: getName(element), id: generateUUID(), elementFinder, value };
}
} else if (element.isContentEditable && listener) {
GoogleAnalyticsService.fireEvent('isContentEditable', { event: 'Wizard' });
const value = await inputListener(element);
if (value) {
return { elementFinder, id: crypto.randomUUID(), value };
return { elementFinder, id: generateUUID(), value };
}
} else {
// Other
return { id: crypto.randomUUID(), elementFinder };
return { id: generateUUID(), elementFinder };
}
return;
};
Expand Down
1 change: 0 additions & 1 deletion libs/acf/common/src/lib/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export type RANDOM_UUID = `${string}-${string}-${string}-${string}-${string}`;
export const CONTEXT_MENU_ELEMENT_ID = 'element-mode';
export const CONTEXT_MENU_FORM_ID = 'form-mode';
export const ACTION_POPUP = 'action-popup';
Expand Down
7 changes: 4 additions & 3 deletions libs/acf/common/src/lib/model/action-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RANDOM_UUID } from '../common';
import { generateUUID, RANDOM_UUID } from '@dhruv-techapps/core-common';

import { Addon } from './addon-model';
import { RETRY_OPTIONS } from './setting-model';

Expand Down Expand Up @@ -28,7 +29,7 @@ export type ActionCondition = {
};

export const getDefaultActionCondition = (operator?: ACTION_CONDITION_OPR): ActionCondition => ({
id: crypto.randomUUID(),
id: generateUUID(),
actionIndex: -1,
status: ACTION_STATUS['~~ Select STATUS ~~'],
operator,
Expand Down Expand Up @@ -78,7 +79,7 @@ export type Action = {
};

export const getDefaultAction = (): Action => ({
id: crypto.randomUUID(),
id: generateUUID(),
elementFinder: '',
error: ['elementFinder'],
});
5 changes: 3 additions & 2 deletions libs/acf/common/src/lib/model/config-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RANDOM_UUID } from '../common';
import { generateUUID, RANDOM_UUID } from '@dhruv-techapps/core-common';

import { Action, getDefaultAction } from './action-model';
import { Batch } from './batch-model';

Expand Down Expand Up @@ -56,7 +57,7 @@ export type Configuration = {
export const getDefaultConfig = (source?: CONFIG_SOURCE, actions?: Array<Action>): Configuration => ({
url: '',
source,
id: crypto.randomUUID(),
id: generateUUID(),
enable: true,
startType: START_TYPES.AUTO,
loadType: LOAD_TYPES.WINDOW,
Expand Down
14 changes: 14 additions & 0 deletions libs/core/common/src/lib/utilities/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,17 @@ export const getRandomValues = () => {
const array = new Uint32Array(1);
return crypto.getRandomValues(array).toString();
};

export type RANDOM_UUID = `${string}-${string}-${string}-${string}-${string}`;

export const generateUUID = (): RANDOM_UUID => {
// Generate a random UUID (version 4)
return (
crypto.randomUUID() ||
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
})
);
};
7 changes: 4 additions & 3 deletions libs/core/service/src/lib/notifications.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { generateUUID } from '@dhruv-techapps/core-common';
import { NotificationsRequest } from '@dhruv-techapps/core-extension';
import { CoreService } from './service';

export class NotificationsService extends CoreService {
static async create(options: chrome.notifications.NotificationOptions<true>, notificationId: string = crypto.randomUUID()) {
static async create(options: chrome.notifications.NotificationOptions<true>, notificationId: string = generateUUID()) {
return await this.message<NotificationsRequest>({ messenger: 'notifications', message: { notificationId, options }, methodName: 'create' });
}

static async update(options: chrome.notifications.NotificationOptions<false>, notificationId: string = crypto.randomUUID()) {
static async update(options: chrome.notifications.NotificationOptions<false>, notificationId: string = generateUUID()) {
return await this.message<NotificationsRequest>({ messenger: 'notifications', message: { notificationId, options }, methodName: 'update' });
}

static async clear(notificationId: string = crypto.randomUUID()) {
static async clear(notificationId: string = generateUUID()) {
return await this.message<NotificationsRequest>({ messenger: 'notifications', message: { notificationId }, methodName: 'clear' });
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { generateUUID } from '@dhruv-techapps/core-common';
import { DEFAULT_ENGAGEMENT_TIME_MSEC, GA_DEBUG_ENDPOINT, GA_ENDPOINT, SESSION_EXPIRATION_IN_MIN } from './google-analytics.constant';
import { FireErrorEventParams, FireEventParams, FirePageViewEventParams } from './google-analytics.types';

Expand All @@ -20,7 +21,7 @@ export class GoogleAnalyticsBackground {
let { clientId } = await chrome.storage.local.get('clientId');
if (!clientId) {
// Generate a unique client ID, the actual value is not relevant
clientId = self.crypto.randomUUID();
clientId = generateUUID();
await chrome.storage.local.set({ clientId });
}
return clientId;
Expand Down
4 changes: 2 additions & 2 deletions libs/shared/sandbox/src/lib/sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigError } from '@dhruv-techapps/core-common';
import { ConfigError, generateUUID } from '@dhruv-techapps/core-common';

export const SANDBOX_INITIALIZED = 'sandbox-initialized';

Expand Down Expand Up @@ -54,7 +54,7 @@ export const Sandbox = (() => {
if (!code) {
return context;
}
const name = crypto.randomUUID();
const name = generateUUID();
try {
return await Sandbox.sendMessage({ command: 'eval', name, context: context ? `\`${context}\`.${code}` : code });
} catch (error) {
Expand Down

0 comments on commit ee09fd7

Please sign in to comment.