-
Notifications
You must be signed in to change notification settings - Fork 27
feat: mc options sorting and count #4444
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
Open
cemreinanc
wants to merge
4
commits into
main
Choose a base branch
from
feat/4318-mc-options-sorting-and-count
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b5d0291
mc choice count and sorting
cemreinanc 0158a58
fix unusual behavior and reuse logic
cemreinanc f439c99
modify timeline items selection logic and dropdown behavior
cemreinanc b58da58
Merge remote-tracking branch 'origin/main' into feat/4318-mc-options-…
cemreinanc 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
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 |
|---|---|---|
|
|
@@ -2,21 +2,21 @@ import { faChevronDown } from "@fortawesome/free-solid-svg-icons"; | |
| import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
| import { Popover, PopoverButton, PopoverPanel } from "@headlessui/react"; | ||
| import { useTranslations } from "next-intl"; | ||
| import { FC, useMemo } from "react"; | ||
| import { FC, useCallback, useEffect, useMemo, useState } from "react"; | ||
|
|
||
| import ChoiceCheckbox from "@/components/choice_checkbox"; | ||
| import Button from "@/components/ui/button"; | ||
| import Checkbox from "@/components/ui/checkbox"; | ||
| import useAppTheme from "@/hooks/use_app_theme"; | ||
| import { ChoiceItem } from "@/types/choices"; | ||
| import cn from "@/utils/core/cn"; | ||
| import { truncateLabel } from "@/utils/formatters/string"; | ||
|
|
||
| type Props = { | ||
| choices: ChoiceItem[]; | ||
| onChoiceChange: (choice: string, checked: boolean) => void; | ||
| onChoiceHighlight: (choice: string, highlighted: boolean) => void; | ||
| onToggleAll: (checked: boolean) => void; | ||
| maxLegendChoices: number; | ||
| othersToggle?: boolean; | ||
| onOthersToggle?: (checked: boolean) => void; | ||
| othersDisabled?: boolean; | ||
|
|
@@ -27,23 +27,41 @@ const ChoicesLegend: FC<Props> = ({ | |
| onChoiceChange, | ||
| onChoiceHighlight, | ||
| onToggleAll, | ||
| maxLegendChoices, | ||
| othersToggle, | ||
| onOthersToggle, | ||
| othersDisabled, | ||
| }) => { | ||
| const t = useTranslations(); | ||
| const { getThemeColor } = useAppTheme(); | ||
| const mcMode = typeof othersToggle === "boolean" && !!onOthersToggle; | ||
| const { legendChoices, dropdownChoices } = useMemo(() => { | ||
| const left = choices.slice(0, maxLegendChoices); | ||
| const right = choices.slice(maxLegendChoices); | ||
| if (mcMode) { | ||
| const leftOff = left.filter((c) => !c.active); | ||
| return { legendChoices: left, dropdownChoices: [...leftOff, ...right] }; | ||
| } | ||
| return { legendChoices: left, dropdownChoices: right }; | ||
| }, [choices, maxLegendChoices, mcMode]); | ||
|
|
||
| const [isDropdownOpen, setIsDropdownOpen] = useState(false); | ||
|
|
||
| const [pinnedChoices, setPinnedChoices] = useState<Set<string>>( | ||
| () => new Set(choices.filter((c) => c.active).map((c) => c.choice)) | ||
| ); | ||
| useEffect(() => { | ||
| if (isDropdownOpen) return; | ||
| setPinnedChoices((prev) => { | ||
| const next = new Set(prev); | ||
| let changed = false; | ||
| choices.forEach((c) => { | ||
| if (c.active && !next.has(c.choice)) { | ||
| next.add(c.choice); | ||
| changed = true; | ||
| } | ||
| }); | ||
| return changed ? next : prev; | ||
| }); | ||
| }, [choices, isDropdownOpen]); | ||
|
|
||
| const { legendChoices, dropdownChoices } = useMemo( | ||
| () => ({ | ||
| legendChoices: choices.filter((c) => pinnedChoices.has(c.choice)), | ||
| dropdownChoices: choices.filter((c) => !pinnedChoices.has(c.choice)), | ||
| }), | ||
| [choices, pinnedChoices] | ||
| ); | ||
|
|
||
| const areAllSelected = useMemo(() => { | ||
| const selectedCount = choices.reduce( | ||
|
|
@@ -58,20 +76,24 @@ const ChoicesLegend: FC<Props> = ({ | |
| [t, dropdownChoices.length] | ||
| ); | ||
|
|
||
| const handleDropdownOpenChange = useCallback((open: boolean) => { | ||
| setIsDropdownOpen(open); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <div className="relative flex flex-wrap items-center justify-center gap-[14px] text-xs font-normal"> | ||
| <div className="relative flex flex-wrap items-center gap-x-3.5 gap-y-2 text-xs font-normal"> | ||
| {legendChoices.map(({ label, choice, color, active }, idx) => ( | ||
| <ChoiceCheckbox | ||
| key={`multiple-choice-legend-${choice}-${idx}`} | ||
| label={label || choice} | ||
| label={truncateLabel(label || choice, 30)} | ||
| color={color.DEFAULT} | ||
|
Comment on lines
85
to
89
Contributor
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. Truncate the dropdown labels too. Line 88 fixes the pinned legend chips, but hidden choices still render Suggested follow-up- label={label || choice}
+ label={truncateLabel(label || choice, 30)}🤖 Prompt for AI Agents |
||
| checked={active} | ||
| onChange={(checked) => onChoiceChange(choice, checked)} | ||
| onHighlight={(highlighted) => onChoiceHighlight(choice, highlighted)} | ||
| /> | ||
| ))} | ||
| {!!dropdownChoices.length && ( | ||
| <div className="flex items-center gap-1 md:ml-auto"> | ||
| <div className="flex items-center gap-1"> | ||
| {mcMode && ( | ||
| <Checkbox | ||
| checked={!!othersToggle} | ||
|
|
@@ -89,6 +111,10 @@ const ChoicesLegend: FC<Props> = ({ | |
| <Popover className="relative"> | ||
| {({ open }) => ( | ||
| <> | ||
| <PopoverOpenSync | ||
| open={open} | ||
| onChange={handleDropdownOpenChange} | ||
| /> | ||
| {!mcMode ? ( | ||
| <PopoverButton | ||
| as={Button} | ||
|
|
@@ -146,4 +172,14 @@ const ChoicesLegend: FC<Props> = ({ | |
| ); | ||
| }; | ||
|
|
||
| const PopoverOpenSync: FC<{ | ||
| open: boolean; | ||
| onChange: (open: boolean) => void; | ||
| }> = ({ open, onChange }) => { | ||
| useEffect(() => { | ||
| onChange(open); | ||
| }, [open, onChange]); | ||
| return null; | ||
| }; | ||
|
|
||
| export default ChoicesLegend; | ||
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
Oops, something went wrong.
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.
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.
The new pinnedChoices state only adds choices but never removes them. If a user toggles many different choices on/off over time, the legend accumulates all ever-active choices. Is this intentional?
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.
Yes intentional.
Any item clicked on from the others list promotes to the main legend after the dropdown closes so that users can see the names and colors of the visible items. If users uncheck the legend item, it shouldn't be instantly hidden because maybe they will want to activate it again quickly after, so hiding it instantly will be a bad UX for users. -- Solution could be adding timeout and hiding after that but I found that unnecessary as it will be returned to original state after page navigation.