Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
sebald committed Oct 29, 2023
1 parent 5a0d973 commit 968a8c3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 41 deletions.
2 changes: 1 addition & 1 deletion app/(stats)/compositions/[[...range]]/compositions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { Card } from '@/ui';
import { useSmallSamplesFilter } from '@/ui/filter/small-samples-filter';
import { useSmallSamplesFilter } from '@/ui/params/small-samples-filter';
import { CompositionTable } from '@/ui/stats/composition-stats';
import type { CompositionData } from '@/lib/stats/module/composition';

Expand Down
2 changes: 1 addition & 1 deletion app/(stats)/compositions/[[...range]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { StatsFilter } from '@/ui/stats/stats-filter';
import { StatsHint } from '@/ui/stats/stats-hint';
import { setup } from '@/lib/stats';
import { CompositionData, composition } from '@/lib/stats/module';
import { SmallSamplesFilter } from '@/ui/filter/small-samples-filter';
import { SmallSamplesFilter } from '@/ui/params/small-samples-filter';
import { Suspense } from 'react';
import { Compositions } from './compositions';

Expand Down
37 changes: 0 additions & 37 deletions ui/filter/useQueryFilter.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use client';

import { Switch } from '@/ui/switch';
import { useQueryFilter } from './useQueryFilter';
import { useParams } from './useParams';

// Hook
// ---------------
export const useSmallSamplesFilter = () => {
const [filter, setFilter] = useQueryFilter(['small-samples']);
const [filter, setFilter] = useParams(['small-samples']);

const setSmallSamples = (val: boolean) =>
setFilter({
Expand Down
35 changes: 35 additions & 0 deletions ui/params/useParams.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use client';

import { usePathname, useRouter, useSearchParams } from 'next/navigation';

// Hook
// ---------------
export const useParams = <Params extends string>(keys: Params[]) => {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();

const params = {} as { [param in Params]: string | null };
keys.forEach(key => {
params[key] = searchParams.get(key) || null;
});

const setParams = (values: { [key: string]: string | null }) => {
const sp = new URLSearchParams(searchParams.toString());

Object.entries(values).forEach(([key, value]) => {
if (value) {
sp.set(key, value);
} else {
sp.delete(key);
}
});

const ps = sp.toString();
const queryString = `${ps.length ? '?' : ''}${ps}`;

router.replace(`${pathname}${queryString}`, { scroll: false });
};

return [params, setParams] as const;
};

0 comments on commit 968a8c3

Please sign in to comment.