Skip to content

Commit

Permalink
fix search page, fix momoka commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
kualta committed Jul 8, 2024
1 parent 1990609 commit 6f88242
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 29 additions & 19 deletions src/app/api/posts/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,28 @@ export async function POST(req: NextRequest) {
const contentURI = await uploadMetadata(data, handle);
const postResult = await createPost(client, contentURI, replyingTo);

return handlePostResult(postResult, handle, contentURI);
if (postResult.isFailure()) {
throw new Error(postResult.error.message);
}

if (postResult.value.__typename === "LensProfileManagerRelayError") {
throw new Error(postResult.value.reason);
}

if (postResult.value.__typename === "RelaySuccess") {
const { txId: id, txHash: hash } = postResult.value;
const date = new Date().toISOString();
console.log(`${handle} created a post: ${id}, hash: ${hash}, ipfs: ${contentURI}, date: ${date}`);
return NextResponse.json({ id, hash }, { status: 200, statusText: "Success" });
}

if (postResult.value.__typename === "CreateMomokaPublicationResult") {
const date = new Date().toISOString();
console.log(`${handle} created a momoka post: ${postResult.value.id}, date: ${date}`);
return NextResponse.json({ id: postResult.value.id }, { status: 200, statusText: "Success" });
}

throw new Error("Unknown error. This should never happen.");
} catch (error) {
console.error("Failed to create a post: ", error);
return NextResponse.json({ error: `Failed to create a post: ${error.message}` }, { status: 500 });
Expand Down Expand Up @@ -196,6 +217,12 @@ async function uploadMetadata(data: any, handle: string) {

async function createPost(client: LensClient, contentURI: string, replyingTo?: string) {
if (replyingTo) {
const [, , da] = replyingTo.split("-");
const isOnMomoka = da === "DA";

if (isOnMomoka) {
return await client.publication.commentOnMomoka({ contentURI, commentOn: replyingTo });
}
return await client.publication.commentOnchain({ contentURI, commentOn: replyingTo });
}
return await client.publication.postOnchain({ contentURI });
Expand All @@ -208,21 +235,4 @@ function handlePostResult(
>,
handle: string,
contentURI: string,
) {
if (postResult.isFailure()) {
throw new Error(postResult.error.message);
}

if (postResult.value.__typename === "LensProfileManagerRelayError") {
throw new Error(postResult.value.reason);
}

if (postResult.value.__typename === "RelaySuccess") {
const { txId: id, txHash: hash } = postResult.value;
const date = new Date().toISOString();
console.log(`${handle} created a post: ${id}, hash: ${hash}, ipfs: ${contentURI}, date: ${date}`);
return NextResponse.json({ id, hash }, { status: 200, statusText: "Success" });
}

throw new Error("Unknown error. This should never happen.");
}
) {}
3 changes: 2 additions & 1 deletion src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FeedSuspense } from "~/components/FeedSuspense";
import { SearchBar } from "~/components/menu/Search";
import { lensItemToPost } from "~/components/post/Post";
import { Button } from "~/components/ui/button";
import { PostView } from "./post/PostView";

export function Search() {
const params = useSearchParams();
Expand All @@ -18,7 +19,7 @@ export function Search() {
if (error && query) throw new Error(error.message);
if (loading) return <FeedSuspense />;

const posts = data?.map((item) => lensItemToPost(item));
const posts = data?.map(lensItemToPost).map((post) => <PostView key={post.id} item={post} />);

return (
<>
Expand Down

0 comments on commit 6f88242

Please sign in to comment.