Skip to content

Commit e33098f

Browse files
committed
chore(release): 0.4.0-alpha.2
1 parent ac711a7 commit e33098f

2 files changed

Lines changed: 34 additions & 22 deletions

File tree

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@inverter-network/react",
33
"description": "INVERTER react",
4-
"version": "0.4.0-alpha.1",
4+
"version": "0.4.0-alpha.2",
55
"exports": {
66
".": {
77
"types": "./dist/types/index.d.ts",
@@ -79,8 +79,8 @@
7979
},
8080
"dependencies": {
8181
"@api3/logos": "^0.2.18",
82-
"@inverter-network/sdk": "0.4.0-alpha.1",
83-
"@inverter-network/graphql": "^0.9.12",
82+
"@inverter-network/sdk": "0.4.0-alpha.2",
83+
"@inverter-network/graphql": "^0.9.15",
8484
"@radix-ui/react-accordion": "^1.2.1",
8585
"@radix-ui/react-alert-dialog": "^1.1.2",
8686
"@radix-ui/react-checkbox": "^1.1.2",

src/hooks/use-graphql-subscription.ts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,50 @@ import * as React from 'react'
44
import { subscription } from '@inverter-network/graphql'
55
import type {
66
GraphQLSubscriptionArgs,
7-
GraphQLSubscriptionResult,
7+
SubscriptionResult,
88
} from '@inverter-network/graphql'
99

1010
export type UseGraphQLSubscriptionParams<T extends GraphQLSubscriptionArgs> = {
1111
fields: T
1212
enabled?: boolean
1313
}
1414

15+
export type UseGraphQLSubscriptionResult<T extends GraphQLSubscriptionArgs> = {
16+
data: SubscriptionResult<T> | null
17+
error: string | null
18+
isLoading: boolean
19+
}
20+
1521
export const useGraphQLSubscription = <T extends GraphQLSubscriptionArgs>({
1622
fields,
1723
enabled = true,
18-
}: UseGraphQLSubscriptionParams<T>): GraphQLSubscriptionResult<T> | null => {
19-
const memo = React.useMemo(() => {
20-
return subscription(fields)
21-
}, [fields])
22-
23-
const [data, setData] = React.useState<Awaited<typeof memo> | null>(null)
24+
}: UseGraphQLSubscriptionParams<T>): UseGraphQLSubscriptionResult<T> => {
25+
const [data, setData] = React.useState<SubscriptionResult<T> | null>(null)
26+
const [error, setError] = React.useState<string | null>(null)
2427

2528
React.useEffect(() => {
26-
if (!memo || !enabled) return
27-
28-
const fetchData = async () => {
29-
try {
30-
const result = memo
31-
setData(result)
32-
} catch (error) {
33-
console.error('Error in GraphQL subscription:', error)
29+
if (!enabled) return () => {} // Return empty cleanup function
30+
31+
try {
32+
const sub = subscription(fields)
33+
let callbackId: string | null = null
34+
35+
callbackId = sub.addCallback(setData)
36+
37+
// Return cleanup function
38+
return () => {
39+
sub.removeCallback(callbackId)
3440
}
41+
} catch (error: any) {
42+
console.error(
43+
'Error subscribing to fields',
44+
error?.message ?? error?.cause ?? error
45+
)
46+
// eslint-disable-next-line @eslint-react/hooks-extra/no-direct-set-state-in-use-effect
47+
setError(error?.message ?? error?.cause ?? 'Unknown error')
48+
return () => {} // Add return for catch block
3549
}
50+
}, [JSON.stringify(fields), enabled])
3651

37-
fetchData()
38-
}, [memo, enabled])
39-
40-
return data
52+
return { data, error, isLoading: !data }
4153
}

0 commit comments

Comments
 (0)