relocationOnboardingContext.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {createContext, useCallback} from 'react';
  2. import {useSessionStorage} from 'sentry/utils/useSessionStorage';
  3. type Data = {
  4. orgSlugs: string;
  5. regionUrl: string;
  6. file?: File;
  7. promoCode?: string;
  8. };
  9. export type RelocationOnboardingContextProps = {
  10. data: Data;
  11. setData: (data: Data) => void;
  12. };
  13. export const RelocationOnboardingContext =
  14. createContext<RelocationOnboardingContextProps>({
  15. data: {
  16. orgSlugs: '',
  17. regionUrl: '',
  18. file: undefined,
  19. promoCode: '',
  20. },
  21. setData: () => {},
  22. });
  23. type ProviderProps = {
  24. children: React.ReactNode;
  25. value?: Data;
  26. };
  27. export function RelocationOnboardingContextProvider({children, value}: ProviderProps) {
  28. const [sessionStorage, setSessionStorage] = useSessionStorage<Data>(
  29. 'relocationOnboarding',
  30. {
  31. orgSlugs: value?.orgSlugs || '',
  32. regionUrl: value?.regionUrl || '',
  33. promoCode: value?.promoCode || '',
  34. file: value?.file || undefined,
  35. }
  36. );
  37. const setData = useCallback(
  38. (data: Data) => {
  39. setSessionStorage(data);
  40. },
  41. [setSessionStorage]
  42. );
  43. return (
  44. <RelocationOnboardingContext.Provider
  45. value={{
  46. data: sessionStorage,
  47. setData,
  48. }}
  49. >
  50. {children}
  51. </RelocationOnboardingContext.Provider>
  52. );
  53. }