Skip to content

Commit

Permalink
chore(Queries): crypto helper [YTFRONT-4612]
Browse files Browse the repository at this point in the history
  • Loading branch information
SimbiozizV committed Feb 5, 2025
1 parent 63dfad4 commit d493ff6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {RootState} from '../../../../store/reducers';
import {wrapApiPromiseByToaster} from '../../../../utils/utils';
import {YTApiId, ytApiV4Id} from '../../../../rum/rum-wrap-api';
import {setContextId} from './neuralNetworkSlice';
import {makeRandomUUID} from '../../utils/crypto';

export const createContextId =
(queryId: string): ThunkAction<void, RootState, unknown, any> =>
async (dispatch) => {
const newContextId = crypto.randomUUID();
const newContextId = makeRandomUUID();

await wrapApiPromiseByToaster(
ytApiV4Id.alterQuery(YTApiId.alterQuery, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
} from '../query-tracker-contants';
import {loadVisualization} from '../queryChart/actions';
import {selectNeuralNetworkContextId} from '../neuralNetwork/selectors';
import {makeRandomUUID} from '../../utils/crypto';

export const setCurrentClusterToQuery =
(): ThunkAction<void, RootState, unknown, any> => (dispatch, getState) => {
Expand Down Expand Up @@ -225,7 +226,7 @@ export function createEmptyQuery(
const state = getState();
const defaultQueryACO = getDefaultQueryACO(state);

dispatch(setContextId(crypto.randomUUID()));
dispatch(setContextId(makeRandomUUID()));
dispatch({
type: SET_QUERY,
data: {
Expand Down
12 changes: 12 additions & 0 deletions packages/ui/src/ui/pages/query-tracker/utils/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const makeRandomUUID = () => {
try {
return crypto.randomUUID();
} catch (e) {
// http and old browsers
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (item) => {
const random = Math.floor(Math.random() * 16);
const newValue = item === 'x' ? random : (random % 4) + 8;
return newValue.toString(16);
});
}
};

0 comments on commit d493ff6

Please sign in to comment.