Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Fix: solution sorting loses state, using local storage to persist it (#1377)

Fahadcbbd26663531
diff --git a/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/index.tsx b/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/index.tsx
index bd96b7f4..587cd8c9 100644
--- a/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/index.tsx
+++ b/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/index.tsx
@@ -18,6 +18,7 @@ import { SolutionsSkeleton } from './solution-skeleton';
import { SortSelect } from '../../../_components/sort-select';
import { useGetQueryString } from './useGetQueryString';
import { useQueryParamState } from './useQueryParamState';
+import { useLocalStorage } from '~/utils/useLocalStorage';
interface Props {
slug: string;
@@ -49,7 +50,10 @@ type View = 'details' | 'editor' | 'list';
export function Solutions({ slug }: Props) {
const [view, setView] = useState<View>('list');
const commentContainerRef = useRef<HTMLDivElement>(null);
- const [sortKey, setSortKey] = useState<(typeof SORT_KEYS)[number]>(SORT_KEYS[0]);
+ const [storageKey, setStorageKey] = useLocalStorage('sortKey', 'newest');
+ const [sortKey, setSortKey] = useState<(typeof SORT_KEYS)[number]>(
+ SORT_KEYS.find((sk) => sk.value === storageKey) ?? SORT_KEYS[0],
+ );
const [page, setPage] = useQueryParamState<number>('page', 1);
const queryKey = ['challenge-solutions', slug, page, sortKey.key, sortKey.order];
const session = useSession();
@@ -64,6 +68,7 @@ export function Solutions({ slug }: Props) {
const handleValueChange = (value: string) => {
setSortKey(SORT_KEYS.find((sk) => sk.value === value) ?? SORT_KEYS[0]);
+ setStorageKey(value);
setPage(1);
};