Skip to content

Commit

Permalink
Merge pull request #171 from denoland/remove-comments-summary-in-item…
Browse files Browse the repository at this point in the history
…-summary

refactor: remove comments count
  • Loading branch information
iuioiua authored May 10, 2023
2 parents 710abb9 + 405eee3 commit 7f43889
Showing 4 changed files with 18 additions and 24 deletions.
18 changes: 10 additions & 8 deletions components/ItemSummary.tsx
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@ export function timeAgo(time: number | Date) {
export interface ItemSummaryProps {
item: Item;
user: User;
commentsCount: number;
isVoted: boolean;
}

@@ -29,19 +28,22 @@ export default function ItemSummary(props: ItemSummaryProps) {
isVoted={props.isVoted}
/>
<div>
<span class="mr-2 text-black">
<a href={props.item.url}>{props.item.title}</a>
<span class="mr-2">
<a class="text-black hover:underline" href={`/item/${props.item.id}`}>
{props.item.title}
</a>
</span>
<span>
<a class="hover:underline" href={props.item.url} target="_blank">
{new URL(props.item.url).host}
</a>
</span>
{new URL(props.item.url).host}
<p>
{getUserDisplayName(props.user)}{" "}
{props.user.isSubscribed && (
<span title="Deno Hunt premium user">🦕{" "}</span>
)}
{timeAgo(new Date(props.item.createdAt))} ago •{" "}
<a href={`/item/${props.item.id}`}>
{pluralize(props.commentsCount, "comment")}
</a>
{timeAgo(new Date(props.item.createdAt))} ago
</p>
</div>
</div>
8 changes: 1 addition & 7 deletions routes/index.tsx
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ import type { State } from "./_middleware.ts";
import ItemSummary from "@/components/ItemSummary.tsx";
import {
getAllItems,
getItemCommentsCount,
getUsersByIds,
getVotedItemIdsByUser,
type Item,
@@ -17,7 +16,6 @@ import {
interface HomePageData extends State {
users: User[];
items: Item[];
commentsCounts: number[];
areVoted: boolean[];
}

@@ -38,15 +36,12 @@ export const handler: Handlers<HomePageData, State> = {
/** @todo Add pagination functionality */
const items = (await getAllItems({ limit: 10 })).sort(compareScore);
const users = await getUsersByIds(items.map((item) => item.userId));
const commentsCounts = await Promise.all(
items.map((item) => getItemCommentsCount(item.id)),
);
const votedItemIds = ctx.state.session
? await getVotedItemIdsByUser(ctx.state.session?.user.id)
: [];
/** @todo Optimise */
const areVoted = items.map((item) => votedItemIds.includes(item.id));
return ctx.render({ ...ctx.state, items, commentsCounts, users, areVoted });
return ctx.render({ ...ctx.state, items, users, areVoted });
},
};

@@ -59,7 +54,6 @@ export default function HomePage(props: PageProps<HomePageData>) {
{props.data.items.map((item, index) => (
<ItemSummary
item={item}
commentsCount={props.data.commentsCounts[index]}
isVoted={props.data.areVoted[index]}
user={props.data.users[index]}
/>
9 changes: 7 additions & 2 deletions routes/item/[id].tsx
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ import {
type User,
} from "@/utils/db.ts";
import { redirect } from "@/utils/http.ts";
import { pluralize } from "@/components/ItemSummary.tsx";

interface ItemPageData extends State {
user: User;
@@ -89,14 +90,18 @@ export default function ItemPage(props: PageProps<ItemPageData>) {
<>
<Head title={props.data.item.title} href={props.url.href} />
<Layout session={props.data.session}>
<div class={`${SITE_WIDTH_STYLES} flex-1 px-8 space-y-4`}>
<div class={`${SITE_WIDTH_STYLES} flex-1 px-8 space-y-8`}>
<ItemSummary
item={props.data.item}
commentsCount={props.data.comments.length}
isVoted={props.data.isVoted}
user={props.data.user}
/>
<div>
<h2>
<strong>
{pluralize(props.data.comments.length, "comment")}
</strong>
</h2>
{props.data.comments.map((comment, index) => (
<div class="py-4">
<p>
7 changes: 0 additions & 7 deletions utils/db.ts
Original file line number Diff line number Diff line change
@@ -98,13 +98,6 @@ export async function getCommentsByItem(
return comments;
}

export async function getItemCommentsCount(itemId: string) {
const iter = kv.list<Comment>({ prefix: ["comments_by_item", itemId] });
let count = 0;
for await (const _ of iter) count++;
return count;
}

interface InitVote {
userId: string;
itemId: string;

0 comments on commit 7f43889

Please sign in to comment.