@openpeepshq/rn-components 0.3.12 → 0.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openpeepshq/rn-components",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.13",
|
|
4
4
|
"description": "React Native community screens and shared UI (extracted from the Black Ambition app) for the OpenPeeps stack.",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -182,13 +182,6 @@
|
|
|
182
182
|
"turbo": "^1.10.7",
|
|
183
183
|
"typescript": "~5.8.3"
|
|
184
184
|
},
|
|
185
|
-
"jest": {
|
|
186
|
-
"preset": "react-native",
|
|
187
|
-
"modulePathIgnorePatterns": [
|
|
188
|
-
"<rootDir>/example/node_modules",
|
|
189
|
-
"<rootDir>/lib/"
|
|
190
|
-
]
|
|
191
|
-
},
|
|
192
185
|
"commitlint": {
|
|
193
186
|
"extends": [
|
|
194
187
|
"@commitlint/config-conventional"
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression tests for the page-chrome infinite update loop
|
|
3
|
+
* ("Maximum update depth exceeded") that crashed Groups, Jams and Explore
|
|
4
|
+
* (and any other page that registers header or plus actions with fresh,
|
|
5
|
+
* non-memoized values) in @openpeepshq/rn-components 0.3.12.
|
|
6
|
+
*
|
|
7
|
+
* Root cause: useSetPageHeader / useSetPlusButtonActions read their
|
|
8
|
+
* setters from the same context whose value changes whenever the header
|
|
9
|
+
* or plus state changes. A screen that registers a fresh element or
|
|
10
|
+
* object therefore re-renders on every chrome update, re-runs its layout
|
|
11
|
+
* effect and calls setState again.
|
|
12
|
+
*/
|
|
13
|
+
import { type ReactNode } from 'react';
|
|
14
|
+
import ReactTestRenderer from 'react-test-renderer';
|
|
15
|
+
import {
|
|
16
|
+
PageChromeProvider,
|
|
17
|
+
usePageChrome,
|
|
18
|
+
useSetPageHeader,
|
|
19
|
+
useSetPlusButtonActions,
|
|
20
|
+
} from './pageChrome';
|
|
21
|
+
|
|
22
|
+
const MAX_RENDER_GUARD = 20;
|
|
23
|
+
|
|
24
|
+
const Text = ({ children }: { children?: ReactNode }) => (
|
|
25
|
+
<text>{children}</text>
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const View = ({ children }: { children?: ReactNode }) => (
|
|
29
|
+
<view>{children}</view>
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const render = (ui: ReactNode) => {
|
|
33
|
+
const element = <PageChromeProvider>{ui}</PageChromeProvider>;
|
|
34
|
+
let tree!: ReactTestRenderer.ReactTestRenderer;
|
|
35
|
+
ReactTestRenderer.act(() => {
|
|
36
|
+
tree = ReactTestRenderer.create(element);
|
|
37
|
+
});
|
|
38
|
+
return tree;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const UnstableHeaderScreen = ({ onRender }: { onRender: () => void }) => {
|
|
42
|
+
onRender();
|
|
43
|
+
useSetPageHeader(
|
|
44
|
+
'Jams',
|
|
45
|
+
<View>
|
|
46
|
+
<Text>actions</Text>
|
|
47
|
+
</View>
|
|
48
|
+
);
|
|
49
|
+
return <Text>screen</Text>;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const UnstablePlusScreen = ({ onRender }: { onRender: () => void }) => {
|
|
53
|
+
onRender();
|
|
54
|
+
useSetPageHeader('Groups');
|
|
55
|
+
useSetPlusButtonActions({ title: 'New group', action: () => {} });
|
|
56
|
+
return <Text>screen</Text>;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const StableScreen = () => {
|
|
60
|
+
useSetPageHeader('Feed');
|
|
61
|
+
return <Text>screen</Text>;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const ChromeProbe = ({
|
|
65
|
+
onChrome,
|
|
66
|
+
}: {
|
|
67
|
+
onChrome: (title?: string, plusTitle?: string) => void;
|
|
68
|
+
}) => {
|
|
69
|
+
const { header, plus } = usePageChrome();
|
|
70
|
+
const plusTitle = Array.isArray(plus) ? plus[0]?.title : plus?.title;
|
|
71
|
+
onChrome(
|
|
72
|
+
typeof header?.title === 'string' ? header.title : undefined,
|
|
73
|
+
plusTitle
|
|
74
|
+
);
|
|
75
|
+
return null;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
test('unstable header actions do not loop (jams/explore)', () => {
|
|
79
|
+
let renders = 0;
|
|
80
|
+
const tree = render(
|
|
81
|
+
<UnstableHeaderScreen
|
|
82
|
+
onRender={() => {
|
|
83
|
+
renders += 1;
|
|
84
|
+
if (renders > MAX_RENDER_GUARD) {
|
|
85
|
+
throw new Error('Maximum update depth exceeded');
|
|
86
|
+
}
|
|
87
|
+
}}
|
|
88
|
+
/>
|
|
89
|
+
);
|
|
90
|
+
expect(renders).toBeLessThanOrEqual(MAX_RENDER_GUARD);
|
|
91
|
+
expect(JSON.stringify(tree.toJSON())).toContain('screen');
|
|
92
|
+
tree.unmount();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('unstable plus actions do not loop (groups)', () => {
|
|
96
|
+
let renders = 0;
|
|
97
|
+
const tree = render(
|
|
98
|
+
<UnstablePlusScreen
|
|
99
|
+
onRender={() => {
|
|
100
|
+
renders += 1;
|
|
101
|
+
if (renders > MAX_RENDER_GUARD) {
|
|
102
|
+
throw new Error('Maximum update depth exceeded');
|
|
103
|
+
}
|
|
104
|
+
}}
|
|
105
|
+
/>
|
|
106
|
+
);
|
|
107
|
+
expect(renders).toBeLessThanOrEqual(MAX_RENDER_GUARD);
|
|
108
|
+
expect(JSON.stringify(tree.toJSON())).toContain('screen');
|
|
109
|
+
tree.unmount();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('stable title-only header still works', () => {
|
|
113
|
+
const tree = render(<StableScreen />);
|
|
114
|
+
expect(JSON.stringify(tree.toJSON())).toContain('screen');
|
|
115
|
+
tree.unmount();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('display chrome still sees registered header and plus', () => {
|
|
119
|
+
let title: string | undefined;
|
|
120
|
+
let plusTitle: string | undefined;
|
|
121
|
+
const tree = render(
|
|
122
|
+
<View>
|
|
123
|
+
<UnstablePlusScreen onRender={() => {}} />
|
|
124
|
+
<ChromeProbe
|
|
125
|
+
onChrome={(nextTitle, nextPlusTitle) => {
|
|
126
|
+
title = nextTitle;
|
|
127
|
+
plusTitle = nextPlusTitle;
|
|
128
|
+
}}
|
|
129
|
+
/>
|
|
130
|
+
</View>
|
|
131
|
+
);
|
|
132
|
+
expect(title).toBe('Groups');
|
|
133
|
+
expect(plusTitle).toBe('New group');
|
|
134
|
+
tree.unmount();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('page chrome hooks throw outside RootLayout', () => {
|
|
138
|
+
const Bare = () => {
|
|
139
|
+
useSetPageHeader('Feed');
|
|
140
|
+
return null;
|
|
141
|
+
};
|
|
142
|
+
expect(() => {
|
|
143
|
+
ReactTestRenderer.act(() => {
|
|
144
|
+
ReactTestRenderer.create(<Bare />);
|
|
145
|
+
});
|
|
146
|
+
}).toThrow('Page chrome hooks must be used within RootLayout');
|
|
147
|
+
});
|
|
@@ -23,43 +23,78 @@ export type PageHeaderValue = {
|
|
|
23
23
|
onBack?: () => void;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
type
|
|
27
|
-
header: PageHeaderValue | undefined;
|
|
26
|
+
type PageChromeSetters = {
|
|
28
27
|
setHeader: (header: PageHeaderValue | undefined) => void;
|
|
29
|
-
plus: PlusButtonActions;
|
|
30
28
|
setPlus: (plus: PlusButtonActions) => void;
|
|
31
29
|
};
|
|
32
30
|
|
|
33
|
-
|
|
31
|
+
type PageChromeValues = {
|
|
32
|
+
header: PageHeaderValue | undefined;
|
|
33
|
+
plus: PlusButtonActions;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Stable setters context. Its value never changes, so components that call
|
|
38
|
+
* `useSetPageHeader` / `useSetPlusButtonActions` are not re-rendered when
|
|
39
|
+
* the chrome values change. Keeping the setters out of the value context is
|
|
40
|
+
* what prevents infinite update loops ("Maximum update depth exceeded") for
|
|
41
|
+
* pages that register a fresh, non-memoized header element or plus-actions
|
|
42
|
+
* object on every render.
|
|
43
|
+
*/
|
|
44
|
+
const PageChromeSettersContext = createContext<PageChromeSetters | null>(null);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Chrome values context. Its value changes whenever the header or plus
|
|
48
|
+
* actions change, so it must only be consumed by display components
|
|
49
|
+
* (header/plus button) — never by a component that registers the values.
|
|
50
|
+
*/
|
|
51
|
+
const PageChromeValuesContext = createContext<PageChromeValues | null>(null);
|
|
52
|
+
|
|
53
|
+
const MISSING_CHROME_ERROR = 'Page chrome hooks must be used within RootLayout';
|
|
34
54
|
|
|
35
55
|
export const PageChromeProvider = ({ children }: { children: ReactNode }) => {
|
|
36
56
|
const [header, setHeader] = useState<PageHeaderValue | undefined>();
|
|
37
57
|
const [plus, setPlus] = useState<PlusButtonActions>(undefined);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
);
|
|
58
|
+
// useState setters are stable, so this value is created exactly once.
|
|
59
|
+
const setters = useMemo(() => ({ setHeader, setPlus }), [setHeader, setPlus]);
|
|
60
|
+
const values = useMemo(() => ({ header, plus }), [header, plus]);
|
|
42
61
|
return (
|
|
43
|
-
<
|
|
44
|
-
{
|
|
45
|
-
|
|
62
|
+
<PageChromeSettersContext.Provider value={setters}>
|
|
63
|
+
<PageChromeValuesContext.Provider value={values}>
|
|
64
|
+
{children}
|
|
65
|
+
</PageChromeValuesContext.Provider>
|
|
66
|
+
</PageChromeSettersContext.Provider>
|
|
46
67
|
);
|
|
47
68
|
};
|
|
48
69
|
|
|
49
|
-
|
|
50
|
-
const ctx = useContext(
|
|
70
|
+
const useSetters = () => {
|
|
71
|
+
const ctx = useContext(PageChromeSettersContext);
|
|
51
72
|
if (!ctx) {
|
|
52
|
-
throw new Error(
|
|
73
|
+
throw new Error(MISSING_CHROME_ERROR);
|
|
53
74
|
}
|
|
54
75
|
return ctx;
|
|
55
76
|
};
|
|
56
77
|
|
|
78
|
+
const useValues = () => {
|
|
79
|
+
const ctx = useContext(PageChromeValuesContext);
|
|
80
|
+
if (!ctx) {
|
|
81
|
+
throw new Error(MISSING_CHROME_ERROR);
|
|
82
|
+
}
|
|
83
|
+
return ctx;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const usePageChrome = () => {
|
|
87
|
+
const values = useValues();
|
|
88
|
+
const setters = useSetters();
|
|
89
|
+
return { ...values, ...setters };
|
|
90
|
+
};
|
|
91
|
+
|
|
57
92
|
export const useSetPageHeader = (
|
|
58
93
|
title?: ReactNode,
|
|
59
94
|
actions?: ReactNode,
|
|
60
95
|
onBack?: () => void
|
|
61
96
|
) => {
|
|
62
|
-
const { setHeader } =
|
|
97
|
+
const { setHeader } = useSetters();
|
|
63
98
|
useLayoutEffect(() => {
|
|
64
99
|
setHeader({ title, actions, onBack });
|
|
65
100
|
return () => setHeader(undefined);
|
|
@@ -67,7 +102,7 @@ export const useSetPageHeader = (
|
|
|
67
102
|
};
|
|
68
103
|
|
|
69
104
|
export const useSetPlusButtonActions = (actions: PlusButtonActions) => {
|
|
70
|
-
const { setPlus } =
|
|
105
|
+
const { setPlus } = useSetters();
|
|
71
106
|
useLayoutEffect(() => {
|
|
72
107
|
setPlus(actions);
|
|
73
108
|
return () => setPlus(undefined);
|