routing.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { useHistory, useLocation } from "react-router";
  2. export type PorterUrl =
  3. | "dashboard"
  4. | "launch"
  5. | "integrations"
  6. | "new-project"
  7. | "cluster-dashboard"
  8. | "infrastructure"
  9. | "project-settings"
  10. | "applications"
  11. | "env-groups"
  12. | "jobs"
  13. | "onboarding"
  14. | "databases"
  15. | "preview-environments"
  16. | "apps"
  17. | "addons"
  18. | "compliance"
  19. | "environment-groups"
  20. | "stacks";
  21. export const PorterUrls = [
  22. "dashboard",
  23. "launch",
  24. "integrations",
  25. "new-project",
  26. "cluster-dashboard",
  27. "project-settings",
  28. "infrastructure",
  29. "applications",
  30. "env-groups",
  31. "jobs",
  32. "onboarding",
  33. "databases",
  34. "datastores",
  35. "preview-environments",
  36. "apps",
  37. "addons",
  38. "compliance",
  39. "environment-groups",
  40. "stacks",
  41. ];
  42. // TODO: consolidate with pushFiltered
  43. export const pushQueryParams = (
  44. props: any,
  45. params: any,
  46. removedParams?: string[]
  47. ) => {
  48. let { location, history } = props;
  49. const urlParams = new URLSearchParams(location.search);
  50. Object.keys(params)?.forEach((key: string) => {
  51. params[key] && urlParams.set(key, params[key]);
  52. });
  53. removedParams?.map((deletedParam) => urlParams.delete(deletedParam));
  54. history.push({
  55. pathname: location.pathname,
  56. search: urlParams.toString(),
  57. });
  58. };
  59. export const pushFiltered = (
  60. props: any, // Props for retrieving history and location
  61. pathname: string, // Path to redirect to
  62. keys: string[], // Query params to preserve during redirect
  63. params?: any
  64. ) => {
  65. let { location, history } = props;
  66. let urlParams = new URLSearchParams(location.search);
  67. let newUrlParams = new URLSearchParams("");
  68. keys?.forEach((key: string) => {
  69. let value = urlParams.get(key);
  70. value && newUrlParams.set(key, value);
  71. });
  72. params &&
  73. Object.keys(params)?.forEach((key: string) => {
  74. params[key] && newUrlParams.set(key, params[key]);
  75. });
  76. history.push({
  77. pathname,
  78. search: newUrlParams.toString(),
  79. });
  80. };
  81. export const getQueryParams = (props: any) => {
  82. const searchParams = props.location.search;
  83. return new URLSearchParams(searchParams);
  84. };
  85. export const getQueryParam = (props: any, paramName: string) => {
  86. const searchParams = getQueryParams(props);
  87. return searchParams?.get(paramName);
  88. };
  89. export const useRouting = () => {
  90. const location = useLocation();
  91. const history = useHistory();
  92. return {
  93. push(path: string, state?: any) {
  94. history.push(path, state);
  95. },
  96. pushQueryParams: (
  97. params: { [key: string]: unknown },
  98. removedParams?: string[]
  99. ) => {
  100. return pushQueryParams({ location, history }, params, removedParams);
  101. },
  102. pushFiltered: (
  103. pathname: string,
  104. keys: string[],
  105. params?: { [key: string]: unknown }
  106. ) => {
  107. return pushFiltered({ location, history }, pathname, keys, params);
  108. },
  109. getQueryParams: () => {
  110. return getQueryParams({ location });
  111. },
  112. getQueryParam: (paramName: string) => {
  113. return getQueryParam({ location }, paramName);
  114. },
  115. };
  116. };