Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-sherman committed Nov 22, 2024
1 parent 5c95747 commit d2f6b1b
Show file tree
Hide file tree
Showing 7 changed files with 819 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,26 @@ import { parseReportForm } from "@/lib/data/db/report-shared";
import { createReport } from "@/lib/data/db/report";
import { getVoteForComment } from "@/lib/data/db/vote";
import { ensureUser } from "@/lib/data/user";
import { createHeadlessEditor } from "@lexical/headless";
import {
SerializedEditorState,
$parseSerializedNode,

Check failure on line 19 in packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx

View workflow job for this annotation

GitHub Actions / lint

'$parseSerializedNode' is defined but never used. Allowed unused vars must match /^_/u
LexicalEditor,

Check failure on line 20 in packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx

View workflow job for this annotation

GitHub Actions / lint

'LexicalEditor' is defined but never used. Allowed unused vars must match /^_/u
$getRoot,
EditorState,
LexicalNode,
$isTextNode,
$isElementNode,
} from "lexical";
import { revalidatePath } from "next/cache";
import { deletePost } from "@/lib/data/atproto/post";

export async function createCommentAction(
input: { parentRkey?: string; postRkey: string; postAuthorDid: DID },
_prevState: unknown,
formData: FormData,
) {
const content = formData.get("comment") as string;
export async function createCommentAction(input: {
parentRkey?: string;
postRkey: string;
postAuthorDid: DID;
content: SerializedEditorState;
}) {
const user = await ensureUser();

const [post, comment] = await Promise.all([

Check failure on line 38 in packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx

View workflow job for this annotation

GitHub Actions / lint

'comment' is assigned a value but never used. Allowed unused elements of array destructuring patterns must match /^_/u
Expand All @@ -41,13 +53,15 @@ export async function createCommentAction(
throw new Error(`[naughty] Cannot comment on deleted post. ${user.did}`);
}

const { rkey } = await createComment({
content,
post,
parent: comment,
});
await waitForComment(rkey);
revalidatePath(`/post`);
const state = createHeadlessEditor().parseEditorState(input.content);

Check failure on line 56 in packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx

View workflow job for this annotation

GitHub Actions / lint

'state' is assigned a value but never used. Allowed unused vars must match /^_/u

// const { rkey } = await createComment({
// content,
// post,
// parent: comment,
// });
// await waitForComment(rkey);
// revalidatePath(`/post`);
}

const MAX_POLLS = 15;
Expand All @@ -64,6 +78,28 @@ async function waitForComment(rkey: string) {
}
}

function editorStateToCommentContent(editorState: EditorState) {

Check failure on line 81 in packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx

View workflow job for this annotation

GitHub Actions / lint

'editorStateToCommentContent' is defined but never used. Allowed unused vars must match /^_/u
return editorState.read(() => {
const root = $getRoot();
root.getChildren().forEach((child) => {});

Check failure on line 84 in packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx

View workflow job for this annotation

GitHub Actions / lint

'child' is defined but never used. Allowed unused args must match /^_/u

const text = root.getTextContent();

Check failure on line 86 in packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx

View workflow job for this annotation

GitHub Actions / lint

'text' is assigned a value but never used. Allowed unused vars must match /^_/u
});
}

function $nodeToFacets(node: LexicalNode) {

Check failure on line 90 in packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx

View workflow job for this annotation

GitHub Actions / lint

'$nodeToFacets' is defined but never used. Allowed unused vars must match /^_/u
if ($isTextNode(node)) {
if (node.hasFormat("bold")) {
return node.selectStart;
}
}
if ($isElementNode(node) && node.isEmpty()) return [];
}

export async function deletePostAction(rkey: string) {
await deletePost(rkey);
}

export async function deleteCommentAction(rkey: string) {
await ensureUser();
await deleteComment(rkey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
AlertDialogTrigger,
} from "@/lib/components/ui/alert-dialog";
import { Button } from "@/lib/components/ui/button";
import { Textarea } from "@/lib/components/ui/textarea";
import { EditableTextArea } from "@/lib/components/ui/textarea";
import { SimpleTooltip } from "@/lib/components/ui/tooltip";
import { useToast } from "@/lib/components/ui/use-toast";
import {
Expand All @@ -22,13 +22,7 @@ import {
reportCommentAction,
} from "./actions";
import { ChatBubbleIcon, TrashIcon } from "@radix-ui/react-icons";
import {
useActionState,
useRef,
useState,
useId,
startTransition,
} from "react";
import { useRef, useState, useId, startTransition, useTransition } from "react";
import {
VoteButton,
VoteButtonState,
Expand All @@ -44,6 +38,7 @@ import { DeleteButton } from "@/app/(app)/_components/delete-button";
import { cva, VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
import { ShareDropdownButton } from "@/app/(app)/_components/share-button";
import { LexicalEditor } from "lexical";

const commentVariants = cva(undefined, {
variants: {
Expand Down Expand Up @@ -226,7 +221,6 @@ export function NewComment({
postRkey,
postAuthorDid,
extraButton,
textAreaRef,
onActionDone,
}: {
parentRkey?: string;
Expand All @@ -237,23 +231,28 @@ export function NewComment({
extraButton?: React.ReactNode;
textAreaRef?: React.RefObject<HTMLTextAreaElement>;
}) {
const editorRef = useRef<LexicalEditor | null>(null);
const [input, setInput] = useState("");
const [_, action, isPending] = useActionState(
createCommentAction.bind(null, { parentRkey, postRkey, postAuthorDid }),
undefined,
);
const [isPending, startTransition] = useTransition();

const id = useId();
const textAreaId = `${id}-comment`;

return (
<form
action={action}
onSubmit={(event) => {
event.preventDefault();
startTransition(() => {
action(new FormData(event.currentTarget));
startTransition(async () => {
const state = editorRef.current?.getEditorState().toJSON();
if (!state) throw new Error("Empty comment");
console.log(state);
await createCommentAction({
parentRkey,
postRkey,
postAuthorDid,
content: state,
});
onActionDone?.();
setInput("");
});
}}
aria-busy={isPending}
Expand All @@ -271,7 +270,7 @@ export function NewComment({
}}
className="space-y-2"
>
<Textarea
<EditableTextArea
value={input}
onChange={(event) => {
setInput(event.target.value);
Expand All @@ -280,10 +279,10 @@ export function NewComment({
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={autoFocus}
name="comment"
ref={textAreaRef}
ref={editorRef}
placeholder="Write a comment..."
disabled={isPending}
className="resize-y flex-1"
className="resize-y flex-1 overflow-auto grow"
/>
<div className="w-full flex justify-between">
<InputLengthIndicator
Expand Down
Loading

0 comments on commit d2f6b1b

Please sign in to comment.