routing.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. export const PorterUrls = [
  16. "dashboard",
  17. "launch",
  18. "integrations",
  19. "new-project",
  20. "cluster-dashboard",
  21. "project-settings",
  22. "infrastructure",
  23. "applications",
  24. "env-groups",
  25. "jobs",
  26. "onboarding",
  27. "databases",
  28. ];
  29. // TODO: consolidate with pushFiltered
  30. export const pushQueryParams = (
  31. props: any,
  32. params: any,
  33. removedParams?: string[]
  34. ) => {
  35. let { location, history } = props;
  36. const urlParams = new URLSearchParams(location.search);
  37. Object.keys(params)?.forEach((key: string) => {
  38. params[key] && urlParams.set(key, params[key]);
  39. });
  40. removedParams?.map((deletedParam) => urlParams.delete(deletedParam));
  41. history.push({
  42. pathname: location.pathname,
  43. search: urlParams.toString(),
  44. });
  45. };
  46. export const pushFiltered = (
  47. props: any, // Props for retrieving history and location
  48. pathname: string, // Path to redirect to
  49. keys: string[], // Query params to preserve during redirect
  50. params?: any
  51. ) => {
  52. let { location, history } = props;
  53. let urlParams = new URLSearchParams(location.search);
  54. let newUrlParams = new URLSearchParams("");
  55. keys?.forEach((key: string) => {
  56. let value = urlParams.get(key);
  57. value && newUrlParams.set(key, value);
  58. });
  59. params &&
  60. Object.keys(params)?.forEach((key: string) => {
  61. params[key] && newUrlParams.set(key, params[key]);
  62. });
  63. history.push({
  64. pathname,
  65. search: newUrlParams.toString(),
  66. });
  67. };
  68. export const getQueryParams = (props: any) => {
  69. const searchParams = props.location.search;
  70. return new URLSearchParams(searchParams);
  71. };
  72. export const getQueryParam = (props: any, paramName: string) => {
  73. const searchParams = getQueryParams(props);
  74. return searchParams?.get(paramName);
  75. };
  76. export const useRouting = () => {
  77. const location = useLocation();
  78. const history = useHistory();
  79. return {
  80. pushQueryParams: (
  81. params: { [key: string]: unknown },
  82. removedParams?: string[]
  83. ) => {
  84. return pushQueryParams({ location, history }, params, removedParams);
  85. },
  86. pushFiltered: (
  87. pathname: string,
  88. keys: string[],
  89. params?: { [key: string]: unknown }
  90. ) => {
  91. return pushFiltered({ location, history }, pathname, keys, params);
  92. },
  93. getQueryParams: () => {
  94. return getQueryParams({ location });
  95. },
  96. getQueryParam: (paramName: string) => {
  97. return getQueryParam({ location }, paramName);
  98. },
  99. };
  100. };