-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add research/search mode toggle with config and feature flag (#779) #782
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
74affc1
feat: add research/search mode toggle with config and feature flag (#…
frano-m e784324
test: add togglebuttongrouprovider and modeprovider tests (#779)
frano-m 50f6ce0
docs: add jsdoc to togglebuttongroup component (#779)
frano-m dc51f9c
refactor: omit children from togglebuttongroup props (#779)
frano-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/components/Layout/components/Sidebar/components/SidebarTools/sidebarTools.styles.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { createContext } from "react"; | ||
| import { ToggleButtonGroupContextProps } from "./types"; | ||
|
|
||
| export const ToggleButtonGroupContext = createContext< | ||
| ToggleButtonGroupContextProps<unknown> | ||
| >({ | ||
| onChange: undefined, | ||
| value: null, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { ToggleButtonGroupProps } from "@mui/material"; | ||
| import { useContext } from "react"; | ||
| import { ToggleButtonGroupContext } from "./context"; | ||
| import { ToggleButtonGroupContextProps } from "./types"; | ||
|
|
||
| /** | ||
| * Returns the toggle button group context. | ||
| * @returns toggle button group context. | ||
| */ | ||
| export const useToggleButtonGroup = < | ||
| T extends ToggleButtonGroupProps["value"], | ||
| >(): ToggleButtonGroupContextProps<T> => { | ||
| return useContext( | ||
| ToggleButtonGroupContext, | ||
| ) as ToggleButtonGroupContextProps<T>; | ||
| }; |
34 changes: 34 additions & 0 deletions
34
src/components/common/ToggleButtonGroup/provider/provider.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { ToggleButtonGroupProps } from "@mui/material"; | ||
| import { JSX, MouseEvent, useCallback, useState } from "react"; | ||
| import { ToggleButtonGroupContext } from "./context"; | ||
| import { ToggleButtonGroupProviderProps } from "./types"; | ||
|
|
||
| /** | ||
| * ToggleButtonGroup provider component. | ||
| * Manages toggle button group state for child components. | ||
| * @param props - Component props. | ||
| * @param props.children - Child elements to render. | ||
| * @param props.initialValue - Initial value for the toggle button group. | ||
| * @returns ToggleButtonGroup provider component. | ||
| */ | ||
| export function ToggleButtonGroupProvider< | ||
| T extends ToggleButtonGroupProps["value"], | ||
| >({ | ||
| children, | ||
| initialValue = null, | ||
| }: ToggleButtonGroupProviderProps<T>): JSX.Element { | ||
| const [value, setValue] = useState<T | null>(initialValue); | ||
|
|
||
| const onChange = useCallback( | ||
| (_: MouseEvent<HTMLElement>, value: T) => setValue(value), | ||
| [], | ||
| ); | ||
|
|
||
| return ( | ||
| <ToggleButtonGroupContext.Provider value={{ onChange, value }}> | ||
| {typeof children === "function" | ||
| ? children({ onChange, value }) | ||
| : children} | ||
| </ToggleButtonGroupContext.Provider> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { ToggleButtonGroupProps } from "@mui/material"; | ||
| import { ReactNode } from "react"; | ||
|
|
||
| export type ToggleButtonGroupContextProps< | ||
| T extends ToggleButtonGroupProps["value"], | ||
| > = Pick<ToggleButtonGroupProps, "onChange"> & { | ||
| value: T | null; | ||
| }; | ||
|
|
||
| export type ToggleButtonGroupProviderProps< | ||
| T extends ToggleButtonGroupProps["value"], | ||
| > = { | ||
| children: | ||
| | ReactNode | ||
| | ((props: ToggleButtonGroupContextProps<T>) => ReactNode); | ||
| initialValue?: T | null; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/views/ExploreView/mode/components/ToggleButtonGroup/constants.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { ToggleButtonOwnProps, ToggleButtonGroupProps } from "@mui/material"; | ||
| import { MODE } from "../../types"; | ||
|
|
||
| export const TOGGLE_BUTTON_GROUP_PROPS: ToggleButtonGroupProps = { | ||
| exclusive: true, | ||
| fullWidth: true, | ||
| }; | ||
|
|
||
| export const TOGGLE_BUTTONS: (Omit<ToggleButtonOwnProps, "value"> & { | ||
| value: MODE; | ||
| })[] = [ | ||
| // `disabled` and `selected` are temporary until we have the mode context fully implemented and can set the value based on that context. | ||
| { children: "Research", disabled: true, value: MODE.RESEARCH }, | ||
| { children: "Search", selected: true, value: MODE.SEARCH }, | ||
| ]; | ||
19 changes: 19 additions & 0 deletions
19
...views/ExploreView/mode/components/ToggleButtonGroup/stories/toggleButtonGroup.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { Meta, StoryObj } from "@storybook/nextjs-vite"; | ||
| import { ToggleButtonGroup } from "../toggleButtonGroup"; | ||
| import { MODE } from "../../../types"; | ||
| import { fn } from "storybook/test"; | ||
|
|
||
| const meta: Meta<typeof ToggleButtonGroup> = { | ||
| component: ToggleButtonGroup, | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const DEFAULT: Story = { | ||
| args: { | ||
| onChange: fn(), | ||
| value: MODE.SEARCH, | ||
| }, | ||
| }; |
14 changes: 14 additions & 0 deletions
14
src/views/ExploreView/mode/components/ToggleButtonGroup/toggleButtonGroup.styles.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import styled from "@emotion/styled"; | ||
| import { ToggleButtonGroup } from "@mui/material"; | ||
| import { PALETTE } from "../../../../../styles/common/constants/palette"; | ||
|
|
||
| export const StyledToggleButtonGroup = styled(ToggleButtonGroup)` | ||
| .MuiToggleButton-root { | ||
| padding: 6px 16px; | ||
| text-transform: none; | ||
|
|
||
| &.Mui-disabled { | ||
| color: ${PALETTE.INK_LIGHT}; | ||
| } | ||
| } | ||
| `; |
22 changes: 22 additions & 0 deletions
22
src/views/ExploreView/mode/components/ToggleButtonGroup/toggleButtonGroup.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { ToggleButton, ToggleButtonGroupProps } from "@mui/material"; | ||
| import { JSX } from "react"; | ||
| import { TOGGLE_BUTTON_GROUP_PROPS, TOGGLE_BUTTONS } from "./constants"; | ||
| import { StyledToggleButtonGroup } from "./toggleButtonGroup.styles"; | ||
|
|
||
| export const ToggleButtonGroup = ({ | ||
| className, | ||
| ...props /* ToggleButtonGroupProps */ | ||
| }: ToggleButtonGroupProps): JSX.Element | null => { | ||
| if (!props.onChange) return null; | ||
| return ( | ||
| <StyledToggleButtonGroup | ||
| className={className} | ||
| {...TOGGLE_BUTTON_GROUP_PROPS} | ||
| {...props} | ||
| > | ||
| {TOGGLE_BUTTONS.map((tButtonProps) => ( | ||
| <ToggleButton key={tButtonProps.value} {...tButtonProps} /> | ||
| ))} | ||
| </StyledToggleButtonGroup> | ||
| ); | ||
| }; | ||
frano-m marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export const FEATURE_FLAG = { | ||
| CHAT: "chat", | ||
| } as const; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { createContext } from "react"; | ||
| import { ModeContextProps } from "./types"; | ||
|
|
||
| export const ModeContext = createContext<ModeContextProps>({}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { useContext } from "react"; | ||
| import { ModeContext } from "./context"; | ||
| import { ModeContextProps } from "./types"; | ||
|
|
||
| /** | ||
| * Returns the mode context. | ||
| * @returns Mode context with value and onChange, or empty object if feature disabled. | ||
| */ | ||
| export const useMode = (): ModeContextProps => { | ||
| return useContext(ModeContext); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { JSX } from "react"; | ||
| import { ToggleButtonGroupProvider } from "../../../../components/common/ToggleButtonGroup/provider/provider"; | ||
| import { useConfig } from "../../../../hooks/useConfig"; | ||
| import { useFeatureFlag } from "../../../../hooks/useFeatureFlag/useFeatureFlag"; | ||
| import { FEATURE_FLAG } from "../constants"; | ||
| import { MODE } from "../types"; | ||
| import { ModeContext } from "./context"; | ||
| import { ModeProviderProps } from "./types"; | ||
|
|
||
| /** | ||
| * Mode provider component "search" or "research" i.e. self-directed search vs. chat-based search. | ||
| * Either mode is available based on the "chat" feature flag, or by configuration. | ||
| * Wraps children with mode context based on whether the feature is enabled or not. | ||
| * @param props - Component props. | ||
| * @param props.children - Children. | ||
| * @returns Mode provider component. | ||
| */ | ||
| export function ModeProvider({ children }: ModeProviderProps): JSX.Element { | ||
| const flagEnabled = useFeatureFlag(FEATURE_FLAG.CHAT); | ||
| const { config } = useConfig(); | ||
| const { ai } = config; | ||
| const enabled = flagEnabled || ai?.enabled; | ||
| return ( | ||
| <ToggleButtonGroupProvider<MODE> initialValue={MODE.SEARCH}> | ||
| {(props) => ( | ||
| <ModeContext.Provider value={enabled ? props : {}}> | ||
| {typeof children === "function" | ||
| ? children(enabled ? props : {}) | ||
| : children} | ||
| </ModeContext.Provider> | ||
| )} | ||
| </ToggleButtonGroupProvider> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { ReactNode } from "react"; | ||
| import { ToggleButtonGroupContextProps } from "../../../../components/common/ToggleButtonGroup/provider/types"; | ||
| import { MODE } from "../types"; | ||
|
|
||
| export type ModeContextProps = Partial<ToggleButtonGroupContextProps<MODE>>; | ||
|
|
||
| export type ModeProviderProps = { | ||
| children: ReactNode | ((props: ModeContextProps) => ReactNode); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export enum MODE { | ||
| RESEARCH = "RESEARCH", | ||
| SEARCH = "SEARCH", | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import styled from "@emotion/styled"; | ||
|
|
||
| export const StyledGrid = styled.div` | ||
| display: grid; | ||
| gap: 8px 16px; | ||
| grid-template-columns: 1fr auto; | ||
| `; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.