-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat: BROS-594: Add Distribution row to Task Summary #8821
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
hlomzik
wants to merge
9
commits into
develop
Choose a base branch
from
fb-bros-594/summary-distribution
base: develop
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
9 commits
Select commit
Hold shift + click to select a range
6dca90e
feat: BROS-594: Add Distribution row to Task Summary
hlomzik 8b0cc69
Merge branch 'develop' into 'fb-bros-594/summary-distribution'
hlomzik 86700f9
Remove link to non-existent ChatRegion to fix `yarn docs`
hlomzik 26f9e8e
Merge branch 'fb-bros-594/summary-distribution' of github.com:HumanSi…
hlomzik 311bb52
Remember View All selection going from task to task
hlomzik 9960e99
Fix the issue in lsf-sdk
hlomzik 13a10ae
Improve styles of the aggregation row
hlomzik 74abc47
Fix shadows
hlomzik bc7d2ff
Show percentage again instead of counts
hlomzik 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
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
166 changes: 166 additions & 0 deletions
166
web/libs/editor/src/components/TaskSummary/Aggregation.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,166 @@ | ||
| import { cnm } from "@humansignal/ui"; | ||
| import type { RawResult } from "../../stores/types"; | ||
| import { Chip } from "./Chip"; | ||
| import type { AnnotationSummary, ControlTag } from "./types"; | ||
| import { getLabelCounts } from "./utils"; | ||
|
|
||
| const resultValue = (result: RawResult) => { | ||
| if (result.type === "textarea") { | ||
| return result.value.text; | ||
| } | ||
| return result.value[result.type]; | ||
| }; | ||
|
|
||
| export const AggregationRow = ({ | ||
| control, | ||
| annotations, | ||
| countEmpty, | ||
| isExpanded, | ||
| }: { control: ControlTag; annotations: AnnotationSummary[]; countEmpty: boolean; isExpanded: boolean }) => { | ||
| const allResults = annotations.flatMap((ann) => ann.results.filter((r) => r.from_name === control.name)); | ||
|
|
||
| if (!allResults.length) { | ||
| return <span className="text-neutral-content-subtler text-xs italic">No data</span>; | ||
| } | ||
|
|
||
| const totalAnnotations = countEmpty ? annotations.length : allResults.length; | ||
|
|
||
| // Handle labels-type controls | ||
| if (control.type.endsWith("labels")) { | ||
| const allLabels = allResults.flatMap((r) => resultValue(r)).flat(); | ||
| const labelCounts = getLabelCounts(allLabels, control.label_attrs); | ||
|
|
||
| // Sort by count descending | ||
| const sortedLabels = Object.entries(labelCounts) | ||
| .filter(([_, data]) => data.count > 0) | ||
| .sort(([, a], [, b]) => b.count - a.count); | ||
|
|
||
| return ( | ||
| <div className={cnm("text-ellipsis", !isExpanded && "line-clamp-2")}> | ||
| {sortedLabels.map(([label, data]) => { | ||
| return ( | ||
| <Chip | ||
| key={label} | ||
| prefix={data.count} | ||
| colors={{ | ||
| background: data.background, | ||
| border: data.border, | ||
| color: data.color || data.border, | ||
| }} | ||
| className="mr-tighter mb-tighter" | ||
| thickBorder | ||
| > | ||
| {label} | ||
| </Chip> | ||
| ); | ||
| })} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // Handle pairwise; they are similar to choices but produce only `left` or `right` values | ||
| if (control.type === "pairwise") { | ||
| const allPairwise = allResults.flatMap((r) => resultValue(r)).flat(); | ||
| const pairwiseCounts: Record<string, number> = {}; | ||
|
|
||
| allPairwise.forEach((pairwise) => { | ||
| pairwiseCounts[pairwise] = (pairwiseCounts[pairwise] || 0) + 1; | ||
| }); | ||
| const sortedPairwise = Object.entries(pairwiseCounts).sort(([, a], [, b]) => b - a); | ||
|
|
||
| return ( | ||
| <div className={cnm("text-ellipsis", !isExpanded && "line-clamp-2")}> | ||
| {sortedPairwise.map(([pairwise, count]) => { | ||
| return ( | ||
| <Chip key={pairwise} prefix={count} className="mr-tighter mb-tighter"> | ||
| {pairwise} | ||
| </Chip> | ||
| ); | ||
| })} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // Handle choices | ||
| if (control.type === "choices") { | ||
| const allChoices = allResults.flatMap((r) => resultValue(r)).flat(); | ||
| const choiceCounts: Record<string, number> = {}; | ||
|
|
||
| allChoices.forEach((choice) => { | ||
| choiceCounts[choice] = (choiceCounts[choice] || 0) + 1; | ||
| }); | ||
|
|
||
| const sortedChoices = Object.entries(choiceCounts).sort(([, a], [, b]) => b - a); | ||
|
|
||
| return ( | ||
| <div className={cnm("text-ellipsis", !isExpanded && "line-clamp-2")}> | ||
| {sortedChoices.map(([choice, count]) => { | ||
| return ( | ||
| <Chip | ||
| key={choice} | ||
| prefix={`${((count / totalAnnotations) * 100).toFixed(1)}%`} | ||
| colors={{ background: control.label_attrs[choice]?.background }} | ||
| className="mr-tighter mb-tighter" | ||
| > | ||
| {choice} | ||
| </Chip> | ||
| ); | ||
| })} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // Handle taxonomy | ||
| if (control.type === "taxonomy") { | ||
| const values = allResults.flatMap((r) => resultValue(r)?.map((r: string[]) => r.at(-1))); | ||
| const pathCounts: Record<string, number> = {}; | ||
|
|
||
| values.filter(Boolean).forEach((path: string | string[]) => { | ||
| const pathStr = Array.isArray(path) ? path.join(" / ") : path; | ||
| pathCounts[pathStr] = (pathCounts[pathStr] || 0) + 1; | ||
| }); | ||
|
|
||
| const sortedPaths = Object.entries(pathCounts).sort(([, a], [, b]) => b - a); | ||
|
|
||
| return ( | ||
| <div className={cnm("text-ellipsis", !isExpanded && "line-clamp-2")}> | ||
| {sortedPaths.map(([path, count]) => { | ||
| return ( | ||
| <Chip key={path} prefix={`${((count / totalAnnotations) * 100).toFixed(1)}%`} className="mr-tighter mb-tighter"> | ||
| {path} | ||
| </Chip> | ||
| ); | ||
| })} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // Handle rating | ||
| if (control.type === "rating") { | ||
| const ratings = allResults.map((r) => resultValue(r)).filter(Boolean); | ||
| if (!ratings.length) return <span className="text-neutral-content-subtler text-xs italic">No ratings</span>; | ||
|
|
||
| const avgRating = ratings.reduce((sum, val) => sum + val, 0) / (countEmpty ? totalAnnotations : ratings.length); | ||
| return ( | ||
| <span className="text-sm font-medium text-neutral-content-subtle"> | ||
| Avg: <span className="font-bold">{avgRating.toFixed(1)}</span> <span className="text-yellow-500">★</span> | ||
| </span> | ||
| ); | ||
| } | ||
|
|
||
| // Handle number | ||
| if (control.type === "number") { | ||
| const numbers = allResults.map((r) => resultValue(r)).filter((v) => v !== null && v !== undefined); | ||
| if (!numbers.length) return <span className="text-neutral-content-subtler text-xs italic">No data</span>; | ||
|
|
||
| const avg = numbers.reduce((sum, val) => sum + Number(val), 0) / (countEmpty ? totalAnnotations : numbers.length); | ||
| return ( | ||
| <span className="text-sm font-medium text-neutral-content-subtle"> | ||
| Avg: <span className="font-bold">{avg.toFixed(1)}</span> | ||
| </span> | ||
| ); | ||
| } | ||
|
|
||
| // Default: show N/A | ||
| return <span className="text-sm font-medium text-neutral-content-subtler">N/A</span>; | ||
| }; |
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,72 @@ | ||
| import type { PropsWithChildren, CSSProperties } from "react"; | ||
| import { cnm } from "@humansignal/ui"; | ||
|
|
||
| interface ChipProps extends PropsWithChildren { | ||
| /** | ||
| * Optional prefix content (e.g., count, percentage) that appears before the main content with a divider | ||
| */ | ||
| prefix?: React.ReactNode; | ||
|
|
||
| /** | ||
| * Optional color configuration from label_attrs | ||
| */ | ||
| colors?: { | ||
| background?: string; | ||
| border?: string; | ||
| color?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Additional inline styles to apply | ||
| */ | ||
| style?: CSSProperties; | ||
|
|
||
| /** | ||
| * Whether to show a thick left border (typically for labels) | ||
| */ | ||
| thickBorder?: boolean; | ||
|
|
||
| /** | ||
| * Additional CSS classes | ||
| */ | ||
| className?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Unified chip component for displaying labels, badges, and tags throughout the Task Summary. | ||
| * Supports various styling options including colors, borders, and prefixes for counts/percentages. | ||
| */ | ||
| export const Chip = ({ children, prefix, colors, style, thickBorder = false, className }: ChipProps) => { | ||
| const combinedStyles: CSSProperties = { | ||
| ...style, | ||
| ...(colors?.background && { background: colors.background }), | ||
| ...(colors?.border && { borderColor: colors.border }), | ||
| ...(colors?.color && { color: colors.color }), | ||
| ...(thickBorder && colors?.border && { borderLeft: `3px solid ${colors.border}` }), | ||
| }; | ||
| const isPercentage = typeof prefix === "string" && prefix.endsWith("%"); | ||
|
|
||
| if (!children) return null; | ||
|
|
||
| return ( | ||
| <span | ||
| className={cnm( | ||
| "inline-flex items-center whitespace-nowrap rounded-4 px-2 py-0.5", | ||
| "text-xs border", | ||
| !colors?.background && "bg-neutral-surface-subtle", | ||
| !colors?.border && "border-neutral-border", | ||
| !colors?.color && "text-neutral-content", | ||
| className, | ||
| )} | ||
| style={combinedStyles} | ||
| > | ||
| {prefix && ( | ||
| <> | ||
| <span className="font-semibold">{prefix}</span> | ||
| {isPercentage ? <span className="opacity-50 mx-tighter">|</span> : <span className="opacity-50 mx-tightest">×</span>} | ||
| </> | ||
| )} | ||
| {children} | ||
| </span> | ||
| ); | ||
| }; |
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.
Do we not have a similar Badge component in the UI lib which could be used here?