Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit c8bc0e2

Browse files
feat: updated search list item nested links
1 parent ff7d9ba commit c8bc0e2

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Flex } from '@chakra-ui/react';
2-
import { useState } from 'react';
3-
import {
4-
AssistantRecipeWithStats,
5-
RecipeSummary,
6-
} from '../../types/assistantTypes';
2+
import useUrlQuery from '../../hooks/useUrlQuery';
3+
import { AssistantRecipeWithStats } from '../../types/assistantTypes';
74
import Code from '../Code/Code';
85
import SnippetResultsList from './SnippetResultsList';
96
import SnippetResultsListItem from './SnippetResultsListItem';
@@ -13,11 +10,12 @@ type SnippetResultsProps = {
1310
};
1411

1512
export default function SnippetResults({ results }: SnippetResultsProps) {
16-
const [snippetInFocus, setSnippetInFocus] = useState(results[0] || {});
13+
const query = useUrlQuery();
14+
const currentSnippetId = query.get('currentSnippetId');
1715

18-
const changeSnippetInFocus = (recipe: RecipeSummary) => {
19-
setSnippetInFocus(recipe);
20-
};
16+
const currentSnippet = currentSnippetId
17+
? results.find((recipe) => String(recipe.id) === currentSnippetId)
18+
: results[0] || {};
2119

2220
return (
2321
<Flex h="full" overflow="hidden">
@@ -26,13 +24,12 @@ export default function SnippetResults({ results }: SnippetResultsProps) {
2624
<SnippetResultsListItem
2725
key={result.id}
2826
recipe={result}
29-
changeSnippetInFocus={changeSnippetInFocus}
30-
currentSnippet={result.id === snippetInFocus.id}
27+
isCurrentSnippet={currentSnippet?.id === result.id}
3128
/>
3229
))}
3330
</SnippetResultsList>
3431

35-
{results[0] ? <Code recipe={snippetInFocus} /> : null}
32+
{currentSnippet?.id ? <Code recipe={currentSnippet} /> : null}
3633
</Flex>
3734
);
3835
}

src/renderer/components/SnippetResults/SnippetResultsListItem.tsx

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
import { Flex, Text } from '@chakra-ui/react';
1+
import { Flex, LinkBox, LinkOverlay, Text } from '@chakra-ui/react';
22
import { ChartBarsIcon, DotIcon, Logo, Tags } from '@codiga/components';
3-
import {
4-
AssistantRecipeWithStats,
5-
RecipeSummary,
6-
} from '../../types/assistantTypes';
3+
import { Link as RouterLink } from 'react-router-dom';
4+
5+
import { AssistantRecipeWithStats } from '../../types/assistantTypes';
76
import FavoriteSnippet from '../Favorite/FavoriteSnippet';
87
import Votes from '../Votes';
98

109
type SnippetResultsListItemProps = {
1110
recipe: AssistantRecipeWithStats;
12-
changeSnippetInFocus: (recipe: RecipeSummary) => void;
13-
currentSnippet: boolean;
11+
isCurrentSnippet: boolean;
1412
};
1513

1614
export default function SnippetResultsListItem({
1715
recipe,
18-
changeSnippetInFocus,
19-
currentSnippet,
16+
isCurrentSnippet,
2017
}: SnippetResultsListItemProps) {
2118
return (
22-
<Flex
19+
<LinkBox
20+
cursor="pointer"
21+
as={Flex}
2322
flexDirection="column"
2423
p="space_16"
2524
gridGap="space_8"
2625
borderBottom="1px"
2726
borderColor="neutral.50"
28-
bg={currentSnippet ? 'neutral.25' : 'neutral.0'}
27+
bg={isCurrentSnippet ? 'neutral.25' : 'neutral.0'}
2928
_dark={{
30-
bg: currentSnippet ? 'base.onyx' : 'neutral.100',
29+
bg: isCurrentSnippet ? 'base.onyx' : 'neutral.100',
3130
borderColor: 'base.onyx',
3231
}}
3332
_focus={{
@@ -42,14 +41,13 @@ export default function SnippetResultsListItem({
4241
bg: 'neutral.25',
4342
_dark: { bg: 'base.onyx' },
4443
}}
45-
onClick={() => changeSnippetInFocus(recipe)}
46-
cursor="pointer"
47-
tabIndex={0}
4844
>
4945
<Flex alignItems="center" gridGap="space_8">
5046
<Logo value={recipe.language} fullSize={false} logoSize={18} />
5147
<Text size="sm" fontWeight="bold" noOfLines={1}>
52-
{recipe.name}
48+
<LinkOverlay as={RouterLink} to={`?currentSnippetId=${recipe.id}`}>
49+
{recipe.name}
50+
</LinkOverlay>
5351
</Text>
5452
<FavoriteSnippet
5553
isSubscribed={recipe.isSubscribed}
@@ -75,6 +73,6 @@ export default function SnippetResultsListItem({
7573
{recipe?.tags && recipe?.tags.length > 0 && (
7674
<Tags values={recipe?.tags || []} max={1} tagProps={{ size: 'sm' }} />
7775
)}
78-
</Flex>
76+
</LinkBox>
7977
);
8078
}

src/renderer/hooks/useUrlQuery.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { useMemo } from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
4+
export default function useUrlQuery() {
5+
const { search } = useLocation();
6+
7+
return useMemo(() => new URLSearchParams(search), [search]);
8+
}

0 commit comments

Comments
 (0)