2022-08-13 10:56:07 +02:00
|
|
|
import { Category } from "./types"
|
|
|
|
|
|
|
|
|
|
export function visitCategoryTree(category: Category, visitor: (category: Category) => void): void {
|
|
|
|
|
visitor(category)
|
|
|
|
|
category.children.forEach(child => visitCategoryTree(child, visitor))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function flattenCategoryTree(category: Category): Category[] {
|
|
|
|
|
const categories: Category[] = []
|
|
|
|
|
visitCategoryTree(category, c => categories.push(c))
|
|
|
|
|
return categories
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function categoryUnreadCount(category?: Category): number {
|
|
|
|
|
if (!category) return 0
|
|
|
|
|
|
|
|
|
|
return flattenCategoryTree(category)
|
|
|
|
|
.flatMap(c => c.feeds)
|
|
|
|
|
.map(f => f.unread)
|
|
|
|
|
.reduce((total, current) => total + current, 0)
|
|
|
|
|
}
|
2022-08-20 23:02:55 +02:00
|
|
|
|
|
|
|
|
export const calculatePlaceholderSize = ({ width, height, maxWidth }: { width?: number; height?: number; maxWidth: number }) => {
|
|
|
|
|
const placeholderWidth = width && Math.min(width, maxWidth)
|
|
|
|
|
const placeholderHeight = height && width && width > maxWidth ? height * (maxWidth / width) : height
|
|
|
|
|
return { width: placeholderWidth, height: placeholderHeight }
|
|
|
|
|
}
|
2022-10-13 11:27:04 +02:00
|
|
|
|
|
|
|
|
export const scrollToWithCallback = ({
|
|
|
|
|
element,
|
|
|
|
|
options,
|
|
|
|
|
onScrollEnded,
|
|
|
|
|
}: {
|
|
|
|
|
element: HTMLElement
|
|
|
|
|
options: ScrollToOptions
|
|
|
|
|
onScrollEnded: () => void
|
|
|
|
|
}) => {
|
|
|
|
|
const offset = (options.top ?? 0).toFixed()
|
|
|
|
|
|
|
|
|
|
const onScroll = () => {
|
|
|
|
|
if (element.offsetTop.toFixed() === offset) {
|
|
|
|
|
element.removeEventListener("scroll", onScroll)
|
|
|
|
|
onScrollEnded()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
element.addEventListener("scroll", onScroll)
|
|
|
|
|
|
|
|
|
|
// scrollTo does not trigger if there's nothing to do, trigger it manually
|
|
|
|
|
onScroll()
|
|
|
|
|
|
|
|
|
|
element.scrollTo(options)
|
|
|
|
|
}
|