Skip to content

Commit

Permalink
chore: changing it up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasgriffintn committed Aug 25, 2024
1 parent 136ca05 commit e913ad4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
8 changes: 2 additions & 6 deletions apps/web/app/components/homepage/search-result-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,9 @@ function getBadgeColor(score: number): string {
}
}

function getRandomImagePosition(): string {
const positions = ['left', 'top', 'right'];
return positions[Math.floor(Math.random() * positions.length)];
}

export const SearchResultItem = ({
result,
imagePosition = 'left',
}: {
result: {
metadata: {
Expand All @@ -113,10 +109,10 @@ export const SearchResultItem = ({
};
score: number;
};
imagePosition?: string;
}) => {
const matchPercentage = Math.round(result.score * 100);
const badgeColor = getBadgeColor(matchPercentage);
const imagePosition = getRandomImagePosition();

return (
<li>
Expand Down
42 changes: 38 additions & 4 deletions apps/web/app/components/homepage/search-results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,50 @@ import { ArrowRight } from 'lucide-react';
import { SearchResultItem } from './search-result-item';
import { Button } from '../ui/button';

const getImagePosition = (
index: number,
result: {
metadata: {
title: string;
description: string;
url: string;
author?: string;
published?: string;
updated?: string;
};
score: number;
}
): string => {
const positions = ['left', 'top', 'right'];

// Use the length of the title and description to influence the position
const titleLength = result?.metadata?.title?.length || 0;
const descriptionLength = result?.metadata?.description?.length || 0;
const score = result?.score || 0;

// Generate a pseudo-random number based on the title and description lengths
const randomFactor =
(titleLength + descriptionLength + Math.round(score * 100) + index) %
positions.length;

return positions[randomFactor];
};

export const SearchResults = ({
data,
}: {
data: {
count: number;
matches: {
title: string;
description: string;
url: string;
metadata: {
title: string;
description: string;
url: string;
author?: string;
published?: string;
updated?: string;
};
score: number;
}[];
};
}) => (
Expand All @@ -26,7 +56,11 @@ export const SearchResults = ({
<ul className="space-y-4">
{data?.matches?.length > 0 ? (
data.matches.map((result, index) => (
<SearchResultItem key={index} result={result} />
<SearchResultItem
key={index}
result={result}
imagePosition={getImagePosition(index, result)}
/>
))
) : (
<p className="text-muted-foreground">Loading...</p>
Expand Down

0 comments on commit e913ad4

Please sign in to comment.