-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
200 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,97 @@ | ||
'use client'; | ||
|
||
import { Article, Hit } from '@/lib/client'; | ||
import { Article, Highlight, Hit } from '@/lib/client'; | ||
import { useHits } from '@clinia/search-sdk-react'; | ||
|
||
export const Hits = () => { | ||
const hits = useHits() as Hit<Article>[]; | ||
|
||
if (hits.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
<div className="grid w-[674px] grid-cols-1 divide-y rounded-lg border"> | ||
{hits.map((hit) => ( | ||
<div key={hit.resource.id}> | ||
<h2>{hit.resource.title}</h2> | ||
</div> | ||
<ArticleHit hit={hit} key={hit.resource.id} /> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
const ArticleHit = ({ hit }: { hit: Hit<Article> }) => { | ||
const passageHighlight = hit.highlight.find((h) => h.type === 'passage'); | ||
|
||
return ( | ||
<article className="p-4"> | ||
<h1 className="mb-2 text-lg font-medium text-foreground"> | ||
{hit.resource.title} | ||
</h1> | ||
{passageHighlight && ( | ||
<PassageHighlight | ||
highlight={passageHighlight} | ||
text={hit.resource.text} | ||
/> | ||
)} | ||
</article> | ||
); | ||
}; | ||
|
||
const PassageHighlight = ({ | ||
highlight, | ||
text, | ||
}: { | ||
highlight: Highlight; | ||
text: string; | ||
}) => { | ||
if (highlight.type !== 'passage') { | ||
return null; | ||
} | ||
|
||
const sentenceHighlight = highlight.highlight; | ||
if (sentenceHighlight?.type === 'sentence') { | ||
return ( | ||
<SentenceHighlight | ||
highlight={sentenceHighlight} | ||
passage={highlight} | ||
text={text} | ||
/> | ||
); | ||
} | ||
|
||
return <p className="text-sm text-muted-foreground">{highlight.match}</p>; | ||
}; | ||
|
||
const SentenceHighlight = ({ | ||
highlight, | ||
text, | ||
passage, | ||
}: { | ||
highlight: Highlight; | ||
passage: Highlight; | ||
text: string; | ||
}) => { | ||
// We get the start offset of the sentence with respect to the passage | ||
const startOffset = highlight.startOffset - passage.startOffset; | ||
|
||
console.log({ | ||
passageStartOffset: passage.startOffset, | ||
sentenceStartOffset: highlight.startOffset, | ||
startOffset, | ||
}); | ||
|
||
const start = passage.match.slice(passage.startOffset, startOffset); | ||
|
||
// We get the end offset of the sentence with respect to the passage | ||
const endOffset = highlight.endOffset - passage.startOffset; | ||
|
||
const end = passage.match.slice(endOffset); | ||
|
||
return ( | ||
<p className="text-sm text-muted-foreground"> | ||
{start} | ||
<mark className="bg-primary/20">{highlight.match}</mark> | ||
{end} | ||
</p> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SearchResponse } from '@/lib/client'; | ||
import { useObserver } from '@clinia/search-sdk-react'; | ||
|
||
export const useMeta = () => { | ||
const meta = useObserver({ | ||
key: 'meta', | ||
onSearchStateChange: (state) => { | ||
if (!state.result) | ||
return { | ||
queryId: '', | ||
queryIntent: '', | ||
query: '', | ||
questions: [], | ||
}; | ||
|
||
return state.result.meta as unknown as SearchResponse['meta']; | ||
}, | ||
}); | ||
|
||
return meta; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,45 @@ | ||
export type InformationPocClient = { | ||
search: <T = Resource>(params: SearchRequest) => Promise<SearchResponse<T>>; | ||
search: <T = Resource>(params: SearchRequest) => Promise<SearchResponse<T>>; | ||
}; | ||
|
||
|
||
export type SearchRequest = { | ||
query: string; | ||
} | ||
query: string; | ||
}; | ||
|
||
export type SearchResponse<T = Resource> = { | ||
hits: Hit<T>[]; | ||
meta: { | ||
queryId: string | ||
queryIntent: 'QUESTION' | ||
questions: string[] | ||
} | ||
} | ||
hits: Hit<T>[]; | ||
meta: { | ||
queryId: string; | ||
queryIntent: 'QUESTION'; | ||
questions: string[]; | ||
}; | ||
}; | ||
|
||
export type Hit<T = Resource> = { | ||
resource: T; | ||
highlight: Highlight[]; | ||
enrichers: Enrichers; | ||
} | ||
|
||
resource: T; | ||
highlight: Highlight[]; | ||
enrichers: Enrichers; | ||
}; | ||
|
||
export type Resource = { | ||
id: string; | ||
[key: string]: any; | ||
} | ||
id: string; | ||
[key: string]: any; | ||
}; | ||
|
||
export type Article = Resource & { | ||
title: string; | ||
text: string; | ||
} | ||
title: string; | ||
text: string; | ||
}; | ||
|
||
export type Highlight = { | ||
match: string; | ||
startOffset: number; | ||
endOffset: number; | ||
type: 'passage' | 'sentence'; | ||
highlight?: Highlight; | ||
score: number; | ||
} | ||
match: string; | ||
startOffset: number; | ||
endOffset: number; | ||
type: 'passage' | 'sentence'; | ||
highlight?: Highlight; | ||
score: number; | ||
}; | ||
|
||
export type Enrichers = { | ||
questions: string[]; | ||
} | ||
questions: string[]; | ||
}; |
Oops, something went wrong.