@micha.bigler/ui-core-micha 2.8.0 → 2.8.2
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/dist/notifications/NotificationSettings.js +1 -2
- package/dist/onboarding/OnboardingProvider.js +7 -2
- package/package.json +1 -1
- package/src/notifications/NotificationSettings.jsx +13 -9
- package/src/onboarding/OnboardingProvider.jsx +11 -2
- package/tests/NotificationSettings.test.jsx +2 -2
- package/tests/OnboardingProvider.test.jsx +55 -3
|
@@ -3,7 +3,6 @@ import React, { useEffect, useState } from 'react';
|
|
|
3
3
|
import { useTranslation } from 'react-i18next';
|
|
4
4
|
import Alert from '@mui/material/Alert';
|
|
5
5
|
import Box from '@mui/material/Box';
|
|
6
|
-
import Button from '@mui/material/Button';
|
|
7
6
|
import CircularProgress from '@mui/material/CircularProgress';
|
|
8
7
|
import Divider from '@mui/material/Divider';
|
|
9
8
|
import FormControlLabel from '@mui/material/FormControlLabel';
|
|
@@ -143,6 +142,6 @@ export function NotificationSettings() {
|
|
|
143
142
|
if (loading) {
|
|
144
143
|
return _jsx(Box, { sx: { display: 'flex', justifyContent: 'center', py: 4 }, children: _jsx(CircularProgress, {}) });
|
|
145
144
|
}
|
|
146
|
-
return (_jsxs(Box, { sx: { maxWidth: 520 }, children: [_jsxs(Typography, { variant: "h6", gutterBottom: true, sx: { display: 'flex', alignItems: 'center', gap: 1 }, children: [_jsx(NotificationsIcon, {}), t('NotificationSettings.TITLE')] }), _jsx(Typography, { variant: "body2", color: "text.secondary", gutterBottom: true, children: t('NotificationSettings.SUBTITLE') }), error && _jsx(Alert, { severity: "error", sx: { mb: 2 }, onClose: () => setError(''), children: error }), conflict && _jsx(Alert, { severity: "warning", sx: { mb: 2 }, onClose: () => setConflict(''), children: conflict }), _jsx(Box, { sx: { py: 2 }, children: _jsx(FormControlLabel, { control: _jsx(Switch, { checked: Boolean(preferences === null || preferences === void 0 ? void 0 : preferences.email_opt_in), onChange: handleEmailToggle, disabled: savingEmail }), label: _jsxs(Box, { children: [_jsx(Typography, { variant: "body1", children: t('NotificationSettings.EMAIL_LABEL') }), _jsx(Typography, { variant: "caption", color: "text.secondary", children: t('NotificationSettings.EMAIL_HINT') })] }), labelPlacement: "end", sx: { alignItems: 'flex-start', ml: 0 } }) }), _jsx(Divider, {}), _jsxs(Box, { sx: { py: 2 }, children: [
|
|
145
|
+
return (_jsxs(Box, { sx: { maxWidth: 520 }, children: [_jsxs(Typography, { variant: "h6", gutterBottom: true, sx: { display: 'flex', alignItems: 'center', gap: 1 }, children: [_jsx(NotificationsIcon, {}), t('NotificationSettings.TITLE')] }), _jsx(Typography, { variant: "body2", color: "text.secondary", gutterBottom: true, children: t('NotificationSettings.SUBTITLE') }), error && _jsx(Alert, { severity: "error", sx: { mb: 2 }, onClose: () => setError(''), children: error }), conflict && _jsx(Alert, { severity: "warning", sx: { mb: 2 }, onClose: () => setConflict(''), children: conflict }), _jsx(Box, { sx: { py: 2 }, children: _jsx(FormControlLabel, { control: _jsx(Switch, { checked: Boolean(preferences === null || preferences === void 0 ? void 0 : preferences.email_opt_in), onChange: handleEmailToggle, disabled: savingEmail }), label: _jsxs(Box, { children: [_jsx(Typography, { variant: "body1", children: t('NotificationSettings.EMAIL_LABEL') }), _jsx(Typography, { variant: "caption", color: "text.secondary", children: t('NotificationSettings.EMAIL_HINT') })] }), labelPlacement: "end", sx: { alignItems: 'flex-start', ml: 0 } }) }), _jsx(Divider, {}), _jsxs(Box, { sx: { py: 2 }, children: [iosNeedsInstall && _jsx(Alert, { severity: "info", sx: { mb: 1.5 }, children: t('NotificationSettings.IOS_HINT') }), !pushSupported && !iosNeedsInstall && _jsx(Alert, { severity: "warning", sx: { mb: 1.5 }, children: t('NotificationSettings.PUSH_NOT_SUPPORTED') }), _jsx(FormControlLabel, { control: (_jsx(Switch, { checked: pushSubscribed, onChange: (event) => (event.target.checked ? handleEnablePush() : handleDisablePush()), disabled: savingPush || !pushSupported || iosNeedsInstall })), label: _jsxs(Box, { children: [_jsx(Typography, { variant: "body1", children: t('NotificationSettings.PUSH_LABEL') }), _jsx(Typography, { variant: "caption", color: "text.secondary", children: t('NotificationSettings.PUSH_HINT') })] }), labelPlacement: "end", sx: { alignItems: 'flex-start', ml: 0 } })] })] }));
|
|
147
146
|
}
|
|
148
147
|
export default NotificationSettings;
|
|
@@ -61,7 +61,11 @@ function getInitialPushState() {
|
|
|
61
61
|
&& 'PushManager' in window;
|
|
62
62
|
return supported ? null : { supported: false, subscribed: false };
|
|
63
63
|
}
|
|
64
|
-
|
|
64
|
+
// Stable empty-object default — an inline `{}` default would create a new
|
|
65
|
+
// reference every render, invalidating the `ctx` memo below for every
|
|
66
|
+
// consumer even when no extraContext is passed.
|
|
67
|
+
const EMPTY_EXTRA_CONTEXT = {};
|
|
68
|
+
export function OnboardingProvider({ children, extraSteps = [], extraContext = EMPTY_EXTRA_CONTEXT }) {
|
|
65
69
|
const { user } = useContext(AuthContext);
|
|
66
70
|
const [configMap, setConfigMap] = useState({});
|
|
67
71
|
const [configLoaded, setConfigLoaded] = useState(false);
|
|
@@ -146,7 +150,8 @@ export function OnboardingProvider({ children, extraSteps = [] }) {
|
|
|
146
150
|
return next;
|
|
147
151
|
});
|
|
148
152
|
}, []);
|
|
149
|
-
|
|
153
|
+
// extraContext is spread last: duplicate core keys intentionally override core values, so apps should use distinct names.
|
|
154
|
+
const ctx = useMemo(() => (Object.assign({ user, pushState, emailOptedIn }, extraContext)), [user, pushState, emailOptedIn, extraContext]);
|
|
150
155
|
const activeSteps = useMemo(() => {
|
|
151
156
|
if (!configLoaded || !user)
|
|
152
157
|
return [];
|
package/package.json
CHANGED
|
@@ -2,7 +2,6 @@ import React, { useEffect, useState } from 'react';
|
|
|
2
2
|
import { useTranslation } from 'react-i18next';
|
|
3
3
|
import Alert from '@mui/material/Alert';
|
|
4
4
|
import Box from '@mui/material/Box';
|
|
5
|
-
import Button from '@mui/material/Button';
|
|
6
5
|
import CircularProgress from '@mui/material/CircularProgress';
|
|
7
6
|
import Divider from '@mui/material/Divider';
|
|
8
7
|
import FormControlLabel from '@mui/material/FormControlLabel';
|
|
@@ -172,15 +171,20 @@ export function NotificationSettings() {
|
|
|
172
171
|
<Divider />
|
|
173
172
|
|
|
174
173
|
<Box sx={{ py: 2 }}>
|
|
175
|
-
<Typography variant="body1" gutterBottom>{t('NotificationSettings.PUSH_LABEL')}</Typography>
|
|
176
|
-
<Typography variant="caption" color="text.secondary" display="block" sx={{ mb: 1.5 }}>{t('NotificationSettings.PUSH_HINT')}</Typography>
|
|
177
174
|
{iosNeedsInstall && <Alert severity="info" sx={{ mb: 1.5 }}>{t('NotificationSettings.IOS_HINT')}</Alert>}
|
|
178
|
-
{!pushSupported && !iosNeedsInstall && <Alert severity="warning">{t('NotificationSettings.PUSH_NOT_SUPPORTED')}</Alert>}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
175
|
+
{!pushSupported && !iosNeedsInstall && <Alert severity="warning" sx={{ mb: 1.5 }}>{t('NotificationSettings.PUSH_NOT_SUPPORTED')}</Alert>}
|
|
176
|
+
<FormControlLabel
|
|
177
|
+
control={(
|
|
178
|
+
<Switch
|
|
179
|
+
checked={pushSubscribed}
|
|
180
|
+
onChange={(event) => (event.target.checked ? handleEnablePush() : handleDisablePush())}
|
|
181
|
+
disabled={savingPush || !pushSupported || iosNeedsInstall}
|
|
182
|
+
/>
|
|
183
|
+
)}
|
|
184
|
+
label={<Box><Typography variant="body1">{t('NotificationSettings.PUSH_LABEL')}</Typography><Typography variant="caption" color="text.secondary">{t('NotificationSettings.PUSH_HINT')}</Typography></Box>}
|
|
185
|
+
labelPlacement="end"
|
|
186
|
+
sx={{ alignItems: 'flex-start', ml: 0 }}
|
|
187
|
+
/>
|
|
184
188
|
</Box>
|
|
185
189
|
</Box>
|
|
186
190
|
);
|
|
@@ -70,7 +70,12 @@ function getInitialPushState() {
|
|
|
70
70
|
return supported ? null : { supported: false, subscribed: false };
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
// Stable empty-object default — an inline `{}` default would create a new
|
|
74
|
+
// reference every render, invalidating the `ctx` memo below for every
|
|
75
|
+
// consumer even when no extraContext is passed.
|
|
76
|
+
const EMPTY_EXTRA_CONTEXT = {};
|
|
77
|
+
|
|
78
|
+
export function OnboardingProvider({ children, extraSteps = [], extraContext = EMPTY_EXTRA_CONTEXT }) {
|
|
74
79
|
const { user } = useContext(AuthContext);
|
|
75
80
|
const [configMap, setConfigMap] = useState({});
|
|
76
81
|
const [configLoaded, setConfigLoaded] = useState(false);
|
|
@@ -162,7 +167,11 @@ export function OnboardingProvider({ children, extraSteps = [] }) {
|
|
|
162
167
|
});
|
|
163
168
|
}, []);
|
|
164
169
|
|
|
165
|
-
|
|
170
|
+
// extraContext is spread last: duplicate core keys intentionally override core values, so apps should use distinct names.
|
|
171
|
+
const ctx = useMemo(
|
|
172
|
+
() => ({ user, pushState, emailOptedIn, ...extraContext }),
|
|
173
|
+
[user, pushState, emailOptedIn, extraContext],
|
|
174
|
+
);
|
|
166
175
|
const activeSteps = useMemo(() => {
|
|
167
176
|
if (!configLoaded || !user) return [];
|
|
168
177
|
return selectActiveSteps(descriptors, configMap, ctx, dismissedSet);
|
|
@@ -49,7 +49,7 @@ describe('NotificationSettings push toggle', () => {
|
|
|
49
49
|
vi.stubGlobal('Notification', { requestPermission: vi.fn().mockResolvedValue('granted') });
|
|
50
50
|
|
|
51
51
|
render(<NotificationSettings />);
|
|
52
|
-
fireEvent.click(await screen.findByRole('
|
|
52
|
+
fireEvent.click(await screen.findByRole('switch', { name: /PUSH_LABEL/ }));
|
|
53
53
|
|
|
54
54
|
await waitFor(() => expect(notificationsApi.patchNotificationPreferences).toHaveBeenCalledWith({ push_opt_in: true }));
|
|
55
55
|
expect(Notification.requestPermission).toHaveBeenCalledOnce();
|
|
@@ -73,7 +73,7 @@ describe('NotificationSettings push toggle', () => {
|
|
|
73
73
|
installPushEnvironment(subscription);
|
|
74
74
|
|
|
75
75
|
render(<NotificationSettings />);
|
|
76
|
-
fireEvent.click(await screen.findByRole('
|
|
76
|
+
fireEvent.click(await screen.findByRole('switch', { name: /PUSH_LABEL/ }));
|
|
77
77
|
|
|
78
78
|
await waitFor(() => expect(subscription.unsubscribe).toHaveBeenCalledOnce());
|
|
79
79
|
expect(notificationsApi.removePushSubscription).toHaveBeenCalledWith({ endpoint: subscription.endpoint });
|
|
@@ -21,10 +21,22 @@ function Probe() {
|
|
|
21
21
|
return <span data-testid="email-opted-in">{String(onboarding?.ctx.emailOptedIn)}</span>;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
function
|
|
24
|
+
function ActiveStepsProbe() {
|
|
25
|
+
const onboarding = useOnboarding();
|
|
26
|
+
return <span data-testid="active-steps">{onboarding?.activeSteps.map((step) => step.id).join(',')}</span>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function renderProvider({ extraContext, extraSteps, children = <Probe /> } = {}) {
|
|
25
30
|
return render(
|
|
26
|
-
<AuthContext.Provider value={{
|
|
27
|
-
|
|
31
|
+
<AuthContext.Provider value={{
|
|
32
|
+
user: {
|
|
33
|
+
id: 1,
|
|
34
|
+
accepted_privacy_statement: true,
|
|
35
|
+
first_name: 'Ada',
|
|
36
|
+
last_name: 'Lovelace',
|
|
37
|
+
},
|
|
38
|
+
}}>
|
|
39
|
+
<OnboardingProvider extraContext={extraContext} extraSteps={extraSteps}>{children}</OnboardingProvider>
|
|
28
40
|
</AuthContext.Provider>,
|
|
29
41
|
);
|
|
30
42
|
}
|
|
@@ -54,3 +66,43 @@ describe('OnboardingProvider notification preferences', () => {
|
|
|
54
66
|
expect(screen.getByTestId('email-opted-in').textContent).toBe('false');
|
|
55
67
|
});
|
|
56
68
|
});
|
|
69
|
+
|
|
70
|
+
describe('OnboardingProvider extra context', () => {
|
|
71
|
+
const unreadMessagesCondition = vi.fn((ctx) => (ctx.unreadCount || 0) > 0);
|
|
72
|
+
const unreadMessagesStep = {
|
|
73
|
+
id: 'unread_messages',
|
|
74
|
+
condition: unreadMessagesCondition,
|
|
75
|
+
persistDismissed: false,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
beforeEach(() => {
|
|
79
|
+
cleanup();
|
|
80
|
+
vi.clearAllMocks();
|
|
81
|
+
onboardingApi.getOnboardingStepConfig.mockResolvedValue([]);
|
|
82
|
+
notificationsApi.getNotificationPreferences.mockResolvedValue({ email_opt_in: true });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('shows an app step when its extra context condition is met', async () => {
|
|
86
|
+
renderProvider({
|
|
87
|
+
extraContext: { unreadCount: 2 },
|
|
88
|
+
extraSteps: [unreadMessagesStep],
|
|
89
|
+
children: <ActiveStepsProbe />,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
await waitFor(() => expect(screen.getByTestId('active-steps').textContent).toContain('unread_messages'));
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it.each([
|
|
96
|
+
['zero', { unreadCount: 0 }],
|
|
97
|
+
['omitted', undefined],
|
|
98
|
+
])('does not show an app step when unreadCount is %s', async (_label, extraContext) => {
|
|
99
|
+
renderProvider({
|
|
100
|
+
extraContext,
|
|
101
|
+
extraSteps: [unreadMessagesStep],
|
|
102
|
+
children: <ActiveStepsProbe />,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
await waitFor(() => expect(unreadMessagesCondition).toHaveBeenCalled());
|
|
106
|
+
expect(screen.getByTestId('active-steps').textContent).not.toContain('unread_messages');
|
|
107
|
+
});
|
|
108
|
+
});
|