Skip to content

Commit 5da19b5

Browse files
authored
feat: remember sort setting works on search/explore (#2060)
1 parent 240d77c commit 5da19b5

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

src/features/feed/sort/feedSortSlice.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { RootState } from "#/store";
88
import { AnyFeed, serializeFeedName } from "../helpers";
99
import { VgerPostSortType } from "./PostSort";
1010
import { VgerSearchSortType } from "./SearchSort";
11+
import { FeedSortContext } from "./useFeedSort";
1112

1213
interface PostSortState {
1314
/**
@@ -81,13 +82,7 @@ export default feedSortSlice.reducer;
8182

8283
export const getFeedSort = createAsyncThunk(
8384
"feedSort/getFeedSort",
84-
async ({
85-
feed,
86-
context,
87-
}: {
88-
feed: AnyFeed;
89-
context: "posts" | "comments" | "search" | "communities";
90-
}) => {
85+
async ({ feed, context }: { feed: AnyFeed; context: FeedSortContext }) => {
9186
const feedName = serializeFeedName(feed);
9287
const sort =
9388
(await db.getSetting(getDefaultSortSettingForContext(context), {
@@ -103,18 +98,13 @@ export const getFeedSort = createAsyncThunk(
10398
);
10499

105100
export const getFeedSortSelectorBuilder =
106-
(
107-
feed: AnyFeed | undefined,
108-
context: "posts" | "comments" | "search" | "communities",
109-
) =>
101+
(feed: AnyFeed | undefined, context: FeedSortContext) =>
110102
(state: RootState) =>
111103
feed
112104
? state.feedSort.sortByContextByFeedName[context][serializeFeedName(feed)]
113105
: null;
114106

115-
function getDefaultSortSettingForContext(
116-
context: "posts" | "comments" | "search" | "communities",
117-
) {
107+
function getDefaultSortSettingForContext(context: FeedSortContext) {
118108
switch (context) {
119109
case "comments":
120110
return "default_comment_sort_by_feed";

src/features/feed/sort/useFeedSort.tsx

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ interface Sorts {
5252
communities: CommunitySortType;
5353
}
5454

55-
export default function useFeedSort<
56-
Context extends "posts" | "comments" | "search" | "communities",
57-
>(
55+
export type FeedSortContext = "posts" | "comments" | "search" | "communities";
56+
57+
export default function useFeedSort<Context extends FeedSortContext>(
5858
context: Context,
5959
feed?: AnyFeed | undefined,
6060
overrideSort?: VgerSorts[Context],
@@ -72,7 +72,9 @@ export default function useFeedSort<
7272
mode ? state.settings.general[context].sort[mode] : undefined,
7373
) as Sort | undefined;
7474
const rememberCommunitySort = useAppSelector(
75-
(state) => state.settings.general[context].rememberCommunitySort,
75+
(state) =>
76+
state.settings.general[findFeedContext(feed) ?? context]
77+
.rememberCommunitySort,
7678
);
7779

7880
const [sort, _setSort] = useState<Sort | undefined>(
@@ -127,9 +129,29 @@ export default function useFeedSort<
127129
return [sort, setSort] as const;
128130
}
129131

130-
export function useFeedSortParams<
131-
Context extends "posts" | "comments" | "search" | "communities",
132-
>(
132+
function findFeedContext(
133+
feed: AnyFeed | undefined,
134+
): FeedSortContext | undefined {
135+
if (!feed) return;
136+
if (!("internal" in feed)) return;
137+
138+
switch (feed.internal) {
139+
case "CommentsSearch":
140+
return "comments";
141+
case "PostsSearch":
142+
return "posts";
143+
case "CommunitiesSearch":
144+
return "communities";
145+
case "CommunitiesExplore":
146+
return "communities";
147+
case "ProfilePosts":
148+
return "posts";
149+
case "ProfileComments":
150+
return "comments";
151+
}
152+
}
153+
154+
export function useFeedSortParams<Context extends FeedSortContext>(
133155
context: Context,
134156
sort: VgerSorts[Context] | undefined,
135157
): Sorts[Context] | undefined {
@@ -140,9 +162,11 @@ export function useFeedSortParams<
140162
return convertSortToLemmyParams(context, sort, mode);
141163
}
142164

143-
function convertSortToLemmyParams<
144-
Context extends "posts" | "comments" | "search" | "communities",
145-
>(context: Context, sort: VgerSorts[Context], mode: ThreadiverseMode) {
165+
function convertSortToLemmyParams<Context extends FeedSortContext>(
166+
context: Context,
167+
sort: VgerSorts[Context],
168+
mode: ThreadiverseMode,
169+
) {
146170
switch (context) {
147171
case "posts":
148172
return convertPostSortToParams(sort as VgerSorts["posts"], mode);

0 commit comments

Comments
 (0)