forked from get-convex/ents-saas-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement message board on team home page
- Loading branch information
Showing
13 changed files
with
179 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useCurrentTeam } from "@/app/t/[teamSlug]/hooks"; | ||
import { Button } from "@/components/ui/button"; | ||
import { ScrollArea } from "@/components/ui/scroll-area"; | ||
import { Separator } from "@/components/ui/separator"; | ||
import { Textarea } from "@/components/ui/textarea"; | ||
import { api } from "@/convex/_generated/api"; | ||
import { useMutation, usePaginatedQuery } from "convex/react"; | ||
import { useCallback, useRef, useState } from "react"; | ||
|
||
export function MessageBoard() { | ||
const team = useCurrentTeam(); | ||
const { | ||
results: messages, | ||
loadMore, | ||
status, | ||
} = usePaginatedQuery( | ||
api.users.teams.messages.list, | ||
team == null ? "skip" : { teamId: team._id }, | ||
{ initialNumItems: 10 } | ||
); | ||
const [message, setMessage] = useState(""); | ||
const sendMessage = useMutation(api.users.teams.messages.create); | ||
const listRef = useRef<HTMLElement>(null); | ||
const handleScroll = useCallback(() => { | ||
if (listRef.current === null) { | ||
return; | ||
} | ||
const { scrollTop, scrollHeight, clientHeight } = listRef.current; | ||
if ( | ||
scrollHeight - scrollTop <= clientHeight * 1.5 && | ||
status === "CanLoadMore" | ||
) { | ||
loadMore(10); | ||
} | ||
}, [loadMore, status]); | ||
|
||
return ( | ||
<div className="max-w-xl flex flex-col gap-2 mt-8"> | ||
<form | ||
className="flex gap-2" | ||
onSubmit={(event) => { | ||
event.preventDefault(); | ||
void sendMessage({ text: message, teamId: team!._id }).then(() => { | ||
setMessage(""); | ||
}); | ||
}} | ||
> | ||
<Textarea | ||
name="message" | ||
placeholder="Message text..." | ||
value={message} | ||
onChange={(event) => setMessage(event.target.value)} | ||
/> | ||
<Button type="submit">Send Message</Button> | ||
</form> | ||
<ScrollArea className="h-72 rounded-md border" onScroll={handleScroll}> | ||
<div className="p-4"> | ||
{messages.map((message) => ( | ||
<div key={message._id} className="text-sm"> | ||
<div> | ||
<span className="font-semibold">{message.author}:</span>{" "} | ||
{message.text} | ||
</div> | ||
<Separator className="my-2" /> | ||
</div> | ||
))} | ||
{status === "Exhausted" && messages.length === 0 && ( | ||
<div className="text-muted-foreground"> | ||
There are no messages posted yet | ||
</div> | ||
)} | ||
</div> | ||
</ScrollArea> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
"use client"; | ||
|
||
import { useState } from "react"; | ||
import { MessageBoard } from "@/app/t/[teamSlug]/MessageBoard"; | ||
import { useCurrentTeam } from "@/app/t/[teamSlug]/hooks"; | ||
|
||
export default function Home() { | ||
const [count, setCount] = useState(0); | ||
const team = useCurrentTeam(); | ||
if (team == null) { | ||
return null; | ||
} | ||
return ( | ||
<main className="container"> | ||
<h1 className="text-4xl font-extrabold my-8">Hello there!</h1> | ||
Count: {count} | ||
<button onClick={() => setCount((count) => count + 1)}>Click me</button> | ||
<h1 className="text-4xl font-extrabold my-8"> | ||
{team.name} | ||
{"'"}s Projects | ||
</h1> | ||
<p>This is where your product actually lives.</p> | ||
<p>As an example, here{"'"}s a message board for the team:</p> | ||
<MessageBoard /> | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { v } from "convex/values"; | ||
import { mutation, query } from "../../functions"; | ||
import { viewerHasPermissionX } from "../../permissions"; | ||
import { paginationOptsValidator } from "convex/server"; | ||
|
||
export const list = query({ | ||
args: { | ||
teamId: v.id("teams"), | ||
paginationOpts: paginationOptsValidator, | ||
}, | ||
handler: async (ctx, { teamId, paginationOpts }) => { | ||
if (ctx.viewer === null) { | ||
return { | ||
page: [], | ||
isDone: true, | ||
continueCursor: "", | ||
}; | ||
} | ||
await viewerHasPermissionX(ctx, teamId, "Contribute"); | ||
return await ctx | ||
.table("teams") | ||
.getX(teamId) | ||
.edge("messages") | ||
.order("desc") | ||
.paginate(paginationOpts) | ||
.map(async (message) => { | ||
const user = await message.edge("member").edge("user"); | ||
return { | ||
_id: message._id, | ||
_creationTime: message._creationTime, | ||
text: message.text, | ||
author: user.firstName ?? user.fullName, | ||
}; | ||
}); | ||
}, | ||
}); | ||
|
||
export const create = mutation({ | ||
args: { | ||
teamId: v.id("teams"), | ||
text: v.string(), | ||
}, | ||
handler: async (ctx, { teamId, text }) => { | ||
const member = await viewerHasPermissionX(ctx, teamId, "Contribute"); | ||
await ctx.table("messages").insert({ | ||
text, | ||
teamId: teamId, | ||
memberId: member._id, | ||
}); | ||
}, | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters