gettingStartedWithProjectContext.tsx 1.5 KB

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