Add label sorting on the web

This commit is contained in:
Jackson Harper
2023-05-05 12:36:30 +08:00
parent aab332eefc
commit f4a860e912
3 changed files with 46 additions and 8 deletions

View File

@ -18,6 +18,7 @@ import {
timeAgo,
TitleStyle,
} from './LibraryCardStyles'
import { sortedLabels } from '../../../lib/labelsSort'
dayjs.extend(relativeTime)
@ -178,11 +179,11 @@ export function LibraryGridCard(props: LinkedItemCardProps): JSX.Element {
minHeight: '35px',
}}
>
{props.item.labels
?.sort((a, b) => a.name.localeCompare(b.name))
.map(({ name, color }, index) => (
{sortedLabels(props.item.labels).map(
({ name, color }, index) => (
<LabelChip key={index} text={name || ''} color={color} />
))}
)
)}
</HStack>
<VStack
css={{

View File

@ -14,6 +14,7 @@ import {
timeAgo,
TitleStyle,
} from './LibraryCardStyles'
import { sortedLabels } from '../../../lib/labelsSort'
export function LibraryListCard(props: LinkedItemCardProps): JSX.Element {
const [isHovered, setIsHovered] = useState(false)
@ -118,11 +119,11 @@ export function LibraryListCard(props: LinkedItemCardProps): JSX.Element {
display: 'block',
}}
>
{props.item.labels
?.sort((a, b) => a.name.localeCompare(b.name))
.map(({ name, color }, index) => (
{sortedLabels(props.item.labels).map(
({ name, color }, index) => (
<LabelChip key={index} text={name || ''} color={color} />
))}
)
)}
</HStack>
</HStack>
</VStack>

View File

@ -0,0 +1,36 @@
import { Label } from './networking/fragments/labelFragment'
export const sortedLabels = (labels: Label[] | undefined): Label[] => {
if (!labels) {
return []
}
const colors = new Map<string, Label[]>()
for (const label of labels) {
let list = colors.get(label.color) ?? []
list.push(label)
colors.set(
label.color,
list.sort((a, b) => a.name.localeCompare(b.name))
)
}
const sortedColors = Array.from(colors.keys()).sort((a, b) => {
// Sort by the first element's name
const aname = colors.get(a)?.find(() => true)?.name ?? a
const bname = colors.get(b)?.find(() => true)?.name ?? b
return aname.localeCompare(bname)
})
const result: Label[] = []
for (const key of sortedColors) {
const items = colors.get(key)
if (!items) {
continue
}
const sorted = items.sort((a, b) => a.name.localeCompare(b.name))
result.push(...sorted)
}
return result
}