Skip to content

Commit

Permalink
fix profile not found error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kualta committed Jun 30, 2024
1 parent 0f839b4 commit 8f3cc35
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/app/(sidebars)/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function error({ error, reset }: { error: Error & { digest?: stri
return (
<div className="max-w-screen-xl px-4 py-16">
<div className="max-w-screen-sm text-center">
<h2 className="text-2xl font-bold">Σ(O_O) Something went wrong</h2>
<h2 className="text-2xl font-bold">Something went wrong</h2>

{message && <p className="text-base mx-auto text-muted-foreground p-4 w-[50%]">{message}</p>}
<div className="flex flex-row gap-4 items-center justify-center mt-10">
Expand Down
4 changes: 1 addition & 3 deletions src/app/(sidebars)/u/[user]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Card } from "~/components/ui/card";
import { UserNavigation } from "~/components/user/UserNavigation";
import { UserProfile } from "~/components/user/UserProfile";
import { getLensClient } from "~/utils/getLensClient";
import { getUserByHandle } from "~/utils/getUserByHandle";

export default async function layout({
Expand All @@ -11,12 +10,11 @@ export default async function layout({
children: React.ReactNode;
params: { user: string };
}) {
const { handle: authenticatedHandle } = await getLensClient();
const handle = params.user;
const isUserProfile = handle === authenticatedHandle;
const user = await getUserByHandle(handle);

if (!user) throw new Error("∑(O_O;) Profile not found");
console.log(user);
return (
<>
<UserProfile user={user} />
Expand Down
4 changes: 4 additions & 0 deletions src/app/(sidebars)/u/[user]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const getInitialData = async (handle: string) => {
const { client } = await getLensClient();
const user = await getUserByHandle(handle);

if (!user) {
throw new Error("☆⌒(>。<) User not found");
}

const lensPosts = await client.publication
.fetchAll({
where: { from: [user.id], publicationTypes: [PublicationType.Post] },
Expand Down
1 change: 0 additions & 1 deletion src/components/user/UserInterests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ProfileInterestTypes } from "@lens-protocol/react-web";
import { useMemo } from "react";
import { toast } from "sonner";
import { Label } from "../ui/label";
import { Separator } from "../ui/separator";
import { Toggle } from "../ui/toggle";
import { type UserInterests, capitalize, parseInterests } from "./User";

Expand Down
6 changes: 2 additions & 4 deletions src/components/user/UserProfile.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { CalendarIcon, EditIcon, MessageCircleIcon, PawPrintIcon, User2Icon } from "lucide-react";
import Link from "next/link";
import Markdown from "~/components/Markdown";
import { TimeSince } from "~/components/TimeLabel";
import { UserAvatar } from "~/components/user/UserAvatar";
import { getLensClient } from "~/utils/getLensClient";
import { FollowButton } from "../FollowButton";
import { TruncatedText } from "../TruncatedText";
import { Badge } from "../ui/badge";
import { Dialog, DialogContent, DialogHeader, DialogTrigger } from "../ui/dialog";
import { Label } from "../ui/label";
import { Separator } from "../ui/separator";
import { type User, parseInterests } from "./User";
import { UserInterestsList } from "./UserInterests";

export const UserProfile = async ({ user }: { user: User }) => {
if (!user) throw new Error("∑(O_O;) Profile not found");
if (!user) throw new Error("Profile not found");

const { user: authedUser } = await getLensClient();
const isUserProfile = user.id === authedUser.id;
const isFollowingMe = user.actions.following;

const commonInterestTypes = user.interests
.map((interest) => interest.value)
.filter((interest) => authedUser.interests.map((interest) => interest.value).includes(interest));
.filter((interest) => authedUser?.interests?.map((interest) => interest.value).includes(interest));
const commonInterests = parseInterests(commonInterestTypes);

return (
Expand Down
6 changes: 4 additions & 2 deletions src/utils/getUserByHandle.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { lensProfileToUser } from "~/components/user/User";
import { type User, lensProfileToUser } from "~/components/user/User";
import { getLensClient } from "./getLensClient";

export const getUserByHandle = async (handle: string) => {
export const getUserByHandle = async (handle: string): Promise<User | null> => {
const { client } = await getLensClient();

const profile = await client.profile.fetch({
forHandle: `lens/${handle}`,
});

if (!profile) return null

const user = lensProfileToUser(profile);
if (!user) throw new Error("∑(O_O;) Profile not found");

Expand Down

0 comments on commit 8f3cc35

Please sign in to comment.