gettingStartedWithProjectContext.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {createContext, useCallback} from 'react';
  2. import {OnboardingSelectedSDK, Project} from 'sentry/types';
  3. import {IssueAlertRule} from 'sentry/types/alerts';
  4. import {useSessionStorage} from 'sentry/utils/useSessionStorage';
  5. type GettingStartedWithProject = Pick<Project, 'name' | 'id'> & {
  6. alertRules: IssueAlertRule[];
  7. platform: OnboardingSelectedSDK;
  8. teamSlug?: Project['team']['slug'];
  9. };
  10. export type ProjectsContextProps = {
  11. setProject: (project: GettingStartedWithProject) => void;
  12. project?: GettingStartedWithProject;
  13. };
  14. export const GettingStartedWithProjectContext = createContext<ProjectsContextProps>({
  15. project: undefined,
  16. setProject: () => {},
  17. });
  18. type ProviderProps = {
  19. children: React.ReactNode;
  20. project?: GettingStartedWithProject;
  21. };
  22. export function GettingStartedWithProjectContextProvider({
  23. children,
  24. project,
  25. }: ProviderProps) {
  26. const [sessionStorage, setSessionStorage] = useSessionStorage<{
  27. project?: GettingStartedWithProject;
  28. }>('getting-started-with-project', {
  29. project,
  30. });
  31. const handleSetProject = useCallback(
  32. (newProject?: GettingStartedWithProject) => {
  33. setSessionStorage({project: newProject});
  34. },
  35. [setSessionStorage]
  36. );
  37. return (
  38. <GettingStartedWithProjectContext.Provider
  39. value={{
  40. project: sessionStorage.project,
  41. setProject: handleSetProject,
  42. }}
  43. >
  44. {children}
  45. </GettingStartedWithProjectContext.Provider>
  46. );
  47. }