@@ -38,13 +38,20 @@ import { VgerPostSortType, VgerPostSortTypeByMode } from "./PostSort";
3838import { VgerSearchSortType , VgerSearchSortTypeByMode } from "./SearchSort" ;
3939import { isTopSort , topSortToDuration , VgerTopSort } from "./topSorts" ;
4040
41- interface VgerSorts {
41+ interface VgerSortsByContext {
4242 posts : VgerPostSortType ;
4343 comments : VgerCommentSortType ;
4444 search : VgerSearchSortType ;
4545 communities : VgerCommunitySortType ;
4646}
4747
48+ interface VgerSortsByContextByMode {
49+ posts : VgerPostSortTypeByMode ;
50+ comments : VgerCommentSortTypeByMode ;
51+ search : VgerSearchSortTypeByMode ;
52+ communities : VgerCommunitySortTypeByMode ;
53+ }
54+
4855interface Sorts {
4956 posts : PostSortType ;
5057 comments : CommentSortType ;
@@ -57,13 +64,21 @@ export type FeedSortContext = "posts" | "comments" | "search" | "communities";
5764export default function useFeedSort < Context extends FeedSortContext > (
5865 context : Context ,
5966 feed ?: AnyFeed | undefined ,
60- overrideSort ?: VgerSorts [ Context ] ,
67+ overrideSort ?:
68+ | VgerSortsByContextByMode [ Context ]
69+ | VgerSortsByContext [ Context ] ,
6170) {
62- type Sort = VgerSorts [ Context ] ;
71+ type Sort = VgerSortsByContext [ Context ] ;
6372
6473 const dispatch = useAppDispatch ( ) ;
6574 const mode = useMode ( ) ;
6675
76+ function getOverrideSort ( mode : ThreadiverseMode ) : Sort | null | undefined {
77+ if ( typeof overrideSort === "string" ) return overrideSort ;
78+ if ( ! mode ) return undefined ;
79+ return ( overrideSort ?. [ mode ] as Sort ) ?? null ;
80+ }
81+
6782 const feedSort = useAppSelector ( getFeedSortSelectorBuilder ( feed , context ) ) as
6883 | Sort
6984 | null
@@ -77,11 +92,12 @@ export default function useFeedSort<Context extends FeedSortContext>(
7792 . rememberCommunitySort ,
7893 ) ;
7994
80- const [ sort , _setSort ] = useState < Sort | undefined > (
81- ! rememberCommunitySort
82- ? ( overrideSort ?? defaultSort )
83- : ( feedSort ?? overrideSort ) ,
84- ) ;
95+ const [ sort , _setSort ] = useState < Sort | null | undefined > ( ( ) => {
96+ if ( ! mode ) return undefined ;
97+ if ( ! rememberCommunitySort ) return getOverrideSort ( mode ) ?? defaultSort ;
98+ if ( feedSort ) return feedSort ;
99+ return getOverrideSort ( mode ) ;
100+ } ) ;
85101
86102 useEffect ( ( ) => {
87103 ( async ( ) => {
@@ -151,31 +167,46 @@ function findFeedContext(
151167 }
152168}
153169
170+ /**
171+ * @param context What kind of feed is this?
172+ * @param sort The Voyager sort to convert to threadiverse sort params
173+ * @returns The sort, null if loaded but no result. Null if still loading async.
174+ */
154175export function useFeedSortParams < Context extends FeedSortContext > (
155176 context : Context ,
156- sort : VgerSorts [ Context ] | undefined ,
157- ) : Sorts [ Context ] | undefined {
177+ sort : VgerSortsByContext [ Context ] | null | undefined ,
178+ ) : Sorts [ Context ] | null | undefined {
158179 const mode = useMode ( ) ;
159180
160- if ( ! sort || ! mode ) return ;
181+ if ( ! mode ) return undefined ; // not loaded
182+ if ( ! sort ) return null ; // loaded, but not found
161183
162- return convertSortToLemmyParams ( context , sort , mode ) ;
184+ return convertSortToLemmyParams ( context , sort , mode ) ?? null ;
163185}
164186
165187function convertSortToLemmyParams < Context extends FeedSortContext > (
166188 context : Context ,
167- sort : VgerSorts [ Context ] ,
189+ sort : VgerSortsByContext [ Context ] ,
168190 mode : ThreadiverseMode ,
169191) {
170192 switch ( context ) {
171193 case "posts" :
172- return convertPostSortToParams ( sort as VgerSorts [ "posts" ] , mode ) ;
194+ return convertPostSortToParams ( sort as VgerSortsByContext [ "posts" ] , mode ) ;
173195 case "comments" :
174- return convertCommentSortToParams ( sort as VgerSorts [ "comments" ] , mode ) ;
196+ return convertCommentSortToParams (
197+ sort as VgerSortsByContext [ "comments" ] ,
198+ mode ,
199+ ) ;
175200 case "search" :
176- return convertSearchSortToParams ( sort as VgerSearchSortType , mode ) ;
201+ return convertSearchSortToParams (
202+ sort as VgerSortsByContext [ "search" ] ,
203+ mode ,
204+ ) ;
177205 case "communities" :
178- return convertCommunitySortToParams ( sort as VgerCommunitySortType , mode ) ;
206+ return convertCommunitySortToParams (
207+ sort as VgerSortsByContext [ "communities" ] ,
208+ mode ,
209+ ) ;
179210 }
180211}
181212
0 commit comments