Skip to content

Commit

Permalink
readd loading indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
sebald committed Nov 1, 2023
1 parent 263791b commit 4760056
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
5 changes: 3 additions & 2 deletions app/(stats)/compositions/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { SortParam } from '@/ui/params/sort-param';
import { Compositions } from './compositions';
import { Suspense } from 'react';
import { toDateRange } from '@/lib/utils/params.utils';
import { Filter } from '@/ui/params/filter';

// Metadata
// ---------------
Expand Down Expand Up @@ -143,12 +144,12 @@ const CompositionsPage = async ({ searchParams }: PageProps) => {
</Inline>
</Caption>
</div>
<Inline className="gap-2 pb-8 sm:gap-4" align="end">
<Filter>
<SmallSamplesFilter />
<DateRangeFilter />
<FactionFilter />
<SortParam />
</Inline>
</Filter>
<Suspense fallback={<CardTableSkeleton />}>
<div className="grid grid-cols-1 gap-4 md:grid-cols-12">
<div className="col-span-full">
Expand Down
21 changes: 21 additions & 0 deletions ui/params/filter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use client';

import type { ReactNode } from 'react';

import { Inline } from '../inline';
import { Spinner } from '../spinner';
import { ParamsProvier, useParamsContext } from './useParams';

const Loading = () => {
const { pending } = useParamsContext();
return pending ? <Spinner className="h-4 w-4" /> : null;
};

export const Filter = ({ children }: { children?: ReactNode }) => (
<Inline className="gap-2 pb-8 sm:gap-4" align="end">
<ParamsProvier>
<Loading />
{children}
</ParamsProvier>
</Inline>
);
28 changes: 27 additions & 1 deletion ui/params/useParams.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
'use client';

import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import { createContext, useContext, useTransition } from 'react';
import type { ReactNode, TransitionStartFunction } from 'react';

// Context
// ---------------
const Context = createContext<{
pending: boolean;
startTransition: TransitionStartFunction;
}>({} as any);

export const useParamsContext = () => useContext(Context);

// Provider
// ---------------
export const ParamsProvier = ({ children }: { children?: ReactNode }) => {
const [pending, startTransition] = useTransition();

return (
<Context.Provider value={{ pending, startTransition }}>
{children}
</Context.Provider>
);
};

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

const params = {} as { [param in Params]: string | null };
keys.forEach(key => {
Expand All @@ -28,7 +52,9 @@ export const useParams = <Params extends string>(keys: Params[]) => {
const ps = sp.toString();
const queryString = `${ps.length ? '?' : ''}${ps}`;

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

return [params, setParams] as const;
Expand Down

0 comments on commit 4760056

Please sign in to comment.