@umituz/web-dashboard 7.6.0 → 7.8.0
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/README.md +4 -2
- package/package.json +3 -1
- package/src/domains/onboarding/components/OnboardingFlow.tsx +9 -0
- package/src/domains/onboarding/components/screens/OrganizationDetailsScreen.test.tsx +115 -0
- package/src/domains/onboarding/components/screens/OrganizationDetailsScreen.tsx +74 -0
- package/src/domains/onboarding/components/screens/index.ts +2 -0
- package/src/domains/onboarding/index.ts +3 -0
- package/src/domains/onboarding/types/index.ts +7 -1
- package/src/domains/onboarding/types/labels.ts +4 -0
- package/src/domains/onboarding/types/screens.ts +12 -0
- package/src/domains/onboarding/validation/screen-config-check.ts +1 -0
- package/src/domains/onboarding/validation/validation.test.ts +7 -1
- package/src/domains/organization/OrganizationContext.test.tsx +113 -0
- package/src/domains/organization/OrganizationContext.tsx +111 -0
- package/src/domains/organization/components/InviteForm.test.tsx +72 -0
- package/src/domains/organization/components/InviteForm.tsx +101 -0
- package/src/domains/organization/components/OrganizationCard.test.tsx +44 -0
- package/src/domains/organization/components/OrganizationCard.tsx +87 -0
- package/src/domains/organization/components/OrganizationMembersPanel.test.tsx +88 -0
- package/src/domains/organization/components/OrganizationMembersPanel.tsx +105 -0
- package/src/domains/organization/components/OrganizationSwitcher.test.tsx +37 -0
- package/src/domains/organization/components/OrganizationSwitcher.tsx +33 -0
- package/src/domains/organization/components/index.ts +11 -0
- package/src/domains/organization/index.ts +29 -0
- package/src/domains/organization/types.ts +59 -0
- package/src/domains/organization/vocab.test.ts +32 -0
- package/src/domains/organization/vocab.ts +39 -0
- package/src/domains/settings/components/OrganizationProfileForm.test.tsx +114 -0
- package/src/domains/settings/components/OrganizationProfileForm.tsx +164 -0
- package/src/domains/settings/components/UserManagementTable.tsx +43 -1
- package/src/domains/settings/components/index.ts +9 -0
- package/src/styles/chrome.css +158 -0
package/README.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# @umituz/web-dashboard
|
|
2
2
|
|
|
3
3
|
Installable app skeleton for SaaS dashboards: layouts, analytics, a content
|
|
4
|
-
calendar, a product onboarding flow,
|
|
4
|
+
calendar, a product onboarding flow, a permissions/module registry, and an
|
|
5
|
+
organization/membership module. Every
|
|
5
6
|
domain is an independent entry point — import only what your app uses. UI
|
|
6
7
|
atoms and design tokens come from [@umituz/web-design-system](https://github.com/umituz/web-design-system);
|
|
7
8
|
translations from [@umituz/web-localization](https://github.com/umituz/web-localization).
|
|
@@ -44,10 +45,11 @@ hosts set dark values on the DS variables under `[data-theme="dark"]` /
|
|
|
44
45
|
```bash
|
|
45
46
|
@umituz/web-dashboard/styles.css # component stylesheet (udb-* BEM, incl. udb-onb-*)
|
|
46
47
|
@umituz/web-dashboard/permissions # PermissionMatrix, hasPermission, guards
|
|
48
|
+
@umituz/web-dashboard/organization # OrganizationProvider, members panel, invites, switcher
|
|
47
49
|
@umituz/web-dashboard/modules # DashboardModule, buildRoutes, buildNav
|
|
48
50
|
@umituz/web-dashboard/layouts # DashboardLayout, DashboardSidebar, DashboardHeader
|
|
49
51
|
@umituz/web-dashboard/settings # SettingsLayout, admin skeletons (users, matrix, audit)
|
|
50
|
-
@umituz/web-dashboard/onboarding # OnboardingFlow +
|
|
52
|
+
@umituz/web-dashboard/onboarding # OnboardingFlow + 15 screens, persistence, analytics
|
|
51
53
|
@umituz/web-dashboard/analytics # AnalyticsLayout, SVG AnalyticsChart, services
|
|
52
54
|
@umituz/web-dashboard/calendar # useCalendar, CalendarService (injected DB)
|
|
53
55
|
@umituz/web-dashboard/config # dashboard config helpers
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-dashboard",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.8.0",
|
|
4
4
|
"description": "Installable app skeleton for the fleet — layouts, analytics (dependency-free SVG charts), calendar, onboarding flow permissions/module registry; udb-* stylesheet on design-system tokens",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
"./layouts/utils": "./src/domains/layouts/utils/index.ts",
|
|
33
33
|
"./modules": "./src/domains/modules/index.ts",
|
|
34
34
|
"./onboarding": "./src/domains/onboarding/index.ts",
|
|
35
|
+
"./organization": "./src/domains/organization/index.ts",
|
|
36
|
+
"./organization/components": "./src/domains/organization/components/index.ts",
|
|
35
37
|
"./package.json": "./package.json",
|
|
36
38
|
"./permissions": "./src/domains/permissions/index.ts",
|
|
37
39
|
"./settings": "./src/domains/settings/index.ts",
|
|
@@ -13,6 +13,7 @@ import { checkFlowStructure } from '../validation/flow-structure-check';
|
|
|
13
13
|
import {
|
|
14
14
|
WelcomeScreen,
|
|
15
15
|
GoalQuestionScreen,
|
|
16
|
+
OrganizationDetailsScreen,
|
|
16
17
|
PainPointsScreen,
|
|
17
18
|
SocialProofScreen,
|
|
18
19
|
TinderCardsScreen,
|
|
@@ -69,6 +70,14 @@ function renderScreen(
|
|
|
69
70
|
}
|
|
70
71
|
/>
|
|
71
72
|
);
|
|
73
|
+
case 'organization-details':
|
|
74
|
+
return (
|
|
75
|
+
<OrganizationDetailsScreen
|
|
76
|
+
config={screen}
|
|
77
|
+
onNext={(data) => onStepCompleted(data)}
|
|
78
|
+
initial={progress.collected[screen.id] as { name: string; teamSize?: string } | undefined}
|
|
79
|
+
/>
|
|
80
|
+
);
|
|
72
81
|
case 'social-proof':
|
|
73
82
|
return <SocialProofScreen config={screen} onNext={() => onStepCompleted(undefined)} />;
|
|
74
83
|
case 'tinder-cards':
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
+
import { OrganizationDetailsScreen } from './OrganizationDetailsScreen';
|
|
4
|
+
import { OnboardingIntlProvider } from '../OnboardingIntl';
|
|
5
|
+
import type { OnboardingLabels } from '../../types/labels';
|
|
6
|
+
import type { OrganizationDetailsScreenConfig } from '../../types/screens';
|
|
7
|
+
|
|
8
|
+
const LABELS: OnboardingLabels = {
|
|
9
|
+
progressLabel: 'P',
|
|
10
|
+
continueAction: 'Continue',
|
|
11
|
+
back: 'Back',
|
|
12
|
+
skip: 'Skip',
|
|
13
|
+
loading: 'Loading',
|
|
14
|
+
stepOfPattern: '{a} of {b}',
|
|
15
|
+
countOfPattern: '{a}/{b}',
|
|
16
|
+
selectAllThatApply: 'Select all',
|
|
17
|
+
selectOne: 'Select one',
|
|
18
|
+
pickUpToPattern: 'Pick {n}',
|
|
19
|
+
pickTheOne: 'Pick one',
|
|
20
|
+
tailorExperience: 'Tailor',
|
|
21
|
+
agree: 'Agree',
|
|
22
|
+
disagree: 'Disagree',
|
|
23
|
+
swipeHint: 'Swipe',
|
|
24
|
+
allDone: 'Done',
|
|
25
|
+
withoutUs: 'Without',
|
|
26
|
+
withUs: 'With',
|
|
27
|
+
previous: 'Prev',
|
|
28
|
+
nextAction: 'Next',
|
|
29
|
+
continueWithResult: 'Continue',
|
|
30
|
+
allowAccess: 'Allow',
|
|
31
|
+
permissionRequestFailed: 'Failed',
|
|
32
|
+
continueWithEmail: 'Email',
|
|
33
|
+
continueWithGoogle: 'Google',
|
|
34
|
+
continueWithApple: 'Apple',
|
|
35
|
+
continueWithGitHub: 'GitHub',
|
|
36
|
+
skipForNow: 'Skip',
|
|
37
|
+
signInFailed: 'Failed',
|
|
38
|
+
couldNotStartTrial: 'No trial',
|
|
39
|
+
trialDaysPattern: '{n} days',
|
|
40
|
+
trialIntroPattern: 'Trial',
|
|
41
|
+
cancelAnytimeNote: 'Cancel anytime',
|
|
42
|
+
share: 'Share',
|
|
43
|
+
copied: 'Copied',
|
|
44
|
+
somethingWentWrong: 'Error',
|
|
45
|
+
unexpectedError: 'Unexpected',
|
|
46
|
+
tryAgain: 'Retry',
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const CONFIG: OrganizationDetailsScreenConfig = {
|
|
50
|
+
id: 'org',
|
|
51
|
+
type: 'organization-details',
|
|
52
|
+
title: 'About your organization',
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const mount = (
|
|
56
|
+
props: Partial<Parameters<typeof OrganizationDetailsScreen>[0]> = {},
|
|
57
|
+
labels?: Partial<OnboardingLabels>,
|
|
58
|
+
) =>
|
|
59
|
+
render(
|
|
60
|
+
<OnboardingIntlProvider labels={{ ...LABELS, ...labels }}>
|
|
61
|
+
<OrganizationDetailsScreen config={CONFIG} onNext={() => undefined} {...props} />
|
|
62
|
+
</OnboardingIntlProvider>,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const continueButton = () =>
|
|
66
|
+
screen.getByText('Continue').closest('button') as HTMLButtonElement;
|
|
67
|
+
|
|
68
|
+
describe('OrganizationDetailsScreen', () => {
|
|
69
|
+
it('requires a non-blank organization name to continue', () => {
|
|
70
|
+
mount();
|
|
71
|
+
expect(continueButton().disabled).toBe(true);
|
|
72
|
+
fireEvent.change(screen.getByLabelText('Organization name'), {
|
|
73
|
+
target: { value: ' Northwind ' },
|
|
74
|
+
});
|
|
75
|
+
expect(continueButton().disabled).toBe(false);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('emits the trimmed name and selected team size', () => {
|
|
79
|
+
const onNext = vi.fn();
|
|
80
|
+
mount({
|
|
81
|
+
onNext,
|
|
82
|
+
config: {
|
|
83
|
+
...CONFIG,
|
|
84
|
+
teamSizeOptions: [
|
|
85
|
+
{ id: '1', label: 'Just me' },
|
|
86
|
+
{ id: '2-10', label: '2-10' },
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
fireEvent.change(screen.getByLabelText('Organization name'), {
|
|
91
|
+
target: { value: ' Northwind ' },
|
|
92
|
+
});
|
|
93
|
+
fireEvent.change(screen.getByLabelText('Team size'), { target: { value: '2-10' } });
|
|
94
|
+
fireEvent.click(continueButton());
|
|
95
|
+
expect(onNext).toHaveBeenCalledWith({ name: 'Northwind', teamSize: '2-10' });
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('skips the team-size question without options', () => {
|
|
99
|
+
mount();
|
|
100
|
+
expect(screen.queryByLabelText('Team size')).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('restores a previously collected answer', () => {
|
|
104
|
+
mount({ initial: { name: 'Contoso', teamSize: '2-10' } });
|
|
105
|
+
expect((screen.getByLabelText('Organization name') as HTMLInputElement).value).toBe(
|
|
106
|
+
'Contoso',
|
|
107
|
+
);
|
|
108
|
+
expect(continueButton().disabled).toBe(false);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('honors label overrides', () => {
|
|
112
|
+
mount({}, { organizationNameLabel: 'Kurum adı' });
|
|
113
|
+
expect(screen.getByLabelText('Kurum adı')).not.toBeNull();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useId, useState } from 'react';
|
|
2
|
+
import { PrimaryButton } from '../PrimaryButton';
|
|
3
|
+
import { QuestionWrapper } from '../QuestionWrapper';
|
|
4
|
+
import { useOnboardingIntl } from '../OnboardingIntl';
|
|
5
|
+
import type {
|
|
6
|
+
OnboardingOption,
|
|
7
|
+
OrganizationDetailsData,
|
|
8
|
+
OrganizationDetailsScreenConfig,
|
|
9
|
+
} from '../../types/screens';
|
|
10
|
+
|
|
11
|
+
export interface OrganizationDetailsScreenProps {
|
|
12
|
+
config: OrganizationDetailsScreenConfig;
|
|
13
|
+
onNext: (data: OrganizationDetailsData) => void;
|
|
14
|
+
/** Previously collected answer, restored when the user navigates back. */
|
|
15
|
+
initial?: OrganizationDetailsData;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function OrganizationDetailsScreen({ config, onNext, initial }: OrganizationDetailsScreenProps) {
|
|
19
|
+
const { labels } = useOnboardingIntl();
|
|
20
|
+
const nameId = useId();
|
|
21
|
+
const teamId = useId();
|
|
22
|
+
const [name, setName] = useState(initial?.name ?? '');
|
|
23
|
+
const [teamSize, setTeamSize] = useState(
|
|
24
|
+
initial?.teamSize ?? config.teamSizeOptions?.[0]?.id ?? '',
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const nameLabel = labels.organizationNameLabel ?? 'Organization name';
|
|
28
|
+
const teamLabel = labels.teamSizeLabel ?? 'Team size';
|
|
29
|
+
const canContinue = name.trim().length > 0;
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<QuestionWrapper title={config.title ?? labels.organizationTitle ?? 'About your organization'}>
|
|
33
|
+
<div className="udb-onb-field">
|
|
34
|
+
<label className="udb-onb-field__label" htmlFor={nameId}>
|
|
35
|
+
{nameLabel}
|
|
36
|
+
</label>
|
|
37
|
+
<input
|
|
38
|
+
id={nameId}
|
|
39
|
+
className="udb-onb-input"
|
|
40
|
+
value={name}
|
|
41
|
+
placeholder={config.namePlaceholder}
|
|
42
|
+
autoComplete="organization"
|
|
43
|
+
onChange={(e) => setName(e.target.value)}
|
|
44
|
+
/>
|
|
45
|
+
</div>
|
|
46
|
+
|
|
47
|
+
{config.teamSizeOptions && config.teamSizeOptions.length > 0 && (
|
|
48
|
+
<div className="udb-onb-field">
|
|
49
|
+
<label className="udb-onb-field__label" htmlFor={teamId}>
|
|
50
|
+
{teamLabel}
|
|
51
|
+
</label>
|
|
52
|
+
<select
|
|
53
|
+
id={teamId}
|
|
54
|
+
className="udb-onb-select"
|
|
55
|
+
value={teamSize}
|
|
56
|
+
onChange={(e) => setTeamSize(e.target.value)}
|
|
57
|
+
>
|
|
58
|
+
{config.teamSizeOptions.map((opt: OnboardingOption) => (
|
|
59
|
+
<option key={opt.id} value={opt.id}>
|
|
60
|
+
{opt.label}
|
|
61
|
+
</option>
|
|
62
|
+
))}
|
|
63
|
+
</select>
|
|
64
|
+
</div>
|
|
65
|
+
)}
|
|
66
|
+
|
|
67
|
+
<PrimaryButton onClick={() => onNext({ name: name.trim(), teamSize })} disabled={!canContinue}>
|
|
68
|
+
{config.cta ?? labels.continueAction}
|
|
69
|
+
</PrimaryButton>
|
|
70
|
+
|
|
71
|
+
</QuestionWrapper>
|
|
72
|
+
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -2,6 +2,8 @@ export { WelcomeScreen } from './WelcomeScreen';
|
|
|
2
2
|
export type { WelcomeScreenProps } from './WelcomeScreen';
|
|
3
3
|
export { GoalQuestionScreen } from './GoalQuestionScreen';
|
|
4
4
|
export type { GoalQuestionScreenProps } from './GoalQuestionScreen';
|
|
5
|
+
export { OrganizationDetailsScreen } from './OrganizationDetailsScreen';
|
|
6
|
+
export type { OrganizationDetailsScreenProps } from './OrganizationDetailsScreen';
|
|
5
7
|
export { PainPointsScreen } from './PainPointsScreen';
|
|
6
8
|
export type { PainPointsScreenProps } from './PainPointsScreen';
|
|
7
9
|
export { SocialProofScreen } from './SocialProofScreen';
|
|
@@ -12,6 +12,7 @@ export type { OnboardingIntl, OnboardingIntlProviderProps } from './components/O
|
|
|
12
12
|
export {
|
|
13
13
|
WelcomeScreen,
|
|
14
14
|
GoalQuestionScreen,
|
|
15
|
+
OrganizationDetailsScreen,
|
|
15
16
|
PainPointsScreen,
|
|
16
17
|
SocialProofScreen,
|
|
17
18
|
TinderCardsScreen,
|
|
@@ -138,4 +139,6 @@ export type {
|
|
|
138
139
|
AnalyticsProvider,
|
|
139
140
|
OnboardingAnalyticsEvent,
|
|
140
141
|
OnboardingResult,
|
|
142
|
+
OrganizationDetailsData,
|
|
143
|
+
OrganizationDetailsScreenConfig,
|
|
141
144
|
} from './types';
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
export type {
|
|
1
|
+
export type {
|
|
2
|
+
OnboardingScreen,
|
|
3
|
+
OnboardingScreenType,
|
|
4
|
+
OnboardingOption,
|
|
5
|
+
OrganizationDetailsData,
|
|
6
|
+
OrganizationDetailsScreenConfig,
|
|
7
|
+
} from './screens';
|
|
2
8
|
export type {
|
|
3
9
|
OnboardingConfig,
|
|
4
10
|
OnboardingCategory,
|
|
@@ -133,6 +133,17 @@ export type ValueDeliveryScreenConfig<TResult = unknown> = BaseScreen<
|
|
|
133
133
|
|
|
134
134
|
export type SignInMethod = 'email' | 'google' | 'apple' | 'github';
|
|
135
135
|
|
|
136
|
+
export interface OrganizationDetailsData {
|
|
137
|
+
name: string;
|
|
138
|
+
teamSize?: string;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type OrganizationDetailsScreenConfig = BaseScreen<'organization-details', OrganizationDetailsData> & {
|
|
142
|
+
namePlaceholder?: string;
|
|
143
|
+
/** Omit to skip the team-size question; otherwise ids map to host labels. */
|
|
144
|
+
teamSizeOptions?: ReadonlyArray<OnboardingOption>;
|
|
145
|
+
};
|
|
146
|
+
|
|
136
147
|
export type AccountGateScreenConfig = BaseScreen<'account-gate', { signedIn: boolean; method?: SignInMethod }> & {
|
|
137
148
|
optional: boolean;
|
|
138
149
|
signInMethod?: SignInMethod;
|
|
@@ -167,6 +178,7 @@ export type PaywallScreenConfig = BaseScreen<
|
|
|
167
178
|
export type OnboardingScreen =
|
|
168
179
|
| WelcomeScreenConfig
|
|
169
180
|
| GoalQuestionScreenConfig
|
|
181
|
+
| OrganizationDetailsScreenConfig
|
|
170
182
|
| PainPointsScreenConfig
|
|
171
183
|
| SocialProofScreenConfig
|
|
172
184
|
| TinderCardsScreenConfig
|
|
@@ -7,6 +7,7 @@ import type { ValidationOutcome } from './flow-structure-check';
|
|
|
7
7
|
const RECOGNIZED_SCREEN_TYPES: ReadonlySet<OnboardingScreenType> = new Set<OnboardingScreenType>([
|
|
8
8
|
'welcome',
|
|
9
9
|
'goal-question',
|
|
10
|
+
'organization-details',
|
|
10
11
|
'pain-points',
|
|
11
12
|
'social-proof',
|
|
12
13
|
'tinder-cards',
|
|
@@ -81,8 +81,14 @@ describe('screen-config-check (§23.2)', () => {
|
|
|
81
81
|
expect(checkScreenConfig(welcome('ok'))).toEqual({ ok: true });
|
|
82
82
|
});
|
|
83
83
|
|
|
84
|
+
it('accepts the organization-details screen', () => {
|
|
85
|
+
expect(
|
|
86
|
+
checkScreenConfig({ id: 'org', type: 'organization-details', title: 'T' }),
|
|
87
|
+
).toEqual({ ok: true });
|
|
88
|
+
});
|
|
89
|
+
|
|
84
90
|
it('knows every screen type the orchestrator can render', () => {
|
|
85
|
-
expect(getRecognizedScreenTypes().size).toBe(
|
|
91
|
+
expect(getRecognizedScreenTypes().size).toBe(15);
|
|
86
92
|
});
|
|
87
93
|
});
|
|
88
94
|
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import {
|
|
4
|
+
OrganizationProvider,
|
|
5
|
+
useOrganization,
|
|
6
|
+
type OrganizationContextValue,
|
|
7
|
+
} from "./OrganizationContext";
|
|
8
|
+
import type { OrgMember, OrganizationRecord } from "./types";
|
|
9
|
+
|
|
10
|
+
const ORG: OrganizationRecord = { id: "org-1", name: "Northwind Labs", plan: "pro" };
|
|
11
|
+
|
|
12
|
+
const MEMBERS: readonly OrgMember[] = [
|
|
13
|
+
{ id: "u-1", name: "Ada Lovelace", email: "ada@example.com", role: "owner", isOwner: true },
|
|
14
|
+
{ id: "u-2", name: "Grace Hopper", email: "grace@example.com", role: "member" },
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const captured: OrganizationContextValue[] = [];
|
|
18
|
+
|
|
19
|
+
const Probe = () => {
|
|
20
|
+
captured.push(useOrganization());
|
|
21
|
+
return null;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const mount = (props: Partial<Parameters<typeof OrganizationProvider>[0]> = {}) =>
|
|
25
|
+
render(
|
|
26
|
+
<OrganizationProvider organization={ORG} members={MEMBERS} currentUserId="u-2" {...props}>
|
|
27
|
+
<Probe />
|
|
28
|
+
</OrganizationProvider>,
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const last = (): OrganizationContextValue => captured[captured.length - 1]!;
|
|
32
|
+
|
|
33
|
+
describe("OrganizationProvider", () => {
|
|
34
|
+
it("resolves the current member from the member list", () => {
|
|
35
|
+
mount();
|
|
36
|
+
expect(last().currentMember?.email).toBe("grace@example.com");
|
|
37
|
+
expect(last().currentMember?.role).toBe("member");
|
|
38
|
+
expect(last().members).toHaveLength(2);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("bridges membership role into the permission matrix", () => {
|
|
42
|
+
mount({ permissions: { matrix: { owner: ["*"], member: ["members.read"] } } });
|
|
43
|
+
expect(last().can("members.read")).toBe(true);
|
|
44
|
+
expect(last().can("members.invite")).toBe(false);
|
|
45
|
+
expect(last().canAny(["members.read", "members.remove"])).toBe(true);
|
|
46
|
+
expect(last().canAny(["members.invite"])).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("applies grant/revoke overrides on top of the matrix", () => {
|
|
50
|
+
mount({
|
|
51
|
+
permissions: {
|
|
52
|
+
matrix: { member: ["members.read"] },
|
|
53
|
+
overrides: { grant: ["members.invite"], revoke: ["members.read"] },
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
expect(last().can("members.invite")).toBe(true);
|
|
57
|
+
expect(last().can("members.read")).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("honors the wildcard grant except explicit revokes", () => {
|
|
61
|
+
mount({
|
|
62
|
+
permissions: {
|
|
63
|
+
matrix: { member: "*" },
|
|
64
|
+
overrides: { revoke: ["billing.manage"] },
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
expect(last().can("members.invite")).toBe(true);
|
|
68
|
+
expect(last().can("billing.manage")).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("denies everything without a permission bridge", () => {
|
|
72
|
+
mount();
|
|
73
|
+
expect(last().can("members.read")).toBe(false);
|
|
74
|
+
expect(last().canAny(["members.read"])).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("resolves the active organization from the multi-org list", () => {
|
|
78
|
+
mount({
|
|
79
|
+
organizations: [ORG, { id: "org-2", name: "Contoso" }],
|
|
80
|
+
activeOrganizationId: "org-2",
|
|
81
|
+
});
|
|
82
|
+
expect(last().organization.id).toBe("org-2");
|
|
83
|
+
expect(last().organization.name).toBe("Contoso");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("defaults the organization list to the single active org", () => {
|
|
87
|
+
mount();
|
|
88
|
+
expect(last().organizations).toEqual([ORG]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("switches organizations through the host callback", () => {
|
|
92
|
+
const onSwitch = vi.fn();
|
|
93
|
+
const SwitchProbe = () => {
|
|
94
|
+
const { switchOrganization } = useOrganization();
|
|
95
|
+
return (
|
|
96
|
+
<button type="button" onClick={() => switchOrganization("org-2")}>
|
|
97
|
+
switch
|
|
98
|
+
</button>
|
|
99
|
+
);
|
|
100
|
+
};
|
|
101
|
+
render(
|
|
102
|
+
<OrganizationProvider organization={ORG} onSwitchOrganization={onSwitch}>
|
|
103
|
+
<SwitchProbe />
|
|
104
|
+
</OrganizationProvider>,
|
|
105
|
+
);
|
|
106
|
+
fireEvent.click(screen.getByText("switch"));
|
|
107
|
+
expect(onSwitch).toHaveBeenCalledWith("org-2");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("throws when useOrganization runs outside the provider", () => {
|
|
111
|
+
expect(() => render(<Probe />)).toThrow(/OrganizationProvider/);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { createContext, useCallback, useContext, useMemo, type ReactNode } from "react";
|
|
2
|
+
import { PERMISSION_WILDCARD, effectivePermissions } from "../permissions";
|
|
3
|
+
import type { Permission } from "../permissions/types";
|
|
4
|
+
import type {
|
|
5
|
+
OrganizationPermissionBridge,
|
|
6
|
+
OrganizationRecord,
|
|
7
|
+
OrgMember,
|
|
8
|
+
} from "./types";
|
|
9
|
+
|
|
10
|
+
export interface OrganizationProviderProps {
|
|
11
|
+
organization: OrganizationRecord;
|
|
12
|
+
/** Omit when membership lives outside this provider's scope. */
|
|
13
|
+
members?: readonly OrgMember[];
|
|
14
|
+
/** Resolves `currentMember` from the member list by id. */
|
|
15
|
+
currentUserId?: string;
|
|
16
|
+
/** Optional permission bridge; without it `can` always resolves false. */
|
|
17
|
+
permissions?: OrganizationPermissionBridge;
|
|
18
|
+
/** Multi-organization support: pass the full org list to enable switching. */
|
|
19
|
+
organizations?: readonly OrganizationRecord[];
|
|
20
|
+
activeOrganizationId?: string;
|
|
21
|
+
onSwitchOrganization?: (organizationId: string) => void;
|
|
22
|
+
children: ReactNode;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface OrganizationContextValue {
|
|
26
|
+
organization: OrganizationRecord;
|
|
27
|
+
members: readonly OrgMember[];
|
|
28
|
+
currentMember: OrgMember | null;
|
|
29
|
+
can: (permission: Permission) => boolean;
|
|
30
|
+
canAny: (permissions: readonly Permission[]) => boolean;
|
|
31
|
+
organizations: readonly OrganizationRecord[];
|
|
32
|
+
switchOrganization: (organizationId: string) => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const OrganizationContext = createContext<OrganizationContextValue | null>(null);
|
|
36
|
+
|
|
37
|
+
export const OrganizationProvider = ({
|
|
38
|
+
organization,
|
|
39
|
+
members,
|
|
40
|
+
currentUserId,
|
|
41
|
+
permissions,
|
|
42
|
+
organizations,
|
|
43
|
+
activeOrganizationId,
|
|
44
|
+
onSwitchOrganization,
|
|
45
|
+
children,
|
|
46
|
+
}: OrganizationProviderProps) => {
|
|
47
|
+
const currentMember = useMemo(
|
|
48
|
+
() =>
|
|
49
|
+
members && currentUserId !== undefined
|
|
50
|
+
? members.find((member) => member.id === currentUserId) ?? null
|
|
51
|
+
: null,
|
|
52
|
+
[members, currentUserId],
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const resolveGrant = useMemo(() => {
|
|
56
|
+
if (!permissions) return null;
|
|
57
|
+
const role = permissions.role ?? currentMember?.role ?? null;
|
|
58
|
+
if (!role) return null;
|
|
59
|
+
const base = permissions.matrix[role];
|
|
60
|
+
if (base === undefined) return null;
|
|
61
|
+
if (base === PERMISSION_WILDCARD) return { wildcard: true as const, permissions };
|
|
62
|
+
return { wildcard: false as const, set: effectivePermissions(base, permissions.overrides) };
|
|
63
|
+
}, [permissions, currentMember]);
|
|
64
|
+
|
|
65
|
+
const can = useCallback(
|
|
66
|
+
(permission: Permission) => {
|
|
67
|
+
if (!resolveGrant) return false;
|
|
68
|
+
if (resolveGrant.wildcard) {
|
|
69
|
+
return !resolveGrant.permissions.overrides?.revoke?.includes(permission);
|
|
70
|
+
}
|
|
71
|
+
return resolveGrant.set.has(permission);
|
|
72
|
+
},
|
|
73
|
+
[resolveGrant],
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const canAny = useCallback(
|
|
77
|
+
(list: readonly Permission[]) => list.some((p) => can(p)),
|
|
78
|
+
[can],
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const switchOrganization = useCallback(
|
|
82
|
+
(organizationId: string) => onSwitchOrganization?.(organizationId),
|
|
83
|
+
[onSwitchOrganization],
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
const value = useMemo<OrganizationContextValue>(
|
|
87
|
+
() => ({
|
|
88
|
+
organization:
|
|
89
|
+
organizations && activeOrganizationId
|
|
90
|
+
? organizations.find((org) => org.id === activeOrganizationId) ?? organization
|
|
91
|
+
: organization,
|
|
92
|
+
members: members ?? [],
|
|
93
|
+
currentMember,
|
|
94
|
+
can,
|
|
95
|
+
canAny,
|
|
96
|
+
organizations: organizations ?? [organization],
|
|
97
|
+
switchOrganization,
|
|
98
|
+
}),
|
|
99
|
+
[organization, organizations, activeOrganizationId, members, currentMember, can, canAny, switchOrganization],
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
return <OrganizationContext.Provider value={value}>{children}</OrganizationContext.Provider>;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const useOrganization = (): OrganizationContextValue => {
|
|
106
|
+
const value = useContext(OrganizationContext);
|
|
107
|
+
if (!value) {
|
|
108
|
+
throw new Error("useOrganization must be used within OrganizationProvider");
|
|
109
|
+
}
|
|
110
|
+
return value;
|
|
111
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { InviteForm } from "./InviteForm";
|
|
4
|
+
import type { RoleInfo } from "../../permissions/types";
|
|
5
|
+
|
|
6
|
+
const ROLES: readonly RoleInfo[] = [
|
|
7
|
+
{ key: "admin", label: "Admin" },
|
|
8
|
+
{ key: "member", label: "Member" },
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
const mount = (props: Partial<Parameters<typeof InviteForm>[0]> = {}) =>
|
|
12
|
+
render(<InviteForm roles={ROLES} onInvite={() => undefined} {...props} />);
|
|
13
|
+
|
|
14
|
+
const submitButton = () =>
|
|
15
|
+
screen.getByText("Send invite").closest("button") as HTMLButtonElement;
|
|
16
|
+
|
|
17
|
+
describe("InviteForm", () => {
|
|
18
|
+
it("locks submit until the email is valid", () => {
|
|
19
|
+
mount();
|
|
20
|
+
expect(submitButton().disabled).toBe(true);
|
|
21
|
+
|
|
22
|
+
fireEvent.change(screen.getByLabelText("Email"), {
|
|
23
|
+
target: { value: "not-an-email" },
|
|
24
|
+
});
|
|
25
|
+
expect(submitButton().disabled).toBe(true);
|
|
26
|
+
expect(screen.getByText("Enter a valid email address")).not.toBeNull();
|
|
27
|
+
|
|
28
|
+
fireEvent.change(screen.getByLabelText("Email"), {
|
|
29
|
+
target: { value: "grace@example.com" },
|
|
30
|
+
});
|
|
31
|
+
expect(submitButton().disabled).toBe(false);
|
|
32
|
+
expect(screen.queryByText("Enter a valid email address")).toBeNull();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("submits the trimmed payload with the chosen role and clears the form", () => {
|
|
36
|
+
const onInvite = vi.fn();
|
|
37
|
+
mount({ onInvite });
|
|
38
|
+
fireEvent.change(screen.getByLabelText("Full name"), {
|
|
39
|
+
target: { value: " Grace Hopper " },
|
|
40
|
+
});
|
|
41
|
+
fireEvent.change(screen.getByLabelText("Email"), {
|
|
42
|
+
target: { value: " grace@example.com " },
|
|
43
|
+
});
|
|
44
|
+
fireEvent.change(screen.getByLabelText("Role"), { target: { value: "admin" } });
|
|
45
|
+
fireEvent.click(submitButton());
|
|
46
|
+
|
|
47
|
+
expect(onInvite).toHaveBeenCalledWith({
|
|
48
|
+
name: "Grace Hopper",
|
|
49
|
+
email: "grace@example.com",
|
|
50
|
+
role: "admin",
|
|
51
|
+
});
|
|
52
|
+
expect((screen.getByLabelText("Email") as HTMLInputElement).value).toBe("");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("omits the name key when the name is blank", () => {
|
|
56
|
+
const onInvite = vi.fn();
|
|
57
|
+
mount({ onInvite });
|
|
58
|
+
fireEvent.change(screen.getByLabelText("Email"), {
|
|
59
|
+
target: { value: "g@x.com" },
|
|
60
|
+
});
|
|
61
|
+
fireEvent.click(submitButton());
|
|
62
|
+
expect(onInvite).toHaveBeenCalledWith({ email: "g@x.com", role: "admin" });
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("locks submit while busy", () => {
|
|
66
|
+
mount({ busy: true });
|
|
67
|
+
fireEvent.change(screen.getByLabelText("Email"), {
|
|
68
|
+
target: { value: "g@x.com" },
|
|
69
|
+
});
|
|
70
|
+
expect(submitButton().disabled).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
});
|