Skip to content

Commit

Permalink
Fix DB failure causing post page to fail to load (aeharding#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored May 10, 2024
1 parent 6a9860a commit 2d46158
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/features/feed/sort/useFeedSort.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommentSortType, SortType } from "lemmy-js-client";
import { useEffect, useState } from "react";
import { useCallback, useEffect, useState } from "react";
import { useAppDispatch, useAppSelector } from "../../../store";
import {
FeedSortFeed,
Expand All @@ -19,9 +19,11 @@ export default function useSortByFeed<Context extends "posts" | "comments">(
}[Context];

const dispatch = useAppDispatch();
const feedSort = useAppSelector(
getFeedSortSelectorBuilder(feed, context),
) as Sort;

const feedSort = useAppSelector(getFeedSortSelectorBuilder(feed, context)) as
| Sort
| null
| undefined;
const defaultSort = useAppSelector(
(state) => state.settings.general[context].sort,
) as Sort;
Expand All @@ -39,7 +41,7 @@ export default function useSortByFeed<Context extends "posts" | "comments">(
if (!feed) return;

try {
await dispatch(getFeedSort({ feed, context }));
await dispatch(getFeedSort({ feed, context })).unwrap(); // unwrap to catch dispatched error (db failure)
} catch (error) {
_setSort((_sort) => _sort ?? defaultSort); // fallback if indexeddb unavailable
throw error;
Expand All @@ -55,19 +57,22 @@ export default function useSortByFeed<Context extends "posts" | "comments">(
_setSort(feedSort ?? defaultSort);
}, [feedSort, sort, defaultSort, rememberCommunitySort]);

function setSort(sort: Sort) {
if (rememberCommunitySort && feed) {
dispatch(
setFeedSort({
feed,
sort,
context,
} as SetSortActionPayload),
);
}
const setSort = useCallback(
(sort: Sort) => {
if (rememberCommunitySort && feed) {
dispatch(
setFeedSort({
feed,
sort,
context,
} as SetSortActionPayload),
);
}

return _setSort(sort);
}
return _setSort(sort);
},
[context, dispatch, feed, rememberCommunitySort],
);

return [sort, setSort] as const;
}

0 comments on commit 2d46158

Please sign in to comment.