@krak-stack/auth 0.1.21 → 0.1.22

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.
@@ -166,6 +166,8 @@ type OrganizationSwitcherProps = {
166
166
  baseUrl?: string | undefined;
167
167
  side?: ComponentProps<typeof DropdownMenuContent>["side"];
168
168
  className?: string;
169
+ defaultDialog?: OrganizationSwitcherDialog | null;
170
+ hideTrigger?: boolean;
169
171
  renderUnauthenticated?: () => ReactNode;
170
172
  locked?: boolean;
171
173
  messages?: OrganizationSwitcherMessages;
@@ -181,5 +183,5 @@ type OrganizationSummary = {
181
183
  metadata?: unknown;
182
184
  };
183
185
  export type OrganizationSwitcherDialog = "create" | "manage" | "members" | "apiKeys" | "invitations";
184
- export declare function OrganizationSwitcher({ authClient, baseUrl, side, className, renderUnauthenticated, locked, messages, dialog: controlledDialog, onChange, onCreate, onDialogChange, }: OrganizationSwitcherProps): import("react").JSX.Element;
186
+ export declare function OrganizationSwitcher({ authClient, baseUrl, side, className, defaultDialog, hideTrigger, renderUnauthenticated, locked, messages, dialog: controlledDialog, onChange, onCreate, onDialogChange, }: OrganizationSwitcherProps): import("react").JSX.Element;
185
187
  export {};
@@ -342,13 +342,13 @@ const organizationMetadataFromForm = async (authClient, value, m) => {
342
342
  }
343
343
  return { translations };
344
344
  };
345
- export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", className, renderUnauthenticated, locked = false, messages, dialog: controlledDialog, onChange, onCreate, onDialogChange, }) {
345
+ export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", className, defaultDialog = null, hideTrigger = false, renderUnauthenticated, locked = false, messages, dialog: controlledDialog, onChange, onCreate, onDialogChange, }) {
346
346
  const labels = organizationSwitcherMessages(messages);
347
347
  const m = organizationMessageFns(labels);
348
348
  const session = authClient.useSession();
349
349
  const organizations = authClient.useListOrganizations();
350
350
  const activeOrganization = authClient.useActiveOrganization();
351
- const [uncontrolledDialog, setUncontrolledDialog] = useState(null);
351
+ const [uncontrolledDialog, setUncontrolledDialog] = useState(defaultDialog);
352
352
  const dialog = controlledDialog !== undefined ? controlledDialog : uncontrolledDialog;
353
353
  const sessionUserId = session.data?.user.id;
354
354
  const [userInvitations, setUserInvitations] = useState([]);
@@ -376,6 +376,7 @@ export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", cla
376
376
  setUncontrolledDialog(nextDialog);
377
377
  onDialogChange?.(nextDialog);
378
378
  };
379
+ const openCreate = () => setDialog("create");
379
380
  useEffect(() => {
380
381
  if (!sessionUserId)
381
382
  return;
@@ -409,7 +410,7 @@ export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", cla
409
410
  }
410
411
  };
411
412
  const activeDisplay = organizationDisplay(active ?? null, m, baseUrl);
