-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Posts #195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: Posts #195
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
app/(dashboard)/[tenant]/projects/[projectId]/posts/page.tsx
This file contains hidden or 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,106 @@ | ||
| "use client"; | ||
|
|
||
| import { Title } from "@radix-ui/react-dialog"; | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import Link from "next/link"; | ||
| import { useParams } from "next/navigation"; | ||
| import { parseAsBoolean, useQueryState } from "nuqs"; | ||
| import { useState } from "react"; | ||
| import EmptyState from "@/components/core/empty-state"; | ||
| import { Panel } from "@/components/core/panel"; | ||
| import PageSection from "@/components/core/section"; | ||
| import PostForm from "@/components/form/post"; | ||
| import PageTitle from "@/components/layout/page-title"; | ||
| import PostsList from "@/components/project/posts/posts-list"; | ||
| import { buttonVariants } from "@/components/ui/button"; | ||
| import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; | ||
| import { useTRPC } from "@/trpc/client"; | ||
|
|
||
| export default function Posts() { | ||
| const { projectId, tenant } = useParams(); | ||
| const [create, setCreate] = useQueryState( | ||
| "create", | ||
| parseAsBoolean.withDefault(false), | ||
| ); | ||
| const [activeTab, setActiveTab] = useState("published"); | ||
|
|
||
| const trpc = useTRPC(); | ||
|
|
||
| const { data: project } = useQuery( | ||
| trpc.projects.getProjectById.queryOptions({ | ||
| id: +projectId!, | ||
| }), | ||
| ); | ||
|
|
||
| const { data: publishedPosts = [] } = useQuery({ | ||
| ...trpc.posts.list.queryOptions({ | ||
| projectId: +projectId!, | ||
| }), | ||
| enabled: activeTab === "published", | ||
| }); | ||
|
|
||
| const { data: myDrafts = [] } = useQuery({ | ||
| ...trpc.posts.myDrafts.queryOptions({ | ||
| projectId: +projectId!, | ||
| }), | ||
| enabled: activeTab === "drafts", | ||
| }); | ||
|
|
||
| return ( | ||
| <> | ||
| <PageTitle | ||
| title="Posts" | ||
| actions={ | ||
| project?.canEdit ? ( | ||
| <Link | ||
| href={`/${tenant}/projects/${projectId}/posts?create=true`} | ||
| className={buttonVariants()} | ||
| > | ||
| New | ||
| </Link> | ||
| ) : undefined | ||
| } | ||
| /> | ||
|
|
||
| <PageSection transparent> | ||
| <Tabs value={activeTab} onValueChange={setActiveTab} className="w-full"> | ||
| <TabsList className="mb-4"> | ||
| <TabsTrigger value="published">Published</TabsTrigger> | ||
| <TabsTrigger value="drafts">My Drafts</TabsTrigger> | ||
| </TabsList> | ||
|
|
||
| <TabsContent value="published"> | ||
| {publishedPosts.length ? ( | ||
| <PostsList posts={publishedPosts} projectId={+projectId!} /> | ||
| ) : ( | ||
| <EmptyState | ||
| show={!publishedPosts.length} | ||
| label="post" | ||
| createLink={`/${tenant}/projects/${projectId}/posts?create=true`} | ||
| /> | ||
| )} | ||
| </TabsContent> | ||
|
|
||
| <TabsContent value="drafts"> | ||
| {myDrafts.length ? ( | ||
| <PostsList posts={myDrafts} projectId={+projectId!} /> | ||
| ) : ( | ||
| <div className="text-center text-muted-foreground py-8"> | ||
| No draft posts | ||
| </div> | ||
| )} | ||
| </TabsContent> | ||
| </Tabs> | ||
| </PageSection> | ||
|
|
||
| {project?.canEdit && ( | ||
| <Panel open={create} setOpen={setCreate}> | ||
| <Title> | ||
| <PageTitle title="New Post" compact /> | ||
| </Title> | ||
| <PostForm /> | ||
| </Panel> | ||
| )} | ||
| </> | ||
| ); | ||
| } | ||
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate projectId before using non-null assertions.
The pattern
+projectId!is used repeatedly (lines 31, 37, 44, 74, 86) to coerce and assert the projectId parameter. This bypasses TypeScript's safety checks and could lead to runtime errors if projectId is undefined or not a valid numeric string.Consider validating the parameter early in the component:
export default function Posts() { const { projectId, tenant } = useParams(); + + if (!projectId || Array.isArray(projectId)) { + throw new Error("Invalid project ID"); + } + + const numericProjectId = Number(projectId); + if (Number.isNaN(numericProjectId)) { + throw new Error("Project ID must be a number"); + } + const [create, setCreate] = useQueryState( "create", parseAsBoolean.withDefault(false), );Then use
numericProjectIdthroughout instead of+projectId!.Also applies to: 37-37, 44-44, 74-74, 86-86
🤖 Prompt for AI Agents