feat: add medusa-react (#913)

This commit is contained in:
Zakaria El Asri
2021-12-14 19:09:36 +01:00
committed by GitHub
parent 40f6e88875
commit d0d8dd7bf6
97 changed files with 22822 additions and 7595 deletions

View File

@@ -0,0 +1,62 @@
import * as React from "react"
type TQueryKey<TKey, TListQuery = any, TDetailQuery = string> = {
all: [TKey]
lists: () => [...TQueryKey<TKey>["all"], "list"]
list: (
query?: TListQuery
) => [
...ReturnType<TQueryKey<TKey>["lists"]>,
{ query: TListQuery | undefined }
]
details: () => [...TQueryKey<TKey>["all"], "detail"]
detail: (
id: TDetailQuery
) => [...ReturnType<TQueryKey<TKey>["details"]>, TDetailQuery]
}
export const makeKeysFactory = <
T,
TListQueryType = any,
TDetailQueryType = string
>(
globalKey: T
) => {
const queryKeyFactory: TQueryKey<T, TListQueryType, TDetailQueryType> = {
all: [globalKey],
lists: () => [...queryKeyFactory.all, "list"],
list: (query?: TListQueryType) => [...queryKeyFactory.lists(), { query }],
details: () => [...queryKeyFactory.all, "detail"],
detail: (id: TDetailQueryType) => [...queryKeyFactory.details(), id],
}
return queryKeyFactory
}
export const useLocalStorage = (key: string, initialState: string) => {
const [item, setItem] = React.useState(() => {
try {
const item =
typeof window !== "undefined" && window.localStorage.getItem(key)
return item || initialState
} catch (err) {
return initialState
}
})
const save = (data: string) => {
setItem(data)
if (typeof window !== "undefined") {
window.localStorage.setItem(key, data)
}
}
const remove = () => {
if (typeof window !== "undefined") {
window.localStorage.removeItem(key)
}
}
return [item, save, remove] as const
}