Skip to content

Commit

Permalink
Basic list of keyword links on homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
bryophyta committed Sep 24, 2024
1 parent f5d59c7 commit 4383b48
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
1 change: 0 additions & 1 deletion newswires/client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
EuiEmptyPrompt,
EuiHeader,
EuiHeaderSectionItem,
EuiPageTemplate,
Expand Down
77 changes: 66 additions & 11 deletions newswires/client/src/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { EuiEmptyPrompt } from '@elastic/eui';
import {
EuiBadge,
EuiButton,
EuiEmptyPrompt,
EuiFlexGroup,
EuiFlexItem,

Check warning on line 6 in newswires/client/src/Home.tsx

View workflow job for this annotation

GitHub Actions / Build and upload to riffraff

'EuiFlexItem' is defined but never used. Allowed unused vars must match /^_/u
EuiListGroup,
} from '@elastic/eui';
import { Fragment, useEffect, useMemo, useState } from 'react';

Check warning on line 9 in newswires/client/src/Home.tsx

View workflow job for this annotation

GitHub Actions / Build and upload to riffraff

'Fragment' is defined but never used. Allowed unused vars must match /^_/u
import { SearchBox } from './SearchBox';
import type { KeywordCounts } from './sharedTypes';
import { KeywordCountsSchema } from './sharedTypes';
import { useHistory } from './urlState';

export function Home({
Expand All @@ -8,15 +18,60 @@ export function Home({
updateQuery: (newQuery: string) => void;
}) {
const { currentState } = useHistory();
return (
<EuiEmptyPrompt
title={<h2>Search wires</h2>}
body={
<SearchBox
initialQuery={currentState.params?.q ?? ''}
update={updateQuery}
/>
}
/>
const [keywords, setKeywords] = useState<KeywordCounts>([]);

useEffect(() => {
fetch('api/keywords?limit=5')
.then((response) => {
if (response.ok) {
return response.json();
}
})
.then((data) => {
const maybeKeywords = KeywordCountsSchema.safeParse(data);
if (maybeKeywords.success) {
setKeywords(maybeKeywords.data);
} else {
console.error('Error parsing keywords:', maybeKeywords.error);
}
})
.catch((error) => {
console.error('Error fetching keywords:', error);
});
});

const body = (
<EuiFlexGroup direction="column" justifyContent="center">
<SearchBox
initialQuery={currentState.params?.q ?? ''}
update={updateQuery}
/>
{keywords.length > 0 && <KeywordsList keywords={keywords} />}
</EuiFlexGroup>
);

return <EuiEmptyPrompt title={<h2>Search wires</h2>} body={body} />;
}

const KeywordsList = ({ keywords }: { keywords: KeywordCounts }) => {
const sortedKeywords = useMemo(() => {
return keywords.sort((a, b) => b.count - a.count);
}, [keywords]);

return (
<EuiFlexGroup>
<EuiListGroup flush={true}>
{sortedKeywords.map(({ keyword, count }) => (
// TODO: fix query when keyword support added to search:
// - specify as keyword rather than 'q'
// - make sure keyword text is properly encoded
// - handle quote marks if still present in response
<EuiButton key={keyword} color="text" href={`/feed?q=${keyword}`}>
{keyword.replaceAll('"', '')}{' '}
<EuiBadge color={'subdued'}>{count}</EuiBadge>
</EuiButton>
))}
</EuiListGroup>
</EuiFlexGroup>
);
};
9 changes: 9 additions & 0 deletions newswires/client/src/sharedTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ export const WireDataSchema = z.object({
});

export type WireData = z.infer<typeof WireDataSchema>;

const KeywordCountSchema = z.object({
keyword: z.string(),
count: z.number(),
});

export const KeywordCountsSchema = z.array(KeywordCountSchema);

export type KeywordCounts = z.infer<typeof KeywordCountsSchema>;

0 comments on commit 4383b48

Please sign in to comment.