UNPKG

@metamask/snaps-simulation

Version:

A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment

1 lines 4.48 kB
{"version":3,"file":"notifications.cjs","sourceRoot":"","sources":["../../../src/methods/hooks/notifications.ts"],"names":[],"mappings":";;;AACA,mDAAuD;AACvD,8CAA0C;AAE1C,gDAAyC;AAGzC,iDAA4D;AAE5D;;;;;;;;GAQG;AACH,QAAQ,CAAC,CAAC,oCAAoC,CAC5C,OAAe,EACf,EAAE,OAAO,EAAgB;IAEzB,MAAM,IAAA,aAAG,EACP,IAAA,uBAAe,EAAC,EAAE,EAAE,EAAE,IAAA,gBAAM,GAAE,EAAE,IAAI,EAAE,4BAAgB,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC,CAC1E,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;GAKG;AACH,SAAgB,uCAAuC,CACrD,OAAwB;IAExB,OAAO,KAAK,EACV,GAAG,IAA6D,EAChE,EAAE;QACF,OAAO,MAAM,OAAO,CAClB,oCAAoC,EACpC,GAAG,IAAI,CACR,CAAC,SAAS,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAXD,0FAWC;AAUD;;;;;;;;;;;GAWG;AACH,QAAQ,CAAC,CAAC,mCAAmC,CAC3C,OAAe,EACf,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAA2B;IAEhE,IAAI,OAAO,EAAE,CAAC;QACZ,MAAM,IAAA,aAAG,EAAC,IAAA,oBAAY,EAAC,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IACjE,CAAC;IAED,MAAM,IAAA,aAAG,EACP,IAAA,uBAAe,EAAC;QACd,EAAE,EAAE,IAAA,gBAAM,GAAE;QACZ,IAAI,EAAE,4BAAgB,CAAC,KAAK;QAC5B,OAAO;QACP,KAAK;QACL,OAAO;QACP,UAAU;KACX,CAAC,CACH,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;GAKG;AACH,SAAgB,sCAAsC,CACpD,OAAwB;IAExB,OAAO,KAAK,EACV,GAAG,IAA4D,EAC/D,EAAE;QACF,OAAO,MAAM,OAAO,CAClB,mCAAmC,EACnC,GAAG,IAAI,CACR,CAAC,SAAS,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAXD,wFAWC","sourcesContent":["import type { NotifyParams } from '@metamask/snaps-sdk';\nimport { NotificationType } from '@metamask/snaps-sdk';\nimport { nanoid } from '@reduxjs/toolkit';\nimport type { SagaIterator } from 'redux-saga';\nimport { put } from 'redux-saga/effects';\n\nimport type { RunSagaFunction } from '../../store';\nimport { addNotification, setInterface } from '../../store';\n\n/**\n * Show a native notification to the user.\n *\n * @param _snapId - The ID of the Snap that created the notification.\n * @param options - The notification options.\n * @param options.message - The message to show in the notification.\n * @yields Adds the notification to the store.\n * @returns `null`.\n */\nfunction* showNativeNotificationImplementation(\n _snapId: string,\n { message }: NotifyParams,\n): SagaIterator {\n yield put(\n addNotification({ id: nanoid(), type: NotificationType.Native, message }),\n );\n\n return null;\n}\n\n/**\n * Get a method that can be used to show a native notification.\n *\n * @param runSaga - A function to run a saga outside the usual Redux flow.\n * @returns A method that can be used to show a native notification.\n */\nexport function getShowNativeNotificationImplementation(\n runSaga: RunSagaFunction,\n) {\n return async (\n ...args: Parameters<typeof showNativeNotificationImplementation>\n ) => {\n return await runSaga(\n showNativeNotificationImplementation,\n ...args,\n ).toPromise();\n };\n}\n\ntype InAppNotificationParams = {\n type: NotificationType;\n message: string;\n title?: string | undefined;\n content?: string | undefined;\n footerLink?: { text: string; href: string } | undefined;\n};\n\n/**\n * Show an in-app notification to the user.\n *\n * @param _snapId - The ID of the Snap that created the notification.\n * @param options - The notification options.\n * @param options.message - The message to show in the notification.\n * @param options.title - The title to show in the notification.\n * @param options.content - The JSX content to show in the notification.\n * @param options.footerLink - The footer to show in the notification.\n * @yields Adds the notification to the store.\n * @returns `null`.\n */\nfunction* showInAppNotificationImplementation(\n _snapId: string,\n { message, title, content, footerLink }: InAppNotificationParams,\n): SagaIterator<null> {\n if (content) {\n yield put(setInterface({ type: 'Notification', id: content }));\n }\n\n yield put(\n addNotification({\n id: nanoid(),\n type: NotificationType.InApp,\n message,\n title,\n content,\n footerLink,\n }),\n );\n\n return null;\n}\n\n/**\n * Get a method that can be used to show an in-app notification.\n *\n * @param runSaga - A function to run a saga outside the usual Redux flow.\n * @returns A method that can be used to show an in-app notification.\n */\nexport function getShowInAppNotificationImplementation(\n runSaga: RunSagaFunction,\n) {\n return async (\n ...args: Parameters<typeof showInAppNotificationImplementation>\n ) => {\n return await runSaga(\n showInAppNotificationImplementation,\n ...args,\n ).toPromise();\n };\n}\n"]}