From f031fcac14d1cae2de6de2355524168a04193686 Mon Sep 17 00:00:00 2001 From: Thijs Daniels Date: Wed, 14 Aug 2024 01:26:58 +0200 Subject: [PATCH] chore: remove redundant changesets --- .changeset/beige-seahorses-cross.md | 5 --- .changeset/sour-berries-change.md | 5 --- .../cdk-next-app/src/constructs/NextApp.ts | 8 +++- .../react-essentials/hooks/useDelayedValue.ts | 15 +++++++ .../react-pagination/hooks/useLoadMore.ts | 44 +++++++++++++++++++ 5 files changed, 65 insertions(+), 12 deletions(-) delete mode 100644 .changeset/beige-seahorses-cross.md delete mode 100644 .changeset/sour-berries-change.md create mode 100644 packages/react-essentials/hooks/useDelayedValue.ts create mode 100644 packages/react-pagination/hooks/useLoadMore.ts diff --git a/.changeset/beige-seahorses-cross.md b/.changeset/beige-seahorses-cross.md deleted file mode 100644 index f8a2699c..00000000 --- a/.changeset/beige-seahorses-cross.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@codedazur/tsconfig": minor ---- - -Array index access can now return undefined and must therefore be checked. diff --git a/.changeset/sour-berries-change.md b/.changeset/sour-berries-change.md deleted file mode 100644 index 1e5d97cb..00000000 --- a/.changeset/sour-berries-change.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@codedazur/tsconfig": patch ---- - -The unchecked index access flag was removed, because it is too strict in many situations. diff --git a/packages/cdk-next-app/src/constructs/NextApp.ts b/packages/cdk-next-app/src/constructs/NextApp.ts index ed3f4efb..b788dc23 100644 --- a/packages/cdk-next-app/src/constructs/NextApp.ts +++ b/packages/cdk-next-app/src/constructs/NextApp.ts @@ -47,7 +47,7 @@ export class NextApp extends DockerCluster { return { directory: source, secrets: { - npmrc: DockerBuildSecret.fromSrc(path.join(os.homedir(), "/.npmrc")), + npmrc: NextApp.npmSecret(), }, }; } @@ -55,9 +55,13 @@ export class NextApp extends DockerCluster { return { ...source, secrets: { - npmrc: DockerBuildSecret.fromSrc(path.join(os.homedir(), "/.npmrc")), + npmrc: NextApp.npmSecret(), ...source.secrets, }, }; } + + protected static npmSecret() { + return DockerBuildSecret.fromSrc(path.join(os.homedir(), ".npmrc")); + } } diff --git a/packages/react-essentials/hooks/useDelayedValue.ts b/packages/react-essentials/hooks/useDelayedValue.ts new file mode 100644 index 00000000..8b7bba30 --- /dev/null +++ b/packages/react-essentials/hooks/useDelayedValue.ts @@ -0,0 +1,15 @@ +import { useEffect, useState } from "react"; + +export function useDelayedValue(value: T, delay: number) { + const [delayedValue, setDelayedValue] = useState(value); + + useEffect(() => { + const timeout = setTimeout(() => { + setDelayedValue(value); + }, delay); + + return () => clearTimeout(timeout); + }, [value, delay]); + + return delayedValue; +} diff --git a/packages/react-pagination/hooks/useLoadMore.ts b/packages/react-pagination/hooks/useLoadMore.ts new file mode 100644 index 00000000..18b83c6b --- /dev/null +++ b/packages/react-pagination/hooks/useLoadMore.ts @@ -0,0 +1,44 @@ +import { useCallback, useEffect, useMemo, useState } from "react"; + +export interface UseLoadMoreProps { + items: T[]; + itemsPerPage: number; +} + +/** + * @todo Review this code before releasing it. + * @todo Support asynchoronously fetching more items _instead_ of providing all + * items at once and using `itemsPerPage` to limit the results. These are two + * very different use cases, so evaluate if this hook should support both, or if + * we should create a new hook for the async case. + * @todo Create an additional hook that calls this hook's `loadMore` function + * when the bottom of some overflowing container is reached. The new hook should + * be called `useInfiniteScroll` and should be a thin wrapper around this hook. + * @todo Support setting the initial page. + */ +export function useLoadMore({ items, itemsPerPage }: UseLoadMoreProps) { + const [page, setPage] = useState(1); + + const [visibleItems, setVisibleItems] = useState( + items.slice(0, itemsPerPage * page), + ); + + useEffect(() => { + setVisibleItems(items.slice(0, itemsPerPage * page)); + }, [page, items, itemsPerPage]); + + const isOverflowing = useMemo( + () => items.length > page * itemsPerPage, + [page, items, itemsPerPage], + ); + + const loadMore = useCallback(() => { + setPage((page) => page + 1); + }, []); + + return { + items: visibleItems, + isOverflowing, + loadMore, + }; +}