Skip to content

Commit

Permalink
Merge pull request #24546 from storybookjs/dannyhw/fix-actions-get-id…
Browse files Browse the repository at this point in the history
…-crashes-react-native

Actions: Fix missing crypto module crashing React Native
  • Loading branch information
shilman authored Oct 22, 2023
2 parents 9657882 + 3072007 commit ea8dd8e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion code/addons/actions/src/runtime/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ const serializeArg = <T>(a: T) => {
return a;
};

// TODO react native doesn't have the crypto module, we should figure out a better way to generate these ids.
const generateId = () => {
return typeof crypto === 'object' && typeof crypto.getRandomValues === 'function'
? uuidv4()
: // pseudo random id, example response lo1e7zm4832bkr7yfl7
Date.now().toString(36) + Math.random().toString(36).substring(2);
};

export function action(name: string, options: ActionOptions = {}): HandlerFunction {
const actionOptions = {
...config,
Expand All @@ -47,7 +55,8 @@ export function action(name: string, options: ActionOptions = {}): HandlerFuncti

const handler = function actionHandler(...args: any[]) {
const channel = addons.getChannel();
const id = uuidv4();
// this makes sure that in js enviroments like react native you can still get an id
const id = generateId();
const minDepth = 5; // anything less is really just storybook internals
const serializedArgs = args.map(serializeArg);
const normalizedArgs = args.length > 1 ? serializedArgs : serializedArgs[0];
Expand Down

0 comments on commit ea8dd8e

Please sign in to comment.