Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript Alternative #26

Open
CodeFromAnywhere opened this issue Jan 24, 2024 · 0 comments
Open

Typescript Alternative #26

CodeFromAnywhere opened this issue Jan 24, 2024 · 0 comments

Comments

@CodeFromAnywhere
Copy link

CodeFromAnywhere commented Jan 24, 2024

Great package. I found this after I made my own. Just sharing this alternative. This alternative has typescript support and also supports complex data structures like arrays and nested objects. Relies on nextjs but can easily be replaced. Hope this helps someone!

Usage:

const useCustomUrlStore = makeComplexUrlStore<{a:string,b:{c:number}}>();

And in your component:

const YourComponent = () => {

  const [a,setA,isReady]=useCustomUrlStore("a");

  /// stuff
}

Code

import { useRouter } from "next/router";
import qs, { IParseOptions, IStringifyOptions } from "qs";

type Keys<T> = Extract<keyof T, string>;
type O = { [key:string]: any };

/**
 * Variant to the original use url store that has an easier setup and allows for more complex datastructures
 *
 * Uses https://www.npmjs.com/package/qs
 *
 * NB: the base type needs to be an object!
 */
export const makeComplexUrlStore = <T extends O>(): (<K extends Keys<T>>(
  queryKey: K,
) => [T[K], (newValue: T[K] | undefined) => Promise<boolean>, boolean]) => {
  const hookFactory = <K extends Keys<T>>(
    queryKey: K,
  ): [T[K], (newValue: T[K] | undefined) => Promise<boolean>, boolean] => {
    const router = useRouter();

    const queryString = router.asPath.split("?")[1];

    /**
     * NB: These options have quite some overlap but may not be 100%. Careful
     */
    const qsParseOptions: IParseOptions & IStringifyOptions = {
      allowDots: true,
    };

    const parsedQuery = qs.parse(queryString, qsParseOptions) as T;
    const value = parsedQuery[queryKey];

    const setter = async (newValue: T[K] | undefined) => {
      const newState = { ...parsedQuery, [queryKey]: newValue };
      const newQueryString = qs.stringify(newState, qsParseOptions);

      const pushed = await router.push(
        `${router.pathname}?${newQueryString}`,
        undefined,
        { shallow: true },
      );

      return pushed;
    };

    return [value, setter, router.isReady];
  };

  return hookFactory;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant