|
| 1 | +import { framer, type MenuItem } from "framer-plugin" |
| 2 | +import { startTransition, useCallback, useEffect, useMemo, useState } from "react" |
| 3 | +import { assertNever } from "../utils/assert" |
| 4 | +import { type ReadonlyGroupedResults } from "../utils/filter/group-results" |
| 5 | +import type { Range } from "../utils/filter/ranges" |
| 6 | +import { type CollectionItemResult, type NodeResult, type Result } from "../utils/filter/types" |
| 7 | +import { useFilter } from "../utils/filter/useFilter" |
| 8 | +import type { RootNodeType } from "../utils/indexer/types" |
| 9 | +import { useIndexer } from "../utils/indexer/useIndexer" |
| 10 | +import { entries } from "../utils/object" |
| 11 | +import { SearchInput } from "./SearchInput" |
| 12 | +import { IconEllipsis } from "./ui/IconEllipsis" |
| 13 | +import { Menu } from "./ui/Menu" |
| 14 | + |
| 15 | +export function SearchScene() { |
| 16 | + const { index } = useIndexer() |
| 17 | + const [query, setQuery] = useState("") |
| 18 | + const { searchOptions, optionsMenuItems } = useOptionsMenuItems() |
| 19 | + const { results } = useFilter(query, searchOptions, index) |
| 20 | + |
| 21 | + const handleQueryChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => { |
| 22 | + startTransition(() => { |
| 23 | + setQuery(event.target.value) |
| 24 | + }) |
| 25 | + }, []) |
| 26 | + |
| 27 | + const hasResults = useMemo(() => { |
| 28 | + for (const [, resultForRootNodeType] of entries(results)) { |
| 29 | + if (!resultForRootNodeType) continue |
| 30 | + |
| 31 | + for (const resultsForRootId of Object.values(resultForRootNodeType)) { |
| 32 | + if (resultsForRootId.length > 0) return true |
| 33 | + } |
| 34 | + } |
| 35 | + return false |
| 36 | + }, [results]) |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (query && hasResults) { |
| 40 | + framer.showUI({ |
| 41 | + height: 320, |
| 42 | + }) |
| 43 | + } else if (query && !hasResults) { |
| 44 | + framer.showUI({ |
| 45 | + height: 140, |
| 46 | + }) |
| 47 | + } else { |
| 48 | + framer.showUI({ |
| 49 | + height: 64, |
| 50 | + }) |
| 51 | + } |
| 52 | + }, [query, hasResults]) |
| 53 | + |
| 54 | + return ( |
| 55 | + <main className="flex flex-col h-full"> |
| 56 | + <div className="flex gap-2 border-b border-framer-divider border-t py-3 mx-3"> |
| 57 | + <SearchInput value={query} onChange={handleQueryChange} /> |
| 58 | + <Menu items={optionsMenuItems}> |
| 59 | + <IconEllipsis /> |
| 60 | + </Menu> |
| 61 | + </div> |
| 62 | + <div className="flex-1 overflow-y-auto px-4 flex flex-col"> |
| 63 | + {query && hasResults ? <SearchResultsByRootType results={results} /> : <NoResults />} |
| 64 | + </div> |
| 65 | + </main> |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +// All components below this line are temporary and will be removed when the search results are implemented |
| 70 | +// Having them ensures it's easier to verify the indexer and filterer are working as expected |
| 71 | + |
| 72 | +function NoResults() { |
| 73 | + return ( |
| 74 | + <div className="flex-1 flex justify-center items-center"> |
| 75 | + <div className="text-center text-amber-500">No results found.</div> |
| 76 | + </div> |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +function SearchResultsByRootType({ results }: { results: ReadonlyGroupedResults }) { |
| 81 | + return Object.entries(results).map(([rootNodeType, resultsByRootId]) => ( |
| 82 | + <RootNodeTypeSection key={rootNodeType} resultsByRootId={resultsByRootId} /> |
| 83 | + )) |
| 84 | +} |
| 85 | + |
| 86 | +function RootNodeTypeSection({ resultsByRootId }: { resultsByRootId: { readonly [id: string]: readonly Result[] } }) { |
| 87 | + return ( |
| 88 | + <div className="flex flex-col gap-2 mb-4 text-amber-500"> |
| 89 | + {Object.entries(resultsByRootId).map(([rootNodeId, results]) => ( |
| 90 | + <SearchResultGroup key={rootNodeId} results={results} /> |
| 91 | + ))} |
| 92 | + </div> |
| 93 | + ) |
| 94 | +} |
| 95 | + |
| 96 | +function SearchResultGroup({ results }: { results: readonly Result[] }) { |
| 97 | + const [first] = results |
| 98 | + |
| 99 | + if (!first) return null |
| 100 | + |
| 101 | + return ( |
| 102 | + <div> |
| 103 | + <div className="text-lg text-amber-800"> |
| 104 | + {first.entry.rootNodeName} ({first.entry.rootNodeType} {first.entry.rootNode.id}) |
| 105 | + </div> |
| 106 | + <ul className="flex flex-col gap-2"> |
| 107 | + {results.map(result => ( |
| 108 | + <SearchResult key={result.id} result={result} /> |
| 109 | + ))} |
| 110 | + </ul> |
| 111 | + </div> |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +function SearchResult({ result }: { result: Result }) { |
| 116 | + if (result.type === "CollectionItem") { |
| 117 | + return <CollectionItemSearchResult result={result} /> |
| 118 | + } else if (result.type === "Node") { |
| 119 | + return <NodeSearchResult result={result} /> |
| 120 | + } |
| 121 | + |
| 122 | + assertNever(result) |
| 123 | +} |
| 124 | + |
| 125 | +function NodeSearchResult({ result }: { result: NodeResult }) { |
| 126 | + if (!result.entry.text) return null |
| 127 | + |
| 128 | + return <SearchResultRanges text={result.entry.text} ranges={result.ranges} resultId={result.id} /> |
| 129 | +} |
| 130 | + |
| 131 | +function CollectionItemSearchResult({ result }: { result: CollectionItemResult }) { |
| 132 | + if (!result.text) return null |
| 133 | + |
| 134 | + return <SearchResultRanges text={result.text} ranges={result.ranges} resultId={result.id} /> |
| 135 | +} |
| 136 | + |
| 137 | +function SearchResultRanges({ text, ranges, resultId }: { text: string; ranges: readonly Range[]; resultId: string }) { |
| 138 | + return ranges.map(range => ( |
| 139 | + <li key={`${resultId}-${range.join("-")}`} className="text-ellipsis overflow-hidden whitespace-nowrap"> |
| 140 | + <HighlightedTextWithContext text={text} range={range} /> ({resultId}) |
| 141 | + </li> |
| 142 | + )) |
| 143 | +} |
| 144 | + |
| 145 | +function HighlightedTextWithContext({ text, range }: { text: string; range: Range }) { |
| 146 | + const [start, end] = range |
| 147 | + const before = text.slice(0, start) |
| 148 | + const match = text.slice(start, end) |
| 149 | + const after = text.slice(end) |
| 150 | + |
| 151 | + return ( |
| 152 | + <> |
| 153 | + {before} |
| 154 | + <span className="font-bold">{match}</span> |
| 155 | + {after} |
| 156 | + </> |
| 157 | + ) |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Contains if you can filter by a root node type. |
| 162 | + * |
| 163 | + * During current state of the plugin, not all types are indexed yet. |
| 164 | + */ |
| 165 | +const optionsEnabled = { |
| 166 | + ComponentNode: true, |
| 167 | + WebPageNode: true, |
| 168 | + Collection: true, |
| 169 | +} as const satisfies Record<RootNodeType, boolean> |
| 170 | + |
| 171 | +const defaultSearchOptions = entries(optionsEnabled) |
| 172 | + .filter(([, enabled]) => enabled) |
| 173 | + .map(([rootNode]) => rootNode) |
| 174 | + |
| 175 | +const optionsMenuLabels = { |
| 176 | + ComponentNode: "Components", |
| 177 | + WebPageNode: "Pages", |
| 178 | + Collection: "Collections", |
| 179 | +} as const satisfies Record<RootNodeType, string> |
| 180 | + |
| 181 | +function useOptionsMenuItems() { |
| 182 | + const [searchOptions, setSearchOptions] = useState<readonly RootNodeType[]>(defaultSearchOptions) |
| 183 | + |
| 184 | + const optionsMenuItems = useMemo((): MenuItem[] => { |
| 185 | + return entries(optionsEnabled).map(([rootNode, enabled]) => ({ |
| 186 | + id: rootNode, |
| 187 | + label: optionsMenuLabels[rootNode], |
| 188 | + enabled, |
| 189 | + checked: searchOptions.includes(rootNode), |
| 190 | + onAction: () => { |
| 191 | + setSearchOptions(prev => { |
| 192 | + if (prev.includes(rootNode)) { |
| 193 | + return prev.filter(option => option !== rootNode) |
| 194 | + } |
| 195 | + |
| 196 | + return [...prev, rootNode] |
| 197 | + }) |
| 198 | + }, |
| 199 | + })) |
| 200 | + }, [searchOptions]) |
| 201 | + |
| 202 | + return { searchOptions, optionsMenuItems } |
| 203 | +} |
0 commit comments