You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.5KB

  1. import { Dialog, DialogActions, DialogContent, Button } from "@mui/material";
  2. import { HasChildren } from "@types";
  3. import { PageID, TabID } from "pages";
  4. import { createContext, useState } from "react";
  5. type ContextProps = {
  6. pageId: PageID;
  7. tabId: TabID;
  8. setPageId: (pageId: PageID) => void;
  9. setTabId: (tabId: TabID) => void;
  10. openDialog: (message: string) => void;
  11. };
  12. const contextInit: ContextProps = {
  13. pageId: PageID.NONE,
  14. tabId: TabID.NONE,
  15. setPageId: (pageId: PageID) => {},
  16. setTabId: (tabId: TabID) => {},
  17. openDialog: (message: string) => {},
  18. };
  19. export const PageContext = createContext(contextInit);
  20. type Props = HasChildren;
  21. export function PageContextProvider({ children }: Props) {
  22. const [pageId, setPageId] = useState<PageID>(PageID.NONE);
  23. const [tabId, setTabId] = useState<TabID>(TabID.NONE);
  24. const [open, setOpen] = useState(false);
  25. const [dialogMessage, setDialogMessage] = useState("");
  26. const openDialog = (message: string) => {
  27. setOpen(true);
  28. setDialogMessage(message);
  29. };
  30. const close = () => {
  31. setOpen(false);
  32. };
  33. return (
  34. <PageContext.Provider
  35. value={{
  36. pageId,
  37. tabId,
  38. setPageId,
  39. setTabId,
  40. openDialog,
  41. }}
  42. >
  43. {children}
  44. <Dialog open={open} onClose={close}>
  45. <DialogContent>{dialogMessage}</DialogContent>
  46. <DialogActions>
  47. <Button onClick={close}>閉じる</Button>
  48. </DialogActions>
  49. </Dialog>
  50. </PageContext.Provider>
  51. );
  52. }