Skip to content

Commit

Permalink
perf(studio): add virtualized list for facet items
Browse files Browse the repository at this point in the history
  • Loading branch information
cesconix committed Sep 4, 2024
1 parent 3a65aa0 commit c42b0cc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
5 changes: 1 addition & 4 deletions packages/pinorama-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
"bin": {
"pinorama": "./cli.mjs"
},
"files": [
"dist",
"cli.mjs"
],
"files": ["dist", "cli.mjs"],
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { FacetItem } from "./facet-item"

import type { AnySchema } from "@orama/orama"
import { useVirtualizer } from "@tanstack/react-virtual"
import type { IntrospectionFacet, PinoramaIntrospection } from "pinorama-types"
import { useRef } from "react"
import type { FacetValue, SearchFilters } from "../types"

type FacetBodyProps = {
Expand All @@ -14,22 +16,44 @@ type FacetBodyProps = {
}

export function FacetBody(props: FacetBodyProps) {
const parentRef = useRef<HTMLDivElement>(null)

const rowVirtualizer = useVirtualizer({
count: props.values.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 38
})

return (
<div className="border box-border rounded-md overflow-auto max-h-[241px] my-2">
{props.values?.map(({ value, count }) => {
return (
<FacetItem
key={value}
introspection={props.introspection}
name={props.name}
type={props.type}
value={value}
count={count}
filters={props.filters}
onFiltersChange={props.onFiltersChange}
/>
)
})}
<div
ref={parentRef}
className="border box-border rounded-md overflow-auto max-h-[241px] my-2"
>
<div
className="w-full relative"
style={{ height: `${rowVirtualizer.getTotalSize()}px` }}
>
{rowVirtualizer.getVirtualItems().map((virtualItem) => (
<div
key={virtualItem.key}
className="absolute top-0 left-0 w-full"
style={{
height: `${virtualItem.size}px`,
transform: `translateY(${virtualItem.start}px)`
}}
>
<FacetItem
introspection={props.introspection}
name={props.name}
type={props.type}
value={props.values[virtualItem.index].value}
count={props.values[virtualItem.index].count}
filters={props.filters}
onFiltersChange={props.onFiltersChange}
/>
</div>
))}
</div>
</div>
)
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c42b0cc

Please sign in to comment.