Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/components/FriendProfileCardsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
onMount,
Show,
} from "solid-js";
import type { FriendProfile } from "../types";
import type { FriendProfile, SelfProfile } from "../types";
import { formatTime } from "../utils";
import "./ButtonStyles.css";
import "./FriendProfileCardsScreen.css";
Expand All @@ -34,6 +34,7 @@ const reportReasons = [

export function FriendProfileCardsScreen(props: FriendProfileCardsScreenProps) {
const [searchParams] = useSearchParams();
const [selfProfile, setSelfProfile] = createSignal<SelfProfile | null>(null);
const [profiles, setProfiles] = createSignal<FriendProfile[]>([]);
const [loading, setLoading] = createSignal(true);
const [error, setError] = createSignal<string | null>(null);
Expand Down Expand Up @@ -75,9 +76,11 @@ export function FriendProfileCardsScreen(props: FriendProfileCardsScreenProps) {
setLoading(true);
setError(null);
try {
const response = await props.api<{ profiles: FriendProfile[] }>(
"/api/friends/profiles",
);
const response = await props.api<{
self: SelfProfile | null;
profiles: FriendProfile[];
}>("/api/friends/profiles");
setSelfProfile(response.self);
setProfiles(response.profiles);
setNicknameDrafts(
Object.fromEntries(
Expand Down Expand Up @@ -269,6 +272,36 @@ export function FriendProfileCardsScreen(props: FriendProfileCardsScreenProps) {
</Show>

<div class="friend-cards-scroll" ref={scrollContainerRef}>
<Show when={selfProfile()}>
{(self) => (
<section class="friend-profile-card">
<div class="friend-profile-card-inner">
<div class="friend-profile-identity">
<div class="friend-profile-top-row">
<div>
<h2>{self().username}</h2>
</div>
</div>
</div>

<ul class="friend-profile-metrics">
<li class="friend-metric">
<span>Friend Count</span>
<strong>{self().friendCount}</strong>
</li>
<li class="friend-metric">
<span>Lifetime Oys Sent</span>
<strong>{self().lifetimeOysSent}</strong>
</li>
<li class="friend-metric">
<span>Lifetime Oys Received</span>
<strong>{self().lifetimeOysReceived}</strong>
</li>
</ul>
</div>
</section>
)}
</Show>
<For each={profiles()}>
{(profile) => (
<section
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export type BlockedUser = {
blocked_at: number;
};

export type SelfProfile = {
username: string;
friendCount: number;
lifetimeOysSent: number;
lifetimeOysReceived: number;
};

export type FriendProfile = {
id: number;
username: string;
Expand Down
51 changes: 46 additions & 5 deletions worker/routes/friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ export function registerFriendRoutes(app: App) {
}
const { startOfTodayNY, startOfYesterdayNY } = getStreakDateBoundaries();

const profilesResult = await c.get("db").query<FriendProfileRow>(
`
const [profilesResult, selfResult] = await Promise.all([
c.get("db").query<FriendProfileRow>(
`
SELECT
u.id,
u.username,
Expand Down Expand Up @@ -219,11 +220,51 @@ export function registerFriendRoutes(app: App) {
AND b1.blocker_user_id IS NULL
AND b2.blocker_user_id IS NULL
ORDER BY loi.last_oy_created_at DESC NULLS LAST, u.username
`,
[user.id],
);
`,
[user.id],
),
c.get("db").query<{
username: string;
friend_count: number;
lifetime_oys_sent: number;
lifetime_oys_received: number;
}>(
`
SELECT
u.username,
(
SELECT COUNT(*)
FROM friendships f_count
WHERE f_count.user_id = u.id
)::INTEGER AS friend_count,
(
SELECT COUNT(*)
FROM oys sent
WHERE sent.from_user_id = u.id
)::INTEGER AS lifetime_oys_sent,
(
SELECT COUNT(*)
FROM oys received
WHERE received.to_user_id = u.id
)::INTEGER AS lifetime_oys_received
FROM users u
WHERE u.id = $1
`,
[user.id],
),
]);

const selfRow = selfResult.rows[0];

return c.json({
self: selfRow
? {
username: selfRow.username,
friendCount: Number(selfRow.friend_count),
lifetimeOysSent: Number(selfRow.lifetime_oys_sent),
lifetimeOysReceived: Number(selfRow.lifetime_oys_received),
}
: null,
profiles: profilesResult.rows.map((row) => ({
id: row.id,
username: row.username,
Expand Down