-
Notifications
You must be signed in to change notification settings - Fork 237
feat(compass-assistant): track opening and closing the drawer sections COMPASS-9884 #7372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -62,6 +62,16 @@ const DrawerOpenStateContext = | |||||||||||||||||||||||||||||
const DrawerSetOpenStateContext = | ||||||||||||||||||||||||||||||
React.createContext<DrawerSetOpenStateContextValue>(() => {}); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
type DrawerCurrentTabStateContextValue = string | null; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
type DrawerSetCurrentTabContextValue = (currentTab: string | null) => void; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const DrawerCurrentTabStateContext = | ||||||||||||||||||||||||||||||
React.createContext<DrawerCurrentTabStateContextValue>(null); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const DrawerSetCurrentTabContext = | ||||||||||||||||||||||||||||||
React.createContext<DrawerSetCurrentTabContextValue>(() => {}); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const DrawerActionsContext = React.createContext<DrawerActionsContextValue>({ | ||||||||||||||||||||||||||||||
current: { | ||||||||||||||||||||||||||||||
openDrawer: () => undefined, | ||||||||||||||||||||||||||||||
|
@@ -104,12 +114,16 @@ const DrawerActionsContext = React.createContext<DrawerActionsContextValue>({ | |||||||||||||||||||||||||||||
* ) | ||||||||||||||||||||||||||||||
* } | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
export const DrawerContentProvider: React.FunctionComponent = ({ | ||||||||||||||||||||||||||||||
children, | ||||||||||||||||||||||||||||||
}) => { | ||||||||||||||||||||||||||||||
export const DrawerContentProvider: React.FunctionComponent<{ | ||||||||||||||||||||||||||||||
onDrawerSectionOpen?: (drawerSectionId: string) => void; | ||||||||||||||||||||||||||||||
onDrawerSectionHide?: (drawerSectionId: string) => void; | ||||||||||||||||||||||||||||||
children?: React.ReactNode; | ||||||||||||||||||||||||||||||
}> = ({ onDrawerSectionOpen, onDrawerSectionHide, children }) => { | ||||||||||||||||||||||||||||||
const [drawerState, setDrawerState] = useState<DrawerSectionProps[]>([]); | ||||||||||||||||||||||||||||||
const [drawerOpenState, setDrawerOpenState] = | ||||||||||||||||||||||||||||||
useState<DrawerOpenStateContextValue>(false); | ||||||||||||||||||||||||||||||
const [drawerCurrentTab, setDrawerCurrentTab] = | ||||||||||||||||||||||||||||||
useState<DrawerCurrentTabStateContextValue>(null); | ||||||||||||||||||||||||||||||
const drawerActions = useRef({ | ||||||||||||||||||||||||||||||
openDrawer: () => undefined, | ||||||||||||||||||||||||||||||
closeDrawer: () => undefined, | ||||||||||||||||||||||||||||||
|
@@ -135,13 +149,36 @@ export const DrawerContentProvider: React.FunctionComponent = ({ | |||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const prevDrawerCurrentTabRef = React.useRef<string | null>(null); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||
if (drawerCurrentTab === prevDrawerCurrentTabRef.current) { | ||||||||||||||||||||||||||||||
// ignore unless it changed | ||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (drawerCurrentTab) { | ||||||||||||||||||||||||||||||
onDrawerSectionOpen?.(drawerCurrentTab); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (prevDrawerCurrentTabRef.current) { | ||||||||||||||||||||||||||||||
onDrawerSectionHide?.(prevDrawerCurrentTabRef.current); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
prevDrawerCurrentTabRef.current = drawerCurrentTab; | ||||||||||||||||||||||||||||||
}, [drawerCurrentTab, onDrawerSectionHide, onDrawerSectionOpen]); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||
<DrawerStateContext.Provider value={drawerState}> | ||||||||||||||||||||||||||||||
<DrawerOpenStateContext.Provider value={drawerOpenState}> | ||||||||||||||||||||||||||||||
<DrawerSetOpenStateContext.Provider value={setDrawerOpenState}> | ||||||||||||||||||||||||||||||
<DrawerActionsContext.Provider value={drawerActions}> | ||||||||||||||||||||||||||||||
{children} | ||||||||||||||||||||||||||||||
</DrawerActionsContext.Provider> | ||||||||||||||||||||||||||||||
<DrawerCurrentTabStateContext.Provider value={drawerCurrentTab}> | ||||||||||||||||||||||||||||||
<DrawerSetCurrentTabContext.Provider value={setDrawerCurrentTab}> | ||||||||||||||||||||||||||||||
<DrawerActionsContext.Provider value={drawerActions}> | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The nesting here is getting a bit wild. Can/should we not combine some of it? Is there a better way? I did kinda just copy what we're already doing for isOpen. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can group all actions together, but for state if we're putting it in one context, there would be no way to subscribe to changes to a single value instead of all of it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I guess it is OK then. |
||||||||||||||||||||||||||||||
{children} | ||||||||||||||||||||||||||||||
</DrawerActionsContext.Provider> | ||||||||||||||||||||||||||||||
</DrawerSetCurrentTabContext.Provider> | ||||||||||||||||||||||||||||||
</DrawerCurrentTabStateContext.Provider> | ||||||||||||||||||||||||||||||
</DrawerSetOpenStateContext.Provider> | ||||||||||||||||||||||||||||||
</DrawerOpenStateContext.Provider> | ||||||||||||||||||||||||||||||
</DrawerStateContext.Provider> | ||||||||||||||||||||||||||||||
|
@@ -152,11 +189,21 @@ const DrawerContextGrabber: React.FunctionComponent = ({ children }) => { | |||||||||||||||||||||||||||||
const drawerToolbarContext = useDrawerToolbarContext(); | ||||||||||||||||||||||||||||||
const actions = useContext(DrawerActionsContext); | ||||||||||||||||||||||||||||||
const openStateSetter = useContext(DrawerSetOpenStateContext); | ||||||||||||||||||||||||||||||
const currentTabSetter = useContext(DrawerSetCurrentTabContext); | ||||||||||||||||||||||||||||||
actions.current.openDrawer = drawerToolbarContext.openDrawer; | ||||||||||||||||||||||||||||||
actions.current.closeDrawer = drawerToolbarContext.closeDrawer; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||
openStateSetter(drawerToolbarContext.isDrawerOpen); | ||||||||||||||||||||||||||||||
}, [drawerToolbarContext.isDrawerOpen, openStateSetter]); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||
const currentTab = | ||||||||||||||||||||||||||||||
drawerToolbarContext.getActiveDrawerContent()?.id ?? null; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
currentTabSetter(currentTab); | ||||||||||||||||||||||||||||||
}, [drawerToolbarContext, currentTabSetter]); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Comment on lines
+200
to
+206
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This useEffect will run on every change to
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing we wouldn't want this because we want open/close even if it is the same tab? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There isn't a value we can check. There's a method. 🤷 |
||||||||||||||||||||||||||||||
return <>{children}</>; | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drive-by