routing.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Location } from "history";
  2. export type PorterUrl =
  3. | "dashboard"
  4. | "launch"
  5. | "integrations"
  6. | "new-project"
  7. | "cluster-dashboard"
  8. | "project-settings"
  9. | "applications"
  10. | "env-groups"
  11. | "jobs";
  12. export const PorterUrls = [
  13. "dashboard",
  14. "launch",
  15. "integrations",
  16. "new-project",
  17. "cluster-dashboard",
  18. "project-settings",
  19. "applications",
  20. "env-groups",
  21. "jobs",
  22. ];
  23. // TODO: consolidate with pushFiltered
  24. export const pushQueryParams = (props: any, params: any) => {
  25. let { location, history } = props;
  26. const urlParams = new URLSearchParams(location.search);
  27. Object.keys(params)?.forEach((key: string) => {
  28. params[key] && urlParams.set(key, params[key]);
  29. });
  30. history.push({
  31. pathname: location.pathname,
  32. search: urlParams.toString(),
  33. });
  34. };
  35. export const pushFiltered = (
  36. props: any, // Props for retrieving history and location
  37. pathname: string, // Path to redirect to
  38. keys: string[], // Query params to preserve during redirect
  39. params?: any
  40. ) => {
  41. let { location, history } = props;
  42. let urlParams = new URLSearchParams(location.search);
  43. let newUrlParams = new URLSearchParams("");
  44. keys?.forEach((key: string) => {
  45. let value = urlParams.get(key);
  46. value && newUrlParams.set(key, value);
  47. });
  48. params &&
  49. Object.keys(params)?.forEach((key: string) => {
  50. params[key] && newUrlParams.set(key, params[key]);
  51. });
  52. history.push({
  53. pathname,
  54. search: newUrlParams.toString(),
  55. });
  56. };
  57. export const getQueryParams = (props: any) => {
  58. const searchParams = props.location.search;
  59. if (searchParams) {
  60. return new URLSearchParams(searchParams);
  61. }
  62. };
  63. export const getQueryParam = (props: any, paramName: string) => {
  64. const searchParams = getQueryParams(props);
  65. return searchParams?.get(paramName);
  66. };