Skip to content

Commit

Permalink
Add persisted search sort (#1580)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored Aug 11, 2024
1 parent 5e927bc commit b8f305c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/features/feed/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { ListingType } from "lemmy-js-client";
import { getFeedUrlName } from "../community/mod/ModActions";

type InternalFeedType =
| "PostsSearch"
| "CommentsSearch"
| "CommunitiesSearch"
| "CommunitiesExplore";

export type AnyFeed =
| {
remoteCommunityHandle: string;
}
| {
listingType: ListingType;
}
| {
internal: InternalFeedType;
};

export function serializeFeedName(feed: AnyFeed): string {
Expand All @@ -15,6 +24,8 @@ export function serializeFeedName(feed: AnyFeed): string {
return feed.remoteCommunityHandle; // always contains @ - will never overlap with getFeedUrlName
case "listingType" in feed:
return getFeedUrlName(feed.listingType);
case "internal" in feed:
return `@@voyager_${feed.internal}`;
default:
return feed;
}
Expand Down
15 changes: 3 additions & 12 deletions src/features/post/appearance/appearanceSlice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PayloadAction, createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { ListingType } from "lemmy-js-client";
import { PostAppearanceType, db } from "../../../services/db";
import { serializeFeedName } from "../../feed/helpers";
import { AnyFeed, serializeFeedName } from "../../feed/helpers";

interface PostAppearanceState {
/**
Expand All @@ -21,7 +20,7 @@ export const postAppearanceSlice = createSlice({
setPostAppeartance: (
state,
action: PayloadAction<{
feed: FeedSortFeed;
feed: AnyFeed;
postAppearance: PostAppearanceType;
}>,
) => {
Expand All @@ -47,17 +46,9 @@ export const { setPostAppeartance } = postAppearanceSlice.actions;

export default postAppearanceSlice.reducer;

export type FeedSortFeed =
| {
remoteCommunityHandle: string;
}
| {
listingType: ListingType;
};

export const getPostAppearance = createAsyncThunk(
"postAppearance/getPostAppearance",
async (feed: FeedSortFeed) => {
async (feed: AnyFeed) => {
const feedName = serializeFeedName(feed);
const postAppearance =
(await db.getSetting("post_appearance_type", {
Expand Down
6 changes: 5 additions & 1 deletion src/routes/pages/search/CommunitiesResultsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export default function CommunitiesResultsPage({
}: CommunitiesResultsPageProps) {
const buildGeneralBrowseLink = useBuildGeneralBrowseLink();
const client = useClient();
const [sort, setSort] = useFeedSort("posts", undefined, "TopAll");
const [sort, setSort] = useFeedSort(
"posts",
{ internal: search ? "CommunitiesSearch" : "CommunitiesExplore" },
"TopAll",
);
const [listingType, setListingType] = useState<ListingType>("All");

const fetchFn: FetchFn<CommunityView> = useCallback(
Expand Down
4 changes: 3 additions & 1 deletion src/routes/pages/search/results/SearchFeedResultsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export default function SearchFeedResultsPage({
}>();
const buildGeneralBrowseLink = useBuildGeneralBrowseLink();
const client = useClient();
const [sort, setSort] = useFeedSort("posts");
const [sort, setSort] = useFeedSort("posts", {
internal: `${type}Search`,
});

const search = decodeURIComponent(_encodedSearch);

Expand Down

0 comments on commit b8f305c

Please sign in to comment.