newTabContext.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {createContext, useState} from 'react';
  2. export type NewView = {
  3. label: string;
  4. query: string;
  5. /**
  6. * If true, the query will be saved to the view.
  7. * Otherwise, the query will act as an unsaved change.
  8. */
  9. saveQueryToView: boolean;
  10. };
  11. export interface NewTabContext {
  12. newViewActive: boolean;
  13. onNewViewsSaved: (newViews: NewView[]) => void;
  14. setNewViewActive: (isActive: boolean) => void;
  15. setOnNewViewsSaved: (onNewViewSaved: (newViews: NewView[]) => void) => void;
  16. }
  17. export const NewTabContext = createContext<NewTabContext>({
  18. newViewActive: false,
  19. setNewViewActive: () => {},
  20. onNewViewsSaved: () => {},
  21. setOnNewViewsSaved: () => {},
  22. });
  23. export function NewTabContextProvider({children}: {children: React.ReactNode}) {
  24. const [newViewActive, setNewViewActive] = useState<boolean>(false);
  25. const [onNewViewsSaved, setOnNewViewsSaved] = useState<
  26. NewTabContext['onNewViewsSaved']
  27. >(() => () => {});
  28. return (
  29. <NewTabContext.Provider
  30. value={{
  31. newViewActive,
  32. setNewViewActive,
  33. onNewViewsSaved,
  34. setOnNewViewsSaved,
  35. }}
  36. >
  37. {children}
  38. </NewTabContext.Provider>
  39. );
  40. }