412
- return (_jsxs(OrganizationMessagesContext.Provider, { value: m, children: [_jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { render: _jsxs(Button, { variant: "ghost", className: cn("h-11 w-full justify-between gap-2 !px-1 !py-0 group-data-[collapsible=icon]:size-8 group-data-[collapsible=icon]:justify-start group-data-[collapsible=icon]:!p-0", className), children: [_jsx(AppBrand, { to: null, label: activeName
413
+ return (_jsxs(OrganizationMessagesContext.Provider, { value: m, children: [hideTrigger ? null : (_jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { render: _jsxs(Button, { variant: "ghost", className: cn("h-11 w-full justify-between gap-2 !px-1 !py-0 group-data-[collapsible=icon]:size-8 group-data-[collapsible=icon]:justify-start group-data-[collapsible=icon]:!p-0", className), children: [_jsx(AppBrand, { to: null, label: activeName
413
414
  ? activeDisplay.name
414
415
  : m.organization_switcher_label(), subtitle: activeDisplay.subtitle, icon: Building2, variant: "sidebar", className: "min-w-0 flex-1 !p-0 text-left group-data-[collapsible=icon]:flex-none group-data-[collapsible=icon]:justify-start", ...(activeDisplay.image
415
416
  ? { imageSrc: activeDisplay.image }
@@ -428,20 +429,21 @@ export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", cla
428
429
  }, children: _jsx(AppBrand, { to: null, label: display.name, subtitle: display.subtitle, icon: Building2, className: "w-full text-left [&>div:first-child]:size-7", ...(display.image ? { imageSrc: display.image } : {}) }) }, organization.id));
429
430
  })) : !locked && !organizations.isPending ? (_jsx(DropdownMenuItem, { disabled: true, children: active
430
431
  ? m.organization_switcher_no_other_organizations()
431
- : m.organization_switcher_empty() })) : null, _jsx(DropdownMenuSeparator, {}), !locked ? (_jsxs(DropdownMenuItem, { onClick: () => setDialog("create"), children: [_jsx(Plus, {}), m.organization_create_title()] })) : null, _jsxs(DropdownMenuItem, { onClick: () => {
432
+ : m.organization_switcher_empty() })) : null, _jsx(DropdownMenuSeparator, {}), !locked ? (_jsxs(DropdownMenuItem, { onClick: openCreate, children: [_jsx(Plus, {}), m.organization_create_title()] })) : null, _jsxs(DropdownMenuItem, { onClick: () => {
432
433
  setDialog("invitations");
433
434
  void loadUserInvitations();
434
- }, children: [_jsx(Mail, {}), _jsxs("span", { className: "flex flex-1 items-center justify-between gap-3", children: [m.organization_user_invitations_title(), userInvitations.length ? (_jsx(Badge, { variant: "secondary", children: userInvitations.length })) : null] })] }), activeOrganization.data ? (_jsxs(_Fragment, { children: [_jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { onClick: () => setDialog("manage"), children: [_jsx(PencilIcon, {}), m.organization_switcher_manage()] }), _jsxs(DropdownMenuItem, { onClick: () => setDialog("members"), children: [_jsx(Users, {}), m.organization_members_title()] }), _jsxs(DropdownMenuItem, { onClick: () => setDialog("apiKeys"), children: [_jsx(KeyRound, {}), m.user_button_api_keys()] })] })) : null] }) })] }), _jsx(Dialog, { open: dialog === "create", onOpenChange: (open) => {
435
+ }, children: [_jsx(Mail, {}), _jsxs("span", { className: "flex flex-1 items-center justify-between gap-3", children: [m.organization_user_invitations_title(), userInvitations.length ? (_jsx(Badge, { variant: "secondary", children: userInvitations.length })) : null] })] }), activeOrganization.data ? (_jsxs(_Fragment, { children: [_jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { onClick: () => setDialog("manage"), children: [_jsx(PencilIcon, {}), m.organization_switcher_manage()] }), _jsxs(DropdownMenuItem, { onClick: () => setDialog("members"), children: [_jsx(Users, {}), m.organization_members_title()] }), _jsxs(DropdownMenuItem, { onClick: () => setDialog("apiKeys"), children: [_jsx(KeyRound, {}), m.user_button_api_keys()] })] })) : null] }) })] })), _jsx(Dialog, { open: dialog === "create", onOpenChange: (open) => {
435
436
  setDialog((current) => open ? "create" : current === "create" ? null : current);
436
437
  }, children: _jsxs(DialogContent, { className: "max-h-[85vh] overflow-y-auto sm:max-w-xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.organization_create_title() }), _jsx(DialogDescription, { children: m.organization_create_description() })] }), _jsx(Separator, {}), _jsx(CreateOrganizationSection, { authClient: authClient, baseUrl: baseUrl, onCreated: async (organization) => {
437
- onCreate?.(organization);
438
438
  setDialog(null);
439
439
  const activeResult = await authClient.organization.setActive({
440
440
  organizationId: organization.id,
441
441
  });
442
442
  await refresh();
443
- if (!activeResult.error)
443
+ if (!activeResult.error) {
444
+ onCreate?.(organization);
444
445
  onChange?.(organization);
446
+ }
445
447
  } })] }) }), _jsx(Dialog, { open: dialog === "manage", onOpenChange: (open) => {
446
448
  setDialog((current) => open ? "manage" : current === "manage" ? null : current);
447
449
  }, children: _jsxs(DialogContent, { className: "max-h-[85vh] overflow-y-auto sm:max-w-xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.organization_switcher_manage() }), _jsx(DialogDescription, { children: m.organization_edit_description() })] }), _jsx(Separator, {}), activeOrganization.data ? (_jsx(EditOrganizationSection, { authClient: authClient, baseUrl: baseUrl, organization: activeOrganization.data, onUpdated: refresh })) : null] }) }), _jsx(Dialog, { open: dialog === "members", onOpenChange: (open) => {
@@ -172,6 +172,8 @@ type UserDropdownProps = {
172
172
  baseUrl?: string | undefined;
173
173
  signOutRedirect?: string;
174
174
  side?: ComponentProps<typeof DropdownMenuContent>["side"];
175
+ defaultDialog?: UserButtonDialog | null;
176
+ hideTrigger?: boolean;
175
177
  renderUnauthenticated?: () => ReactNode;
176
178
  apiKeyPermissions?: Record<string, string[]>;
177
179
  messages?: UserButtonMessages;
@@ -179,5 +181,5 @@ type UserDropdownProps = {
179
181
  onDialogChange?: (dialog: UserButtonDialog | null) => void;
180
182
  };
181
183
  export type UserButtonDialog = "account" | "security" | "apiKeys";
182
- export declare const UserButton: ({ authClient, baseUrl, signOutRedirect, side, renderUnauthenticated, apiKeyPermissions, messages, dialog: controlledDialog, onDialogChange, }: UserDropdownProps) => import("react").JSX.Element;
184
+ export declare const UserButton: ({ authClient, baseUrl, signOutRedirect, side, defaultDialog, hideTrigger, renderUnauthenticated, apiKeyPermissions, messages, dialog: controlledDialog, onDialogChange, }: UserDropdownProps) => import("react").JSX.Element;
183
185
  export {};
@@ -186,19 +186,16 @@ const getLocale = () => (globalThis.document?.documentElement.lang ||
186
186
  .startsWith("fr")
187
187
  ? "fr"
188
188
  : "en";
189
- const interpolate = (value, params) => params
190
- ? value.replace(/\{([^}]+)\}/g, (_, key) => String(params[key] ?? `{${key}}`))
191
- : value;
192
189
  const userButtonMessages = (overrides) => ({
193
190
  ...(getLocale().startsWith("fr") ? messages.fr : messages.en),
194
191
  ...overrides,
195
192
  });
196
- const userButtonMessageFns = (labels) => {
197
- const localized = labels;
198
- return new Proxy({}, {
199
- get: (_target, key) => (params) => interpolate(localized[key], params),
200
- });
201
- };
193
+ const interpolate = (value, params) => params
194
+ ? value.replace(/\{([^}]+)\}/g, (_, key) => String(params[key] ?? `{${key}}`))
195
+ : value;
196
+ const userButtonMessageFns = (labels) => new Proxy({}, {
197
+ get: (_target, key) => (params) => interpolate(labels[key], params),
198
+ });
202
199
  const UserButtonMessagesContext = createContext(userButtonMessageFns(userButtonMessages()));
203
200
  const useUserButtonMessages = () => useContext(UserButtonMessagesContext);
204
201
  const isFile = (value) => value instanceof File;
@@ -218,7 +215,7 @@ const presignUserImageUpload = async (authClient, file, m) => {
218
215
  }
219
216
  return { uploadUrl, url };
220
217
  };
221
- export const UserButton = ({ authClient, baseUrl, signOutRedirect = "/", side = "bottom", renderUnauthenticated, apiKeyPermissions, messages, dialog: controlledDialog, onDialogChange, }) => {
218
+ export const UserButton = ({ authClient, baseUrl, signOutRedirect = "/", side = "bottom", defaultDialog = null, hideTrigger = false, renderUnauthenticated, apiKeyPermissions, messages, dialog: controlledDialog, onDialogChange, }) => {
222
219
  const labels = userButtonMessages(messages);
223
220
  const m = userButtonMessageFns(labels);
224
221
  const navigate = useNavigate();
@@ -226,7 +223,7 @@ export const UserButton = ({ authClient, baseUrl, signOutRedirect = "/", side =
226
223
  select: (state) => `${import.meta.env.VITE_SITE_URL}${state.location.href}`,
227
224
  });
228
225
  const { data: session, isPending, refetch } = authClient.useSession();
229
- const [uncontrolledDialog, setUncontrolledDialog] = useState(null);
226
+ const [uncontrolledDialog, setUncontrolledDialog] = useState(defaultDialog);
230
227
  const settingsDialog = controlledDialog !== undefined ? controlledDialog : uncontrolledDialog;
231
228
  const [formError, setFormError] = useState(null);
232
229
  const setSettingsDialog = (next) => {
@@ -279,7 +276,7 @@ export const UserButton = ({ authClient, baseUrl, signOutRedirect = "/", side =
279
276
  await refetch();
280
277
  setSettingsDialog(null);
281
278
  };
282
- return (_jsxs(UserButtonMessagesContext.Provider, { value: m, children: [_jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { render: _jsxs(Button, { variant: "outline", size: "icon", children: [_jsx(UserIcon, { className: "size-4.5" }), _jsx("span", { className: "sr-only", children: m.user_button_aria_label() })] }) }), _jsx(DropdownMenuContent, { className: "w-56 rounded-lg", side: side, align: "end", sideOffset: 4, children: _jsxs(DropdownMenuGroup, { children: [_jsx(DropdownMenuLabel, { className: "p-0 font-normal", children: _jsx(AppBrand, { to: null, label: displayName || displayEmail, subtitle: displayName ? displayEmail : m.user_button_account(), icon: UserIcon, className: "px-1 py-1.5 text-left text-sm", ...(displayImage ? { imageSrc: displayImage } : {}) }) }), _jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { disabled: isPending, onClick: () => setSettingsDialog("account"), children: [_jsx(UserCircleIcon, {}), m.user_button_account()] }), _jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("security"), children: [_jsx(ShieldCheck, {}), m.user_button_security()] }), _jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("apiKeys"), children: [_jsx(KeyRound, {}), m.user_button_api_keys()] }), _jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { onClick: signOut, children: [_jsx(LogOutIcon, {}), m.user_button_logout()] })] }) })] }), _jsx(Dialog, { open: settingsDialog === "account", onOpenChange: (open) => {
279
+ return (_jsxs(UserButtonMessagesContext.Provider, { value: m, children: [hideTrigger ? null : (_jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { render: _jsxs(Button, { variant: "outline", size: "icon", children: [_jsx(UserIcon, { className: "size-4.5" }), _jsx("span", { className: "sr-only", children: m.user_button_aria_label() })] }) }), _jsx(DropdownMenuContent, { className: "w-56 rounded-lg", side: side, align: "end", sideOffset: 4, children: _jsxs(DropdownMenuGroup, { children: [_jsx(DropdownMenuLabel, { className: "p-0 font-normal", children: _jsx(AppBrand, { to: null, label: displayName || displayEmail, subtitle: displayName ? displayEmail : m.user_button_account(), icon: UserIcon, className: "px-1 py-1.5 text-left text-sm", ...(displayImage ? { imageSrc: displayImage } : {}) }) }), _jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { disabled: isPending, onClick: () => setSettingsDialog("account"), children: [_jsx(UserCircleIcon, {}), m.user_button_account()] }), _jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("security"), children: [_jsx(ShieldCheck, {}), m.user_button_security()] }), _jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("apiKeys"), children: [_jsx(KeyRound, {}), m.user_button_api_keys()] }), _jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { onClick: signOut, children: [_jsx(LogOutIcon, {}), m.user_button_logout()] })] }) })] })), _jsx(Dialog, { open: settingsDialog === "account", onOpenChange: (open) => {
283
280
  setSettingsDialog((current) => open ? "account" : current === "account" ? null : current);
284
281
  }, children: _jsxs(DialogContent, { className: "max-h-[85vh] overflow-y-auto sm:max-w-3xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.user_form_title() }), _jsx(DialogDescription, { children: m.user_form_description() })] }), _jsx(Separator, {}), isPending ? (_jsx("p", { className: "text-muted-foreground text-sm", children: m.user_loading() })) : (_jsxs("div", { className: "flex flex-col gap-6", children: [_jsx(UserForm, { defaultValues: {
285
282
  name: displayName ?? "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krak-stack/auth",
3
- "version": "0.1.21",
3
+ "version": "0.1.22",
4
4
  "description": "Effect HttpApi clients and schemas for KrakStack Auth.",
5
5
  "license": "MIT",
6
6
  "files": [