From 12b4e4d7656f162793813426a4de1b97d1dbefd6 Mon Sep 17 00:00:00 2001 From: Zach Hammer Date: Tue, 18 Apr 2023 16:21:43 -0400 Subject: [PATCH] Match implementation of other search result items for stack overflow Allow for empty result Signed-off-by: Zach Hammer --- plugins/stack-overflow/src/plugin.ts | 6 ++++- .../StackOverflowSearchResultListItem.tsx | 27 ++++++++++--------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/plugins/stack-overflow/src/plugin.ts b/plugins/stack-overflow/src/plugin.ts index a837846708..5be4c89cbe 100644 --- a/plugins/stack-overflow/src/plugin.ts +++ b/plugins/stack-overflow/src/plugin.ts @@ -23,6 +23,8 @@ import { import { createCardExtension } from '@backstage/plugin-home'; import { StackOverflowQuestionsContentProps } from './types'; import { stackOverflowApiRef, StackOverflowClient } from './api'; +import { SearchResultListItemExtensionProps } from '@backstage/plugin-search-react'; +import { StackOverflowSearchResultListItemProps } from './search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem'; /** * The Backstage plugin that holds stack overflow specific components @@ -45,7 +47,9 @@ export const stackOverflowPlugin = createPlugin({ * * @public */ -export const StackOverflowSearchResultListItem = stackOverflowPlugin.provide( +export const StackOverflowSearchResultListItem: ( + props: SearchResultListItemExtensionProps, +) => JSX.Element | null = stackOverflowPlugin.provide( createComponentExtension({ name: 'StackOverflowResultListItem', component: { diff --git a/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx b/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx index 9971b0e7e5..de015145e1 100644 --- a/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx +++ b/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx @@ -29,8 +29,8 @@ import { useAnalytics } from '@backstage/core-plugin-api'; import { ResultHighlight } from '@backstage/plugin-search-common'; import { HighlightedSearchResultText } from '@backstage/plugin-search-react'; -type StackOverflowSearchResultListItemProps = { - result: any; // TODO(emmaindal): type to StackOverflowDocument. +export type StackOverflowSearchResultListItemProps = { + result?: any; // TODO(emmaindal): type to StackOverflowDocument. icon?: React.ReactNode; rank?: number; highlight?: ResultHighlight; @@ -39,17 +39,20 @@ type StackOverflowSearchResultListItemProps = { export const StackOverflowSearchResultListItem = ( props: StackOverflowSearchResultListItemProps, ) => { - const { location, title, text, answers, tags } = props.result; - const { highlight } = props; + const { result, highlight } = props; const analytics = useAnalytics(); const handleClick = () => { - analytics.captureEvent('discover', title, { - attributes: { to: location }, + analytics.captureEvent('discover', result.title, { + attributes: { to: result.location }, value: props.rank, }); }; + if (!result) { + return null; + } + return ( <> @@ -58,7 +61,7 @@ export const StackOverflowSearchResultListItem = ( + {highlight?.fields?.title ? ( ) : ( - _unescape(title) + _unescape(result.title) )} } @@ -81,13 +84,13 @@ export const StackOverflowSearchResultListItem = ( /> ) : ( - `Author: ${text}` + `Author: ${result.text}` ) } /> - - {tags && - tags.map((tag: string) => ( + + {result.tags && + result.tags.map((tag: string) => ( ))}