Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add PartnerSubmittedLeadComments component

Rafif526791bffef
diff --git a/apps/web/lib/swr/use-partner-submitted-lead-comments.ts b/apps/web/lib/swr/use-partner-submitted-lead-comments.ts
new file mode 100644
index 0000000000..2aa01e181a
--- /dev/null
+++ b/apps/web/lib/swr/use-partner-submitted-lead-comments.ts
@@ -0,0 +1,32 @@
+import { fetcher } from "@dub/utils";
+import { useParams } from "next/navigation";
+import useSWR, { SWRConfiguration } from "swr";
+import { SubmittedLeadCommentProps } from "../types";
+
+export function usePartnerSubmittedLeadComments(
+ {
+ leadId,
+ }: {
+ leadId: string;
+ },
+ swrOptions: SWRConfiguration = {},
+) {
+ const { programSlug } = useParams<{ programSlug: string }>();
+
+ const { data, isLoading, error, mutate } = useSWR<
+ (SubmittedLeadCommentProps & { delivered?: false })[]
+ >(
+ programSlug
+ ? `/api/partner-profile/programs/${programSlug}/submitted-leads/${leadId}/comments`
+ : undefined,
+ fetcher,
+ swrOptions,
+ );
+
+ return {
+ comments: data,
+ loading: isLoading,
+ error,
+ mutate,
+ };
+}
diff --git a/apps/web/ui/submitted-leads/partner-submitted-lead-comments.tsx b/apps/web/ui/submitted-leads/partner-submitted-lead-comments.tsx
new file mode 100644
index 0000000000..6bfd0fbd0d
--- /dev/null
+++ b/apps/web/ui/submitted-leads/partner-submitted-lead-comments.tsx
@@ -0,0 +1,107 @@
+"use client";
+
+import { createPartnerSubmittedLeadCommentAction } from "@/lib/submitted-leads/create-partner-submitted-lead-comment-action";
+import usePartnerProfile from "@/lib/swr/use-partner-profile";
+import { usePartnerSubmittedLeadComments } from "@/lib/swr/use-partner-submitted-lead-comments";
+import useProgramEnrollment from "@/lib/swr/use-program-enrollment";
+import useUser from "@/lib/swr/use-user";
+import { MessageInput } from "@/ui/shared/message-input";
+import { useAction } from "next-safe-action/hooks";
+import { toast } from "sonner";
+import { v4 as uuid } from "uuid";
+import { CommentCard, CommentWithDelivery } from "./submitted-lead-comments";
+
+export function PartnerSubmittedLeadComments({ leadId }: { leadId: string }) {
+ const { user } = useUser();
+ const { partner } = usePartnerProfile();
+ const { programEnrollment } = useProgramEnrollment();
+ const program = programEnrollment?.program;
+
+ const { comments, loading, mutate } = usePartnerSubmittedLeadComments(
+ { leadId },
+ { keepPreviousData: true },
+ );
+
+ const { executeAsync: createComment } = useAction(
+ createPartnerSubmittedLeadCommentAction,
+ );
+
+ return (
+ <div className="flex flex-col gap-4">
+ <MessageInput
+ onSendMessage={(text) => {
+ if (!user || !partner || !program) return false;
+
+ const createdAt = new Date();
+
+ const optimisticComment: CommentWithDelivery = {
+ id: `tmp_${uuid()}`,
+ leadId,
+ userId: user.id,
+ partnerId: partner.id,
+ text,
+ partnerVisible: true,
+ createdAt,
+ updatedAt: createdAt,
+ user: {
+ id: user.id,
+ name: user.name,
+ image: user.image || null,
+ },
+ delivered: false,
+ };
+
+ mutate(
+ async (data) => {
+ const result = await createComment({
+ programId: program.id,
+ leadId,
+ text,
+ });
+
+ if (!result?.data?.comment)
+ throw new Error(
+ result?.serverError || "Failed to post comment",
+ );
+
+ return data
+ ? [result.data.comment, ...data]
+ : [result.data.comment];
+ },
+ {
+ optimisticData: (data) =>
+ data ? [optimisticComment, ...data] : [optimisticComment],
+ rollbackOnError: true,
+ revalidate: true,
+ },
+ ).catch((e) => {
+ console.log("Failed to post comment", e);
+ toast.error("Failed to post comment");
+ });
+ }}
+ placeholder="Leave a comment for this program"
+ sendButtonText="Post"
+ />
+
+ {comments && program ? (
+ comments.length > 0 ? (
+ <div className="flex flex-col gap-4">
+ {comments.map((comment) => (
+ <CommentCard
+ key={comment.id}
+ comment={comment}
+ program={program}
+ />
+ ))}
+ </div>
+ ) : null
+ ) : loading || !program ? (
+ <CommentCard className="opacity-50" />
+ ) : (
+ <div className="text-content-muted py-4 text-center text-xs">
+ Failed to load comments
+ </div>
+ )}
+ </div>
+ );
+}