Skip to content

Commit

Permalink
Refactor username decoding and URL generation
Browse files Browse the repository at this point in the history
  • Loading branch information
thebkht committed Nov 16, 2023
1 parent 5ddd5f8 commit 155f04d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions app/(Main)/[username]/[url]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ type Props = {
}

async function getPostData(username: string, url: string) {
const decodedUsername = decodeURIComponent(username);
const post = await postgres.post.findFirst({
where: {
url: url,
author: {
username: username
username: decodedUsername.substring(1)
}
},
include: {
Expand Down Expand Up @@ -93,9 +94,10 @@ async function getPostData(username: string, url: string) {
export default async function PostLayout(
{ children, params }: Props
) {
const decodedUsername = decodeURIComponent(params.username);
const author = await postgres.user.findFirst({
where: {
username: params.username
username: decodedUsername.substring(1)
},
include: {
posts: {
Expand Down
4 changes: 2 additions & 2 deletions app/(Main)/[username]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export async function generateMetadata(
{ params }: Props
): Promise<Metadata> {
try {
const encodedString = params.username.replace(/ /g, "%20");
const response = await fetch(`${process.env.DOMAIN}/api/users/${encodedString}`);
const decodedUsername = decodeURIComponent(params.username);
const response = await fetch(`${process.env.DOMAIN}/api/users/${decodedUsername.substring(1)}`);
if (!response.ok) {
throw new Error(`Error fetching user data: ${response.statusText}`);
}
Expand Down
2 changes: 1 addition & 1 deletion components/settings/profile-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const defaultValues: Partial<ProfileFormValues> = {
action: (
<ToastAction
altText="View profile"
onClick={() => router.push(`/user/${data.username}`)}
onClick={() => router.push(`/@${data.username}`)}
className="hover:text-secondary-foreground"
>
View profile
Expand Down

0 comments on commit 155f04d

Please sign in to comment.