diff --git a/packages/sdk/packages/embed/package.json b/packages/sdk/packages/embed/package.json index 0feb1926c8..3e04740bdf 100644 --- a/packages/sdk/packages/embed/package.json +++ b/packages/sdk/packages/embed/package.json @@ -49,6 +49,7 @@ "@xylabs/delay": "^4.0.10", "@xylabs/react-async-effect": "^4.2.10", "@xylabs/react-flexbox": "^4.2.10", + "@xylabs/react-hooks": "^4.2.10", "@xylabs/react-select": "^4.2.10", "@xylabs/react-shared": "^4.2.10", "@xyo-network/huri": "^3.1.9", @@ -80,4 +81,4 @@ "access": "public" }, "docs": "dist/docs.json" -} +} \ No newline at end of file diff --git a/packages/sdk/packages/embed/src/contexts/EmbedPluginContext/Provider.tsx b/packages/sdk/packages/embed/src/contexts/EmbedPluginContext/Provider.tsx index f1b79f3bfe..acaa117bfe 100644 --- a/packages/sdk/packages/embed/src/contexts/EmbedPluginContext/Provider.tsx +++ b/packages/sdk/packages/embed/src/contexts/EmbedPluginContext/Provider.tsx @@ -1,6 +1,6 @@ +import { useResetState } from '@xylabs/react-hooks' import type { WithChildren } from '@xylabs/react-shared' -import type { PayloadRenderPlugin } from '@xyo-network/react-payload-plugin' -import React, { useEffect, useState } from 'react' +import React from 'react' import { EmbedPluginContext } from './Context.ts' import type { EmbedPluginBase } from './State.ts' @@ -16,11 +16,7 @@ export const EmbedPluginProvider: React.FC { - const [activePlugin, setActivePlugin] = useState(plugins ? plugins[0] : undefined) - - useEffect(() => { - setActivePlugin(plugins ? plugins[0] : undefined) - }, [plugins]) + const [activePlugin, setActivePlugin] = useResetState(plugins ? plugins[0] : undefined) return ( = ({ // Allow external components to control the map setMapBoxInstance?.(map) + // eslint-disable-next-line @eslint-react/hooks-extra/no-direct-set-state-in-use-effect setMap(map) // save the map canvas ref to help with resizing diff --git a/packages/sdk/packages/map/src/Components/MapBoxPoints.tsx b/packages/sdk/packages/map/src/Components/MapBoxPoints.tsx index 846302a62c..4ff6794240 100644 --- a/packages/sdk/packages/map/src/Components/MapBoxPoints.tsx +++ b/packages/sdk/packages/map/src/Components/MapBoxPoints.tsx @@ -3,7 +3,7 @@ import { FlexCol } from '@xylabs/react-flexbox' import type { Feature, Point } from 'geojson' import type { MapOptions } from 'mapbox-gl' import React, { - useCallback, useEffect, useState, + useCallback, useEffect, useMemo, } from 'react' import { useMapBoxInstance, useMapSettings } from '../Contexts/index.ts' @@ -25,7 +25,6 @@ export const MapboxPointsFlexBox: React.FC = ({ zoom, ...props }) => { - const [mapPoints, setMapPoints] = useState() const { mapSettings } = useMapSettings() const { map, mapInitialized } = useMapBoxInstance() @@ -40,6 +39,14 @@ export const MapboxPointsFlexBox: React.FC = ({ return {} } + const mapPoints = useMemo(() => { + return (map && features?.length) + ? new MapPoints({ + features, map, zoom, + }) + : undefined + }, [map, features, zoom]) + const updateFeatures = useCallback(() => { if (mapPoints?.isMapReady && features?.length && layers) for (const layer of layers) { @@ -70,14 +77,6 @@ export const MapboxPointsFlexBox: React.FC = ({ updateFeatures() }, [mapPoints, fitToPointsPadding, updateFeatures, zoom]) - useEffect(() => { - if (map && features?.length) { - setMapPoints(new MapPoints({ - features, map, zoom, - })) - } - }, [map, features, zoom]) - useEffect(() => { if (mapInitialized) { updateMapSetup() diff --git a/packages/sdk/packages/map/src/Contexts/HeatMapInitializer/Provider.tsx b/packages/sdk/packages/map/src/Contexts/HeatMapInitializer/Provider.tsx index 6f9d5621e9..7aab424f57 100644 --- a/packages/sdk/packages/map/src/Contexts/HeatMapInitializer/Provider.tsx +++ b/packages/sdk/packages/map/src/Contexts/HeatMapInitializer/Provider.tsx @@ -1,9 +1,7 @@ import { forget } from '@xylabs/forget' import type { WithChildren } from '@xylabs/react-shared' import type { Feature, Polygon } from 'geojson' -import React, { - useEffect, useMemo, useState, -} from 'react' +import React, { useEffect, useMemo } from 'react' import type { AnimatedHeatMapColorProps, HeatMapColorProps } from '../../Colors/index.ts' import { useDynamicPositioning } from '../../hooks/index.ts' @@ -34,11 +32,18 @@ export const HeatMapInitializerProvider: React.FC { - const [mapHeat, setMapHeat] = useState() const { options } = useDynamicPositioning() const { mapSettings } = useMapSettings() const { map, mapInitialized } = useMapBoxInstance() + const mapHeat = useMemo(() => { + return (map && features?.length) + ? new MapHeat({ + features, map, zoom, + }) + : undefined + }, [map, features, zoom]) + const value: HeatMapInitializerState = useMemo(() => ({ MapHeat: mapHeat, heatMapColorProps, @@ -76,7 +81,7 @@ export const HeatMapInitializerProvider: React.FC { - if (map && features?.length) { - // Every time we get a new map or features, we make a new class - setMapHeat(new MapHeat({ - features, map, zoom, - })) - } - }, [map, features, zoom]) - return {children} } diff --git a/packages/sdk/packages/map/src/hooks/useDynamicMapResize.tsx b/packages/sdk/packages/map/src/hooks/useDynamicMapResize.tsx index 61e7138828..6c223502c6 100644 --- a/packages/sdk/packages/map/src/hooks/useDynamicMapResize.tsx +++ b/packages/sdk/packages/map/src/hooks/useDynamicMapResize.tsx @@ -1,8 +1,6 @@ import type { Map } from 'mapbox-gl' import type { MutableRefObject } from 'react' -import { - useEffect, useMemo, useState, -} from 'react' +import { useEffect, useMemo } from 'react' /** * Inspired by - https://bl.ocks.org/danswick/fc56f37c10d40be62e4feac5984250d2 @@ -13,8 +11,6 @@ export const useDynamicMapResize = ( mapInstance?: Map, active = true, ) => { - const [dependenciesReady, setDependenciesReady] = useState(false) - const resizer = useMemo( () => new ResizeObserver(() => { @@ -29,9 +25,8 @@ export const useDynamicMapResize = ( [mapCanvasRef, mapContainerRef, mapInstance], ) - useEffect(() => { - const dependenciesReady = !!(active && mapInstance && mapContainerRef?.current && mapCanvasRef.current) - setDependenciesReady(dependenciesReady) + const dependenciesReady = useMemo(() => { + return !!(active && mapInstance && mapContainerRef?.current && mapCanvasRef.current) }, [active, mapCanvasRef, mapContainerRef, mapInstance]) useEffect(() => { diff --git a/packages/sdk/packages/map/src/hooks/useDynamicPositioning.tsx b/packages/sdk/packages/map/src/hooks/useDynamicPositioning.tsx index 97abedb247..85bc182f3d 100644 --- a/packages/sdk/packages/map/src/hooks/useDynamicPositioning.tsx +++ b/packages/sdk/packages/map/src/hooks/useDynamicPositioning.tsx @@ -1,6 +1,6 @@ import { useWindowSize } from '@xylabs/react-shared' import type { MapOptions } from 'mapbox-gl' -import { useEffect, useState } from 'react' +import { useMemo } from 'react' /** * Zoom level for the map @@ -49,17 +49,16 @@ const linearInterpolate = (aspectRatio: number, degreeRange: number[], aspectRat } const useDynamicPositioning = () => { - const [options, setOptions] = useState>({}) const { width, height } = useWindowSize() - useEffect(() => { + const options = useMemo(() => { if (width && height) { const aspectRatio = width / height - setOptions({ + return { center: [linearInterpolate(aspectRatio, lngRange), linearInterpolate(aspectRatio, latRange)], zoom: defaultZoom, - }) + } as Partial } }, [height, width]) diff --git a/packages/sdk/packages/map/src/hooks/useQuadKeyPayloadsToFeatures.tsx b/packages/sdk/packages/map/src/hooks/useQuadKeyPayloadsToFeatures.tsx index 1c18b4c28c..b38b127523 100644 --- a/packages/sdk/packages/map/src/hooks/useQuadKeyPayloadsToFeatures.tsx +++ b/packages/sdk/packages/map/src/hooks/useQuadKeyPayloadsToFeatures.tsx @@ -1,7 +1,7 @@ import { exists } from '@xylabs/exists' import { GeoJson } from '@xyo-network/sdk-geo' import type { Feature, Geometry } from 'geojson' -import { useEffect, useState } from 'react' +import { useMemo, useState } from 'react' import type { NetworkLocationHeatmapQuadkeyAnswerPayload } from '../types/index.ts' @@ -26,7 +26,7 @@ const useQuadKeyPayloadsToFeatures = (payloads?: NetworkLocationHeatmapQuadkeyAn const [features, setFeatures] = useState[]>([]) const [error, setError] = useState() - useEffect(() => { + useMemo(() => { // Convert Multiple Payloads from Quadkey to GeoJson if (Array.isArray(payloads)) { if ((payloads)?.filter(exists).length > 0) { diff --git a/packages/sdk/packages/network/src/components/SelectEx/NetworkSelectEx.stories.tsx b/packages/sdk/packages/network/src/components/SelectEx/NetworkSelectEx.stories.tsx index ca44af5860..cfaf8f0998 100644 --- a/packages/sdk/packages/network/src/components/SelectEx/NetworkSelectEx.stories.tsx +++ b/packages/sdk/packages/network/src/components/SelectEx/NetworkSelectEx.stories.tsx @@ -1,7 +1,9 @@ import { Card, CardContent } from '@mui/material' import type { Meta, StoryFn } from '@storybook/react' import { FlexCol } from '@xylabs/react-flexbox' -import React, { useEffect, useState } from 'react' +import React, { + useEffect, useMemo, useState, +} from 'react' import { BrowserRouter } from 'react-router-dom' import { @@ -59,7 +61,7 @@ const TemplateWithRouteProviderInner: StoryFn = (props) const { network } = useNetwork() const [uris, setUris] = useState<(string | undefined)[]>([]) - useEffect(() => { + useMemo(() => { setUris(previous => [...previous, network?.nodes?.find(node => node.type === 'archivist')?.uri]) }, [network?.nodes]) diff --git a/packages/sdk/packages/network/src/contexts/Provider/Route.tsx b/packages/sdk/packages/network/src/contexts/Provider/Route.tsx index 7919a8f018..155092b58d 100644 --- a/packages/sdk/packages/network/src/contexts/Provider/Route.tsx +++ b/packages/sdk/packages/network/src/contexts/Provider/Route.tsx @@ -1,8 +1,6 @@ import type { WithChildren } from '@xylabs/react-shared' import type { NetworkPayload } from '@xyo-network/network' -import React, { - useCallback, useEffect, useState, -} from 'react' +import React, { useCallback, useMemo } from 'react' import { useSearchParams } from 'react-router-dom' import { defaultNetworkConfigs, findNetworkConfig } from '../../lib/index.ts' @@ -12,7 +10,6 @@ import { NetworkMemoryProvider } from './Memory.tsx' import type { NetworkProviderProps } from './Props.ts' const NetworkRouteProviderInner: React.FC = ({ children }) => { - const [initialized, setInitialized] = useState(false) const { network, setNetwork } = useNetwork() const [params, setParams] = useSearchParams() @@ -44,7 +41,7 @@ const NetworkRouteProviderInner: React.FC = ({ children }) => { ) // sync memory and route storage of network - useEffect(() => { + const initialized = useMemo(() => { if (routeNetwork !== network) { if (routeNetwork === undefined && network !== undefined) { // if the route does not have a network selected, use what is in the memory context @@ -54,7 +51,7 @@ const NetworkRouteProviderInner: React.FC = ({ children }) => { setNetwork?.(routeNetwork) } } - setInitialized(true) + return true }, [routeNetwork, network, setNetworkParam, setNetwork]) return ( diff --git a/packages/sdk/packages/payload/packages/context/src/contexts/Payload/Provider.tsx b/packages/sdk/packages/payload/packages/context/src/contexts/Payload/Provider.tsx index 17e76f95a6..150799fa01 100644 --- a/packages/sdk/packages/payload/packages/context/src/contexts/Payload/Provider.tsx +++ b/packages/sdk/packages/payload/packages/context/src/contexts/Payload/Provider.tsx @@ -3,7 +3,9 @@ import { useAsyncEffect } from '@xylabs/react-async-effect' import type { WithChildren } from '@xylabs/react-shared' import type { ArchivistInstance, ArchivistModule } from '@xyo-network/archivist-model' import type { Payload } from '@xyo-network/payload-model' -import React, { useEffect, useState } from 'react' +import React, { + useCallback, useEffect, useState, +} from 'react' import { PayloadContext } from './Context.ts' @@ -28,9 +30,10 @@ export const PayloadProvider: React.FC> = ({ const [payload, setPayload] = useState() const [payloadError, setPayloadError] = useState() - const refreshPayload = () => { + const refreshPayload = useCallback(() => { + // eslint-disable-next-line @eslint-react/hooks-extra/no-direct-set-state-in-use-effect setPayload(undefined) - } + }, [setPayload]) const clearPayload = () => { setPayload(null) diff --git a/packages/sdk/packages/payload/packages/plugin-resolver/package.json b/packages/sdk/packages/payload/packages/plugin-resolver/package.json index 8d679c5469..b1efeb06bb 100644 --- a/packages/sdk/packages/payload/packages/plugin-resolver/package.json +++ b/packages/sdk/packages/payload/packages/plugin-resolver/package.json @@ -44,6 +44,7 @@ }, "dependencies": { "@xylabs/assert": "^4.0.10", + "@xylabs/react-hooks": "^4.2.10", "@xyo-network/payload-model": "^3.1.9", "@xyo-network/payload-plugin": "^3.1.9", "@xyo-network/payloadset-plugin": "^3.1.9", @@ -74,4 +75,4 @@ "access": "public" }, "docs": "dist/docs.json" -} +} \ No newline at end of file diff --git a/packages/sdk/packages/payload/packages/plugin-resolver/src/contexts/PayloadSetPluginResolver/Provider.tsx b/packages/sdk/packages/payload/packages/plugin-resolver/src/contexts/PayloadSetPluginResolver/Provider.tsx index 28624713f5..18dbcd3d9d 100644 --- a/packages/sdk/packages/payload/packages/plugin-resolver/src/contexts/PayloadSetPluginResolver/Provider.tsx +++ b/packages/sdk/packages/payload/packages/plugin-resolver/src/contexts/PayloadSetPluginResolver/Provider.tsx @@ -1,6 +1,7 @@ +import { useResetState } from '@xylabs/react-hooks' import type { PayloadSetPluginResolver } from '@xyo-network/payloadset-plugin' import type { ContextExProviderProps } from '@xyo-network/react-shared' -import React, { useEffect, useState } from 'react' +import React from 'react' import { PayloadSetPluginResolverContext } from './Context.ts' @@ -13,11 +14,7 @@ export const PayloadSetPluginResolverProvider: React.FC { - const [resolver, setResolver] = useState(resolverProp) - - useEffect(() => { - setResolver(resolverProp) - }, [resolverProp]) + const [resolver, setResolver] = useResetState(resolverProp) return ( = ({ value, ...props }) => { const theme = useTheme() - const [parentHeight, setParentHeight] = useState() const ref = useRef(null) - useEffect(() => { - setParentHeight(ref.current?.parentElement?.parentElement?.clientHeight) - }, []) + const parentHeight = useMemo(() => ref.current?.parentElement?.parentElement?.clientHeight, [ref.current]) const calculatedHeight = parentHeight ?? 0 diff --git a/packages/sdk/packages/schema/package.json b/packages/sdk/packages/schema/package.json index 7ad597e4e0..c6c4629490 100644 --- a/packages/sdk/packages/schema/package.json +++ b/packages/sdk/packages/schema/package.json @@ -47,6 +47,7 @@ "@xylabs/hex": "^4.0.10", "@xylabs/react-async-effect": "^4.2.10", "@xylabs/react-link": "^4.2.10", + "@xylabs/react-promise": "^4.2.10", "@xylabs/react-select": "^4.2.10", "@xylabs/react-shared": "^4.2.10", "@xyo-network/diviner-schema-list-model": "^3.1.9", @@ -70,6 +71,7 @@ "@mui/styles": "^5.16.7", "@storybook/react": "^8.2.9", "@xylabs/react-flexbox": "^4.2.10", + "@xylabs/react-hooks": "^4.2.10", "@xylabs/ts-scripts-yarn3": "^4.0.7", "@xylabs/tsconfig-react": "^4.0.7", "@xyo-network/bridge-http": "^3.1.9", diff --git a/packages/sdk/packages/schema/src/hooks/useGetSchema.stories.tsx b/packages/sdk/packages/schema/src/hooks/useGetSchema.stories.tsx index 61a885603e..b55bbdcc2d 100644 --- a/packages/sdk/packages/schema/src/hooks/useGetSchema.stories.tsx +++ b/packages/sdk/packages/schema/src/hooks/useGetSchema.stories.tsx @@ -3,9 +3,10 @@ import { } from '@mui/material' import type { Meta, StoryFn } from '@storybook/react' import { FlexRow } from '@xylabs/react-flexbox' +import { useResetState } from '@xylabs/react-hooks' import { JsonViewerEx } from '@xyo-network/react-payload-raw-info' import { SchemaCache } from '@xyo-network/schema-cache' -import React, { useEffect, useState } from 'react' +import React from 'react' import { useGetSchemaPayload } from './useGetSchema.tsx' @@ -13,15 +14,9 @@ SchemaCache.instance.proxy = 'https://beta.api.archivist.xyo.network/domain' const UseGetSchemaComponent: React.FC<{ schema: string }> = ({ schema }) => { const exampleSchemas = ['network.xyo.domain', 'network.xyo.payload', 'network.xyo.schema'] - const [schemaFieldValue, setSchemaFieldValue] = useState('') + const [schemaFieldValue, setSchemaFieldValue] = useResetState(schema) const { schemaPayload } = useGetSchemaPayload(schemaFieldValue) - useEffect(() => { - if (schema) { - setSchemaFieldValue(schema) - } - }, [schema]) - return ( <> diff --git a/packages/sdk/packages/schema/src/hooks/useSchemaList.tsx b/packages/sdk/packages/schema/src/hooks/useSchemaList.tsx index e40ca456c5..c23dcb7de5 100644 --- a/packages/sdk/packages/schema/src/hooks/useSchemaList.tsx +++ b/packages/sdk/packages/schema/src/hooks/useSchemaList.tsx @@ -1,16 +1,12 @@ import type { Address } from '@xylabs/hex' -import { useAsyncEffect } from '@xylabs/react-async-effect' +import { usePromise } from '@xylabs/react-promise' import type { SchemaListPayload, SchemaListQueryPayload } from '@xyo-network/diviner-schema-list-model' import { SchemaListQuerySchema } from '@xyo-network/diviner-schema-list-model' import type { WithMeta } from '@xyo-network/payload-model' import { useWeakDivinerFromNode } from '@xyo-network/react-diviner' -import { - useEffect, useMemo, useState, -} from 'react' +import { useMemo } from 'react' export const useSchemaList = (address?: Address, nameOrAddress = 'SchemaListDiviner'): [SchemaListPayload | null | undefined, Error | undefined] => { - const [schemaList, setSchemaList] = useState() - const [error, setError] = useState() const [diviner, divinerError] = useWeakDivinerFromNode(nameOrAddress) const query: SchemaListQueryPayload[] | undefined = useMemo( @@ -26,27 +22,12 @@ export const useSchemaList = (address?: Address, nameOrAddress = 'SchemaListDivi [address], ) - useEffect(() => { - if (diviner === null) { - setSchemaList(null) - setError(undefined) - } - }, [diviner]) - - useAsyncEffect( - async (mounted) => { + const [schemaList, error] = usePromise( + async () => { const divinerInstance = diviner?.deref() if (divinerInstance) { - try { - const response = (await divinerInstance.divine(query)) as WithMeta[] - if (mounted()) { - setSchemaList(response?.[0]) - setError(undefined) - } - } catch (e) { - setError(e as Error) - setSchemaList(undefined) - } + const response = (await divinerInstance.divine(query)) as WithMeta[] + return response.at(0) } }, [diviner, divinerError, query], diff --git a/packages/sdk/packages/shared/package.json b/packages/sdk/packages/shared/package.json index cbee1f03cf..2879525160 100644 --- a/packages/sdk/packages/shared/package.json +++ b/packages/sdk/packages/shared/package.json @@ -47,6 +47,7 @@ "@xylabs/react-appbar": "^4.2.10", "@xylabs/react-button": "^4.2.10", "@xylabs/react-flexbox": "^4.2.10", + "@xylabs/react-hooks": "^4.2.10", "@xylabs/react-link": "^4.2.10", "@xylabs/react-promise": "^4.2.10", "@xylabs/react-shared": "^4.2.10", diff --git a/packages/sdk/packages/shared/src/contexts/diviner/Provider.tsx b/packages/sdk/packages/shared/src/contexts/diviner/Provider.tsx index 88badf3260..3a7cd76669 100644 --- a/packages/sdk/packages/shared/src/contexts/diviner/Provider.tsx +++ b/packages/sdk/packages/shared/src/contexts/diviner/Provider.tsx @@ -1,8 +1,7 @@ +import { useResetState } from '@xylabs/react-hooks' import type { WithChildren } from '@xylabs/react-shared' import type { Context } from 'react' -import React, { - useEffect, useMemo, useState, -} from 'react' +import React, { useMemo } from 'react' import type { ContextExProviderProps } from '../contextEx/index.ts' import type { ResolvedDivinerState } from './State.ts' @@ -16,13 +15,7 @@ export interface DivinerProviderProps extends ContextExProviderProps, WithChi export const ResolvedDivinerProvider = ({ diviner: divinerProp, required = false, children, context, }: DivinerProviderProps) => { - const [diviner, setDiviner] = useState(divinerProp) - - useEffect(() => { - if (divinerProp) { - setDiviner(divinerProp) - } - }, [divinerProp, setDiviner]) + const [diviner, setDiviner] = useResetState(divinerProp) const value = useMemo(() => { const resolveDiviner = () => { diff --git a/packages/sdk/packages/standard-node/package.json b/packages/sdk/packages/standard-node/package.json index d305de0150..68fe657d8d 100644 --- a/packages/sdk/packages/standard-node/package.json +++ b/packages/sdk/packages/standard-node/package.json @@ -45,6 +45,7 @@ "dependencies": { "@xylabs/hex": "^4.0.10", "@xylabs/react-async-effect": "^4.2.10", + "@xylabs/react-hooks": "^4.2.10", "@xylabs/react-promise": "^4.2.10", "@xylabs/react-shared": "^4.2.10", "@xyo-network/account-model": "^3.1.9", diff --git a/packages/sdk/packages/standard-node/src/contexts/StandardRemoteNodes/Providers.tsx b/packages/sdk/packages/standard-node/src/contexts/StandardRemoteNodes/Providers.tsx index a1299828b8..f6fc38dcf2 100644 --- a/packages/sdk/packages/standard-node/src/contexts/StandardRemoteNodes/Providers.tsx +++ b/packages/sdk/packages/standard-node/src/contexts/StandardRemoteNodes/Providers.tsx @@ -1,12 +1,11 @@ +import { useResetState } from '@xylabs/react-hooks' import { usePromise } from '@xylabs/react-promise' import type { WithChildren } from '@xylabs/react-shared' import type { MemoryNode } from '@xyo-network/node-memory' import { assertDefinedEx } from '@xyo-network/react-shared' import { useWalletContext } from '@xyo-network/react-wallet' import type { WalletInstance } from '@xyo-network/wallet-model' -import React, { - useEffect, useMemo, useState, -} from 'react' +import React, { useMemo } from 'react' import { BuildStandardNodes } from '../../lib/index.ts' import { StandardNodesContext } from './Context.ts' @@ -20,11 +19,7 @@ export interface StandardNodesProviderProps extends WithChildren { export const StandardNodesProvider: React.FC = ({ children, defaultRemoteNodes, wallet, }) => { - const [nodes, setNodes] = useState(defaultRemoteNodes) - - useEffect(() => { - setNodes(defaultRemoteNodes) - }, [defaultRemoteNodes]) + const [nodes, setNodes] = useResetState(defaultRemoteNodes) usePromise(async () => { if (wallet) { diff --git a/packages/sdk/packages/table/package.json b/packages/sdk/packages/table/package.json index eb17303dbd..dc170b83d1 100644 --- a/packages/sdk/packages/table/package.json +++ b/packages/sdk/packages/table/package.json @@ -43,6 +43,7 @@ "lint-pkg": "npmPkgJsonLint ." }, "dependencies": { + "@xylabs/react-hooks": "^4.2.10", "@xylabs/react-shared": "^4.2.10", "@xyo-network/react-shared": "workspace:^" }, diff --git a/packages/sdk/packages/table/src/context/TableHeight/Provider.tsx b/packages/sdk/packages/table/src/context/TableHeight/Provider.tsx index 48f35b3a5a..8ee24eae13 100644 --- a/packages/sdk/packages/table/src/context/TableHeight/Provider.tsx +++ b/packages/sdk/packages/table/src/context/TableHeight/Provider.tsx @@ -1,8 +1,7 @@ +import { useResetState } from '@xylabs/react-hooks' import type { WithChildren } from '@xylabs/react-shared' import type { ContextExProviderProps } from '@xyo-network/react-shared' -import React, { - useEffect, useMemo, useState, -} from 'react' +import React, { useMemo, useState } from 'react' import { TableHeightContext } from './Context.ts' @@ -19,22 +18,17 @@ export const TableHeightProvider: React.FC = ({ defaultVisibleRows, heightFormat = 'px', }) => { - const [visibleRows, setVisibleRows] = useState(defaultVisibleRows) - const [height, setHeight] = useState() + const [visibleRows, setVisibleRows] = useResetState(defaultVisibleRows) const [rowHeight, setRowHeight] = useState() - const formattedHeight = useMemo(() => (height === undefined ? undefined : `${height}${heightFormat}`), [height, heightFormat]) - - useEffect(() => { - setVisibleRows(defaultVisibleRows) - }, [defaultVisibleRows]) - - useEffect(() => { + const height = useMemo(() => { if (rowHeight !== undefined && visibleRows !== undefined) { - setHeight(rowHeight * (visibleRows + additionalRows)) + return rowHeight * (visibleRows + additionalRows) } }, [defaultVisibleRows, rowHeight, visibleRows, additionalRows]) + const formattedHeight = useMemo(() => (height === undefined ? undefined : `${height}${heightFormat}`), [height, heightFormat]) + const value = useMemo(() => ({ height: formattedHeight, provided: true, rowHeight, setRowHeight, setVisibleRows, visibleRows, }), [formattedHeight, rowHeight, setRowHeight, setVisibleRows, visibleRows]) diff --git a/packages/sdk/packages/wallet/package.json b/packages/sdk/packages/wallet/package.json index 4f49369bd2..6b61be2f1d 100644 --- a/packages/sdk/packages/wallet/package.json +++ b/packages/sdk/packages/wallet/package.json @@ -48,6 +48,7 @@ "@xylabs/react-button": "^4.2.10", "@xylabs/react-crypto": "^4.2.10", "@xylabs/react-flexbox": "^4.2.10", + "@xylabs/react-hooks": "^4.2.10", "@xylabs/react-identicon": "^4.2.10", "@xylabs/react-number-status": "^4.2.10", "@xylabs/react-promise": "^4.2.10", diff --git a/packages/sdk/packages/wallet/src/components/SeedPhrase/DefaultSeedPhrase.tsx b/packages/sdk/packages/wallet/src/components/SeedPhrase/DefaultSeedPhrase.tsx index a57e4ddba5..9e0ced3420 100644 --- a/packages/sdk/packages/wallet/src/components/SeedPhrase/DefaultSeedPhrase.tsx +++ b/packages/sdk/packages/wallet/src/components/SeedPhrase/DefaultSeedPhrase.tsx @@ -5,7 +5,7 @@ import { generateMnemonic } from '@scure/bip39' // eslint-disable-next-line import-x/no-internal-modules import { wordlist } from '@scure/bip39/wordlists/english' import type { WithChildren } from '@xylabs/react-shared' -import React, { useEffect, useState } from 'react' +import React, { useMemo, useState } from 'react' export interface DefaultSeedPhraseProps extends WithChildren { changeSeedPhrase?: (seedPhrase: string) => void @@ -18,7 +18,7 @@ export const DefaultSeedPhrase: React.FC = ({ }) => { const [showSnackBar, setShowSnackBar] = useState(false) - useEffect(() => { + useMemo(() => { if (!seedPhrase) { const mnemonic = generateMnemonic(wordlist, 256) changeSeedPhrase?.(mnemonic) diff --git a/packages/sdk/packages/wallet/src/contexts/SeedPhrase/Provider.tsx b/packages/sdk/packages/wallet/src/contexts/SeedPhrase/Provider.tsx index d7e8bfa0d9..4229eae5d7 100644 --- a/packages/sdk/packages/wallet/src/contexts/SeedPhrase/Provider.tsx +++ b/packages/sdk/packages/wallet/src/contexts/SeedPhrase/Provider.tsx @@ -1,6 +1,7 @@ import { generateMnemonic, validateMnemonic } from '@scure/bip39' // eslint-disable-next-line import-x/no-internal-modules import { wordlist } from '@scure/bip39/wordlists/english' +import { useResetState } from '@xylabs/react-hooks' import type { WithChildren } from '@xylabs/react-shared' import React, { useCallback, useEffect, useMemo, useState, @@ -31,13 +32,9 @@ export const SeedPhraseProvider: React.FC = ({ seedPhrase, open, }) => { - const [phrase, setPhrase] = useState() + const [phrase, setPhrase] = useResetState(defaultPhrase) const [overwriteWarning, setOverwriteWarning] = useState(false) - useEffect(() => { - setPhrase(defaultPhrase) - }, [defaultPhrase]) - useEffect(() => { if (seedPhrase || open) { setPhrase?.(seedPhrase ?? '') diff --git a/packages/sdk/packages/wallet/src/contexts/Wallet/Provider.tsx b/packages/sdk/packages/wallet/src/contexts/Wallet/Provider.tsx index 6b9f587cc4..16676699e6 100644 --- a/packages/sdk/packages/wallet/src/contexts/Wallet/Provider.tsx +++ b/packages/sdk/packages/wallet/src/contexts/Wallet/Provider.tsx @@ -1,9 +1,8 @@ +import { useResetState } from '@xylabs/react-hooks' import { usePromise } from '@xylabs/react-promise' import type { WithChildren } from '@xylabs/react-shared' import type { WalletInstance } from '@xyo-network/wallet-model' -import React, { - useEffect, useMemo, useState, -} from 'react' +import React, { useMemo } from 'react' import { WalletContext } from './Context.ts' @@ -18,13 +17,7 @@ export const WalletProvider: React.FC> = ({ rootWallet = null, ...props }) => { - const [activeAccountIndex, setActiveAccountIndex] = useState(defaultActiveAccountIndex) - - useEffect(() => { - if (defaultActiveAccountIndex !== undefined) { - setActiveAccountIndex(defaultActiveAccountIndex) - } - }, [defaultActiveAccountIndex]) + const [activeAccountIndex, setActiveAccountIndex] = useResetState(defaultActiveAccountIndex) const [activeAccount = null] = usePromise(async () => await rootWallet?.derivePath(activeAccountIndex.toString()), [activeAccountIndex, rootWallet]) diff --git a/yarn.lock b/yarn.lock index a6f6d501a5..a6b4962e5b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10603,7 +10603,9 @@ __metadata: "@xylabs/hex": "npm:^4.0.10" "@xylabs/react-async-effect": "npm:^4.2.10" "@xylabs/react-flexbox": "npm:^4.2.10" + "@xylabs/react-hooks": "npm:^4.2.10" "@xylabs/react-link": "npm:^4.2.10" + "@xylabs/react-promise": "npm:^4.2.10" "@xylabs/react-select": "npm:^4.2.10" "@xylabs/react-shared": "npm:^4.2.10" "@xylabs/ts-scripts-yarn3": "npm:^4.0.7" @@ -10799,6 +10801,7 @@ __metadata: "@xylabs/react-appbar": "npm:^4.2.10" "@xylabs/react-button": "npm:^4.2.10" "@xylabs/react-flexbox": "npm:^4.2.10" + "@xylabs/react-hooks": "npm:^4.2.10" "@xylabs/react-link": "npm:^4.2.10" "@xylabs/react-promise": "npm:^4.2.10" "@xylabs/react-quick-tip-button": "npm:^4.2.10" @@ -10832,6 +10835,7 @@ __metadata: "@storybook/react": "npm:^8.2.9" "@xylabs/hex": "npm:^4.0.10" "@xylabs/react-async-effect": "npm:^4.2.10" + "@xylabs/react-hooks": "npm:^4.2.10" "@xylabs/react-promise": "npm:^4.2.10" "@xylabs/react-shared": "npm:^4.2.10" "@xylabs/ts-scripts-yarn3": "npm:^4.0.7" @@ -10898,6 +10902,7 @@ __metadata: "@mui/material": "npm:^5.16.7" "@mui/styles": "npm:^5.16.7" "@storybook/react": "npm:^8.2.9" + "@xylabs/react-hooks": "npm:^4.2.10" "@xylabs/react-shared": "npm:^4.2.10" "@xylabs/ts-scripts-yarn3": "npm:^4.0.7" "@xylabs/tsconfig-react": "npm:^4.0.7" @@ -11029,6 +11034,7 @@ __metadata: "@xylabs/react-button": "npm:^4.2.10" "@xylabs/react-crypto": "npm:^4.2.10" "@xylabs/react-flexbox": "npm:^4.2.10" + "@xylabs/react-hooks": "npm:^4.2.10" "@xylabs/react-identicon": "npm:^4.2.10" "@xylabs/react-number-status": "npm:^4.2.10" "@xylabs/react-promise": "npm:^4.2.10"