-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sortable profile posts and comments (#1782)
- Loading branch information
Showing
8 changed files
with
238 additions
and
97 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
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,53 @@ | ||
import { IonButtons, IonTitle, IonToolbar } from "@ionic/react"; | ||
import { IonPage } from "@ionic/react"; | ||
import { IonBackButton } from "@ionic/react"; | ||
import { useParams } from "react-router-dom"; | ||
|
||
import { FetchFn } from "#/features/feed/Feed"; | ||
import PostCommentFeed, { | ||
PostCommentItem, | ||
} from "#/features/feed/PostCommentFeed"; | ||
import AppHeader from "#/features/shared/AppHeader"; | ||
import { useBuildGeneralBrowseLink } from "#/helpers/routes"; | ||
import FeedContent from "#/routes/pages/shared/FeedContent"; | ||
|
||
interface BaseProfileFeedItemsPageProps { | ||
label: string; | ||
fetchFn: FetchFn<PostCommentItem>; | ||
sortComponent?: React.ReactNode; | ||
} | ||
|
||
export default function BaseProfileFeedItemsPage({ | ||
fetchFn, | ||
sortComponent, | ||
label, | ||
}: BaseProfileFeedItemsPageProps) { | ||
const buildGeneralBrowseLink = useBuildGeneralBrowseLink(); | ||
const { handle } = useParams<{ handle: string }>(); | ||
|
||
return ( | ||
<IonPage> | ||
<AppHeader> | ||
<IonToolbar> | ||
<IonTitle>{label}</IonTitle> | ||
<IonButtons slot="start"> | ||
<IonBackButton | ||
// Kinda hacky since Ionic doesn't handle clipping | ||
text={handle.length > 10 ? `${handle.slice(0, 10)}...` : handle} | ||
defaultHref={buildGeneralBrowseLink(`/u/${handle}`)} | ||
/> | ||
</IonButtons> | ||
|
||
{sortComponent && <IonButtons slot="end">{sortComponent}</IonButtons>} | ||
</IonToolbar> | ||
</AppHeader> | ||
<FeedContent> | ||
<PostCommentFeed | ||
fetchFn={fetchFn} | ||
filterHiddenPosts={false} | ||
filterKeywordsAndWebsites={false} | ||
/> | ||
</FeedContent> | ||
</IonPage> | ||
); | ||
} |
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,58 @@ | ||
import { CommentSortType, PostSortType } from "lemmy-js-client"; | ||
import { useParams } from "react-router-dom"; | ||
|
||
import CommentSort from "#/features/comment/CommentSort"; | ||
import { FetchFn } from "#/features/feed/Feed"; | ||
import { PostCommentItem } from "#/features/feed/PostCommentFeed"; | ||
import useFeedSort from "#/features/feed/sort/useFeedSort"; | ||
import useClient from "#/helpers/useClient"; | ||
import { LIMIT } from "#/services/lemmy"; | ||
|
||
import BaseProfileFeedItemsPage from "./BaseProfileFeedItemsPage"; | ||
|
||
export default function ProfileFeedCommentsPage() { | ||
const client = useClient(); | ||
const { handle } = useParams<{ handle: string }>(); | ||
|
||
const [sort, setSort] = useFeedSort( | ||
"comments", | ||
{ | ||
internal: `ProfileComments`, | ||
}, | ||
"New", | ||
); | ||
|
||
const fetchFn: FetchFn<PostCommentItem> = async (pageData, ...rest) => { | ||
const { comments } = await client.getPersonDetails( | ||
{ | ||
...pageData, | ||
limit: LIMIT, | ||
username: handle, | ||
sort: sort ? convertCommentSortToPostSort(sort) : "New", | ||
}, | ||
...rest, | ||
); | ||
|
||
return comments; | ||
}; | ||
|
||
return ( | ||
<BaseProfileFeedItemsPage | ||
label="Comments" | ||
fetchFn={fetchFn} | ||
sortComponent={<CommentSort sort={sort} setSort={setSort} />} | ||
/> | ||
); | ||
} | ||
|
||
function convertCommentSortToPostSort(sort: CommentSortType): PostSortType { | ||
switch (sort) { | ||
case "Controversial": | ||
case "Hot": | ||
case "New": | ||
case "Old": | ||
return sort; | ||
case "Top": | ||
return "TopAll"; | ||
} | ||
} |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useParams } from "react-router-dom"; | ||
|
||
import { FetchFn } from "#/features/feed/Feed"; | ||
import { PostCommentItem } from "#/features/feed/PostCommentFeed"; | ||
import PostSort from "#/features/feed/PostSort"; | ||
import useFeedSort from "#/features/feed/sort/useFeedSort"; | ||
import useClient from "#/helpers/useClient"; | ||
import { LIMIT } from "#/services/lemmy"; | ||
|
||
import BaseProfileFeedItemsPage from "./BaseProfileFeedItemsPage"; | ||
|
||
export default function ProfileFeedPostsPage() { | ||
const client = useClient(); | ||
const { handle } = useParams<{ handle: string }>(); | ||
|
||
const [sort, setSort] = useFeedSort( | ||
"posts", | ||
{ | ||
internal: `ProfilePosts`, | ||
}, | ||
"New", | ||
); | ||
|
||
const fetchFn: FetchFn<PostCommentItem> = async (pageData, ...rest) => { | ||
const { posts } = await client.getPersonDetails( | ||
{ | ||
...pageData, | ||
limit: LIMIT, | ||
username: handle, | ||
sort: sort ?? "New", | ||
}, | ||
...rest, | ||
); | ||
|
||
return posts; | ||
}; | ||
|
||
return ( | ||
<BaseProfileFeedItemsPage | ||
label="Posts" | ||
fetchFn={fetchFn} | ||
sortComponent={<PostSort sort={sort} setSort={setSort} />} | ||
/> | ||
); | ||
} |
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,31 @@ | ||
import { useParams } from "react-router-dom"; | ||
|
||
import { FetchFn } from "#/features/feed/Feed"; | ||
import { PostCommentItem } from "#/features/feed/PostCommentFeed"; | ||
import { sortPostCommentByPublished } from "#/helpers/lemmy"; | ||
import useClient from "#/helpers/useClient"; | ||
import { LIMIT } from "#/services/lemmy"; | ||
|
||
import BaseProfileFeedItemsPage from "./BaseProfileFeedItemsPage"; | ||
|
||
export default function ProfileFeedSavedPage() { | ||
const { handle } = useParams<{ handle: string }>(); | ||
const client = useClient(); | ||
|
||
const fetchFn: FetchFn<PostCommentItem> = async (pageData, ...rest) => { | ||
const { comments, posts } = await client.getPersonDetails( | ||
{ | ||
...pageData, | ||
limit: LIMIT, | ||
username: handle, | ||
sort: "New", | ||
saved_only: true, | ||
}, | ||
...rest, | ||
); | ||
|
||
return [...comments, ...posts].sort(sortPostCommentByPublished); | ||
}; | ||
|
||
return <BaseProfileFeedItemsPage label="Saved" fetchFn={fetchFn} />; | ||
} |
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,39 @@ | ||
import { GetComments, GetPosts } from "lemmy-js-client"; | ||
|
||
import { FetchFn } from "#/features/feed/Feed"; | ||
import { PostCommentItem } from "#/features/feed/PostCommentFeed"; | ||
import { sortPostCommentByPublished } from "#/helpers/lemmy"; | ||
import useClient from "#/helpers/useClient"; | ||
import { LIMIT } from "#/services/lemmy"; | ||
|
||
import BaseProfileFeedItemsPage from "./BaseProfileFeedItemsPage"; | ||
|
||
interface ProfileFeedVotedPageProps { | ||
type: "Upvoted" | "Downvoted"; | ||
} | ||
|
||
export default function ProfileFeedVotedPage({ | ||
type, | ||
}: ProfileFeedVotedPageProps) { | ||
const client = useClient(); | ||
|
||
const fetchFn: FetchFn<PostCommentItem> = async (pageData, ...rest) => { | ||
const requestPayload: GetPosts & GetComments = { | ||
...pageData, | ||
limit: Math.floor(LIMIT / 2), | ||
sort: "New", | ||
liked_only: type === "Upvoted", | ||
disliked_only: type === "Downvoted", | ||
show_read: true, | ||
}; | ||
|
||
const [{ posts }, { comments }] = await Promise.all([ | ||
client.getPosts(requestPayload, ...rest), | ||
client.getComments(requestPayload, ...rest), | ||
]); | ||
|
||
return [...comments, ...posts].sort(sortPostCommentByPublished); | ||
}; | ||
|
||
return <BaseProfileFeedItemsPage label={type} fetchFn={fetchFn} />; | ||
} |
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