relocationOnboardingContext.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. };
  8. export type RelocationOnboardingContextProps = {
  9. data: Data;
  10. setData: (data: Data) => void;
  11. };
  12. export const RelocationOnboardingContext =
  13. createContext<RelocationOnboardingContextProps>({
  14. data: {
  15. orgSlugs: '',
  16. regionUrl: '',
  17. file: undefined,
  18. },
  19. setData: () => {},
  20. });
  21. type ProviderProps = {
  22. children: React.ReactNode;
  23. value?: Data;
  24. };
  25. export function RelocationOnboardingContextProvider({children, value}: ProviderProps) {
  26. const [sessionStorage, setSessionStorage] = useSessionStorage<Data>(
  27. 'relocationOnboarding',
  28. {
  29. orgSlugs: value?.orgSlugs || '',
  30. regionUrl: value?.regionUrl || '',
  31. file: value?.file || undefined,
  32. }
  33. );
  34. const setData = useCallback(
  35. (data: Data) => {
  36. setSessionStorage(data);
  37. },
  38. [setSessionStorage]
  39. );
  40. return (
  41. <RelocationOnboardingContext.Provider
  42. value={{
  43. data: sessionStorage,
  44. setData,
  45. }}
  46. >
  47. {children}
  48. </RelocationOnboardingContext.Provider>
  49. );
  50. }