Skip to content

Commit

Permalink
feat: join similar groups together based on product.title
Browse files Browse the repository at this point in the history
  • Loading branch information
cdxker authored and skeptrunedev committed Nov 12, 2024
1 parent 74800eb commit 9651e40
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { GroupChunk } from "../../utils/types";

type Props = {
requestID: string;
group: GroupChunk;
// Group of Groups (with subvariants)
group: GroupChunk[];
index: number;
}

export const ProductGroupItem = ({ index, group, requestID }: Props) => {

const [groupItemIndex] = useState(0);
const selectedItem = useMemo(() => group.chunks[groupItemIndex], [groupItemIndex]);
const selectedItem = useMemo(() => group[groupItemIndex].chunks[0], [groupItemIndex]);

return (
<ProductItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const SearchMode = () => {
const { switchToChatAndAskQuestion } = useChatState();

const getItemComponent = (
result: ChunkWithHighlights | GroupChunk,
result: ChunkWithHighlights | GroupChunk[],
index: number
) => {
const chunkOrGroup = isChunksWithHighlights(result);
Expand Down Expand Up @@ -70,8 +70,8 @@ export const SearchMode = () => {
} else {
return (
<div key={index} className="item-group-container">
<p className="item-group-name">{result.group.name}</p>
{result.chunks.map((chunk, index) => (
<p className="item-group-name">{result[0].group.name}</p>
{result[0].chunks.map((chunk, index) => (
<DocsItem
item={chunk}
index={index}
Expand Down
20 changes: 16 additions & 4 deletions clients/search-component/src/utils/hooks/modal-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ const ModalContext = createContext<{
trieveSDK: TrieveSDK;
query: string;
setQuery: React.Dispatch<React.SetStateAction<string>>;
results: ChunkWithHighlights[] | GroupChunk[];
results: ChunkWithHighlights[] | GroupChunk[][];
setResults: React.Dispatch<
React.SetStateAction<ChunkWithHighlights[] | GroupChunk[]>
React.SetStateAction<ChunkWithHighlights[] | GroupChunk[][]>
>;
requestID: string;
setRequestID: React.Dispatch<React.SetStateAction<string>>;
Expand Down Expand Up @@ -154,7 +154,7 @@ const ModalProvider = ({
...onLoadProps,
});
const [query, setQuery] = useState("");
const [results, setResults] = useState<ChunkWithHighlights[] | GroupChunk[]>(
const [results, setResults] = useState<ChunkWithHighlights[] | GroupChunk[][]>(
[]
);
const [requestID, setRequestID] = useState("");
Expand Down Expand Up @@ -203,7 +203,19 @@ const ModalProvider = ({
type: props.type,
});

setResults(results.groups);
// join similar groups
// add to hashmap
const groupMap = new Map<string, GroupChunk[]>();
results.groups.forEach((group) => {
const title = group.chunks[0].chunk.metadata?.title;
if (groupMap.has(title)) {
groupMap.get(title)?.push(group);
} else {
groupMap.set(title, [group]);
}
});

setResults(Array.from(groupMap.values()));
setRequestID(results.requestID);
} else {
const results = await searchWithTrieve({
Expand Down
2 changes: 1 addition & 1 deletion clients/search-component/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type GroupSearchResults = {
};

export function isChunksWithHighlights(
result: ChunkWithHighlights | GroupChunk
result: ChunkWithHighlights | GroupChunk[]
): result is ChunkWithHighlights {
return (result as ChunkWithHighlights).highlights !== undefined;
}
Expand Down

0 comments on commit 9651e40

Please sign in to comment.