Skip to content

Commit 51c62d6

Browse files
committed
feat: show error logs when useFlow is used outside the context of the provider
1 parent b17ae3d commit 51c62d6

File tree

7 files changed

+44
-5
lines changed

7 files changed

+44
-5
lines changed

.changeset/rude-bugs-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@frigade/react": patch
3+
---
4+
5+
Show error logs when hooks are used outside the context of the `Frigade.Provider`

apps/smithy/.storybook/preview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import { FrigadeJS, Provider } from "@frigade/react";
2+
import { FrigadeJS, Provider, useFlow } from "@frigade/react";
33

44
import "./global.css";
55

packages/react/src/hooks/useFlow.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type Flow } from '@frigade/js'
22
import { useCallback, useContext, useEffect, useState, useSyncExternalStore } from 'react'
33

44
import { FrigadeContext } from '@/components/Provider'
5+
import { logOnce } from '../shared/log'
56

67
export interface FlowConfig {
78
variables?: Record<string, unknown>
@@ -14,7 +15,11 @@ export function useFlow(
1415
flow: Flow | undefined
1516
isLoading: boolean
1617
} {
17-
const { frigade, variables } = useContext(FrigadeContext)
18+
const context = useContext(FrigadeContext)
19+
if (!context || !context.frigade) {
20+
logOnce(`useFlow('${flowId}') must be used in a child of the Frigade Provider`, 'error')
21+
}
22+
const { frigade, variables } = context ?? {}
1823
const [, setForceRender] = useState<boolean>(false)
1924

2025
const subscribe = useCallback(
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { useContext } from 'react'
22

33
import { FrigadeContext } from '@/components/Provider'
4+
import { logOnce } from '../shared/log'
45

56
export function useFrigade() {
6-
const { frigade } = useContext(FrigadeContext)
7+
const context = useContext(FrigadeContext)
8+
if (!context || !context.frigade) {
9+
logOnce('useFrigade() must be used in a child of the Frigade Provider', 'error')
10+
}
11+
12+
const { frigade } = context
713

814
return { frigade, isLoading: !frigade?.isReady() }
915
}

packages/react/src/hooks/useGroup.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { useContext } from 'react'
22

33
import { FrigadeContext } from '@/components/Provider'
4+
import { logOnce } from '../shared/log'
45

56
export function useGroup() {
6-
const { frigade } = useContext(FrigadeContext)
7+
const context = useContext(FrigadeContext)
8+
if (!context || !context.frigade) {
9+
logOnce('useGroup() must be used in a child of the Frigade Provider', 'error')
10+
}
11+
const { frigade } = context ?? {}
712

813
/**
914
* Sets properties for the current group

packages/react/src/hooks/useUser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { useContext } from 'react'
22

33
import { FrigadeContext } from '@/components/Provider'
4+
import { logOnce } from '../shared/log'
45

56
export function useUser() {
6-
const { userId, frigade } = useContext(FrigadeContext)
7+
const context = useContext(FrigadeContext)
8+
if (!context || !context.frigade) {
9+
logOnce('useUser() must be used in a child of the Frigade Provider', 'error')
10+
}
11+
const { userId, frigade } = context ?? {}
712

813
/**
914
* Adds properties for the current user

packages/react/src/shared/log.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const logOnce = (message: string, type: 'log' | 'warn' | 'error' = 'log') => {
2+
const key = `__frigade_logged_${message}`
3+
4+
if (globalThis[key as keyof typeof globalThis]) {
5+
return
6+
}
7+
8+
// @ts-expect-error: globalThis is not typed
9+
globalThis[key as keyof typeof globalThis] = true
10+
console[type](message)
11+
}
12+
13+
export { logOnce }

0 commit comments

Comments
 (0)