Source

externalCode/deeplinksApi.js

  1. /**
  2. * @typedef {Function} ExternalLinkNavigation
  3. * @param {string} path
  4. * @param {Object} params
  5. * @param {Function} navigate
  6. * @return {boolean | null}
  7. */
  8. /**
  9. * @typedef {Function} DeeplinksWithoutEmbeddedCallback
  10. * @param {Boolean} defaultValue
  11. * @param {Object} linkObject Link object provided by API
  12. * @param {NavigationService} navigation
  13. * @return {Boolean}
  14. */
  15. /**
  16. * @typedef {Function} DeeplinksFallbackCallback
  17. * @param {boolean} defaultValue
  18. * @param {Object} linkObject Link object provided by API
  19. * @param {NavigationService} navigation
  20. * @return {boolean}
  21. */
  22. /**
  23. * @class
  24. * Deeplinking Hooks.
  25. * Instance name: deeplinksApi
  26. Hooks to modify deeplink behaviour/options when users tap on a link within the app.
  27. * @example
  28. * externalCodeSetup.deeplinksApi.METHOD_NAME
  29. */
  30. export class DeeplinksApi {
  31. deeplinksWithoutEmbeddedReturnValueFilter = (
  32. defaultValue,
  33. linkObject,
  34. navigation
  35. ) => defaultValue;
  36. /**
  37. * Sets a function which decides if deep linking will happen. If the function returns `true`, deep linking will not happen.
  38. * This is used to stop deep linking or to even try to match the route and fallback to open links in a WebView screen if the subject of navigation is not supported.
  39. * You can put conditions to use this behaviour across multiple areas or one specific area within the app.
  40. * @method
  41. * @param {DeeplinksWithoutEmbeddedCallback} deeplinksWithoutEmbeddedReturnValueFilter
  42. * @example <caption>Deep linking to profile screen will redirect to a url using page screen</caption>
  43. * externalCodeSetup.deeplinksApi.setDeeplinksWithoutEmbeddedReturnValueFilter((defaultValue, linkObject, navigationService) => {
  44. * if (linkObject.action === "open_member_profile") {
  45. * navigationService.navigate({
  46. * routeName: "PageScreen",
  47. * params: {
  48. * url: "https://link-to-url.com"
  49. * }
  50. * })
  51. * }
  52. * return true;
  53. * });
  54. */
  55. setDeeplinksWithoutEmbeddedReturnValueFilter = deeplinksWithoutEmbeddedReturnValueFilter => {
  56. this.deeplinksWithoutEmbeddedReturnValueFilter = deeplinksWithoutEmbeddedReturnValueFilter;
  57. };
  58. deeplinksReturnValueFilter = (defaultValue, linkObject, navigation) =>
  59. defaultValue;
  60. /**
  61. * Sets a function which decides what happens if deep linking fails to match the route.
  62. * If the function returns `true` and deep linking fails, navigating to the WebView screen (as a default fallback) will not happen.
  63. * @method
  64. * @param {DeeplinksWithoutEmbeddedCallback} deeplinksWithoutEmbeddedReturnValueFilter
  65. * @example <caption>Deep linking to route "inapp" fails so user would like to redirect to a different page instead</caption>
  66. * externalCodeSetup.deeplinksApi.setDeeplinksReturnValueFilter((defaultValue, linkObject, navigationService) => {
  67. * if (linkObject.action === "inapp") {
  68. * navigationService.navigate({
  69. * routeName: "PageScreen",
  70. * params: {
  71. * url: "https://link-to-url.com"
  72. * }
  73. * })
  74. * }
  75. * return true;
  76. * });
  77. */
  78. setDeeplinksReturnValueFilter = deeplinksReturnValueFilter => {
  79. this.deeplinksReturnValueFilter = deeplinksReturnValueFilter;
  80. };
  81. externalLinkNavigate = null;
  82. /**
  83. * @deprecated
  84. * @method
  85. * @param {ExternalLinkNavigation} externalLinkNavigate
  86. */
  87. setExternalLinkNavigate = externalLinkNavigate => {
  88. this.externalLinkNavigate = externalLinkNavigate;
  89. };
  90. }