-
Notifications
You must be signed in to change notification settings - Fork 418
feat(ui,clerk-js,shared): Surface organization creation defaults #7488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
05b9250
7ca2f6c
945f86c
986368f
ea22991
52941a9
c5fcb39
41e096f
33bc7ba
22edba6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import type { | ||
| OrganizationCreationAdvisorySeverity, | ||
| OrganizationCreationAdvisoryType, | ||
| OrganizationCreationDefaultsJSON, | ||
| OrganizationCreationDefaultsJSONSnapshot, | ||
| OrganizationCreationDefaultsResource, | ||
| } from '@clerk/shared/types'; | ||
|
|
||
| import { BaseResource } from './internal'; | ||
|
|
||
| export class OrganizationCreationDefaults extends BaseResource implements OrganizationCreationDefaultsResource { | ||
| advisory: { | ||
| code: OrganizationCreationAdvisoryType; | ||
| severity: OrganizationCreationAdvisorySeverity; | ||
| meta: Record<string, string>; | ||
| } | null = null; | ||
| form: { | ||
| name: string; | ||
| slug: string; | ||
| logo: string | null; | ||
| } = { | ||
| name: '', | ||
| slug: '', | ||
| logo: null, | ||
| }; | ||
|
|
||
| public constructor(data: OrganizationCreationDefaultsJSON | OrganizationCreationDefaultsJSONSnapshot | null = null) { | ||
| super(); | ||
| this.fromJSON(data); | ||
| } | ||
|
|
||
| protected fromJSON(data: OrganizationCreationDefaultsJSON | OrganizationCreationDefaultsJSONSnapshot | null): this { | ||
| if (!data) { | ||
| return this; | ||
| } | ||
|
|
||
| if (data.advisory) { | ||
| this.advisory = this.withDefault(data.advisory, this.advisory ?? null); | ||
| } | ||
|
|
||
| if (data.form) { | ||
| this.form.name = this.withDefault(data.form.name, this.form.name); | ||
| this.form.slug = this.withDefault(data.form.slug, this.form.slug); | ||
| this.form.logo = this.withDefault(data.form.logo, this.form.logo); | ||
| } | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| static async retrieve(): Promise<OrganizationCreationDefaultsResource> { | ||
| return await BaseResource._fetch({ | ||
| path: '/me/organization_creation_defaults', | ||
| method: 'GET', | ||
| }).then(res => { | ||
| const data = res?.response as unknown as OrganizationCreationDefaultsJSON; | ||
| return new OrganizationCreationDefaults(data); | ||
| }); | ||
| } | ||
|
|
||
| public __internal_toSnapshot(): OrganizationCreationDefaultsJSONSnapshot { | ||
| return { | ||
| advisory: this.advisory | ||
| ? { | ||
| code: this.advisory.code, | ||
| meta: this.advisory.meta, | ||
| severity: this.advisory.severity, | ||
| } | ||
| : null, | ||
| } as unknown as OrganizationCreationDefaultsJSONSnapshot; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -877,6 +877,10 @@ export const enUS: LocalizationResource = { | |
| actionLink: 'Sign out', | ||
| actionText: 'Signed in as {{identifier}}', | ||
| }, | ||
| alerts: { | ||
| existingOrgWithDomain: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will generate the other locale files once I get a first PR review, in case the localization key changes |
||
| 'An organization already exists for the detected company name and {{email}}. Join by invitation.', | ||
| }, | ||
| }, | ||
| taskResetPassword: { | ||
| formButtonPrimary: 'Reset Password', | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { ClerkResourceJSON } from './json'; | ||
| import type { ClerkResource } from './resource'; | ||
|
|
||
| export type OrganizationCreationAdvisoryType = 'existing_org_with_domain'; | ||
|
|
||
| export type OrganizationCreationAdvisorySeverity = 'warning'; | ||
|
|
||
| export interface OrganizationCreationDefaultsJSON extends ClerkResourceJSON { | ||
| advisory: { | ||
| code: OrganizationCreationAdvisoryType; | ||
| severity: OrganizationCreationAdvisorySeverity; | ||
| meta: Record<string, string>; | ||
| } | null; | ||
| form: { | ||
| name: string; | ||
| slug: string; | ||
| logo: string | null; | ||
| }; | ||
| } | ||
|
|
||
| export interface OrganizationCreationDefaultsResource extends ClerkResource { | ||
| advisory: { | ||
| code: OrganizationCreationAdvisoryType; | ||
| severity: OrganizationCreationAdvisorySeverity; | ||
| meta: Record<string, string>; | ||
| } | null; | ||
| form: { | ||
| name: string; | ||
| slug: string; | ||
| logo: string | null; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,22 +1,28 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useOrganizationList } from '@clerk/shared/react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { CreateOrganizationParams } from '@clerk/shared/types'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { CreateOrganizationParams, OrganizationCreationDefaultsResource } from '@clerk/shared/types'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useState } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useEnvironment } from '@/ui/contexts'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useSessionTasksContext, useTaskChooseOrganizationContext } from '@/ui/contexts/components/SessionTasks'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { localizationKeys } from '@/ui/customizables'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Icon, localizationKeys } from '@/ui/customizables'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useCardState } from '@/ui/elements/contexts'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Form } from '@/ui/elements/Form'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { FormButtonContainer } from '@/ui/elements/FormButtons'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { FormContainer } from '@/ui/elements/FormContainer'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Header } from '@/ui/elements/Header'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { IconButton } from '@/ui/elements/IconButton'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Upload } from '@/ui/icons'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { createSlug } from '@/ui/utils/createSlug'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { handleError } from '@/ui/utils/errorHandler'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useFormControl } from '@/ui/utils/useFormControl'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| import { OrganizationProfileAvatarUploader } from '../../../OrganizationProfile/OrganizationProfileAvatarUploader'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { organizationListParams } from '../../../OrganizationSwitcher/utils'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { OrganizationCreationDefaultsAlert } from './OrganizationCreationDefaultsAlert'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| type CreateOrganizationScreenProps = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| onCancel?: () => void; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| organizationCreationDefaults?: OrganizationCreationDefaultsResource; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| export const CreateOrganizationScreen = (props: CreateOrganizationScreenProps) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -27,13 +33,14 @@ export const CreateOrganizationScreen = (props: CreateOrganizationScreenProps) = | |||||||||||||||||||||||||||||||||||||||||||||||||
| userMemberships: organizationListParams.userMemberships, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const { organizationSettings } = useEnvironment(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const [file, setFile] = useState<File | null>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| const nameField = useFormControl('name', '', { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const nameField = useFormControl('name', props.organizationCreationDefaults?.form.name ?? '', { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| type: 'text', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| label: localizationKeys('taskChooseOrganization.createOrganization.formFieldLabel__name'), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| placeholder: localizationKeys('taskChooseOrganization.createOrganization.formFieldInputPlaceholder__name'), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const slugField = useFormControl('slug', '', { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const slugField = useFormControl('slug', props.organizationCreationDefaults?.form.slug ?? '', { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| type: 'text', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| label: localizationKeys('taskChooseOrganization.createOrganization.formFieldLabel__slug'), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| placeholder: localizationKeys('taskChooseOrganization.createOrganization.formFieldInputPlaceholder__slug'), | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -57,6 +64,15 @@ export const CreateOrganizationScreen = (props: CreateOrganizationScreenProps) = | |||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| const organization = await createOrganization(createOrgParams); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if (file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| await organization.setLogo({ file }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (defaultLogoUrl) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const response = await fetch(defaultLogoUrl); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const blob = await response.blob(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const logoFile = new File([blob], 'logo', { type: blob.type }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| await organization.setLogo({ file: logoFile }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+67
to
+74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing error handling for logo fetch could break organization creation. The fetch and blob conversion for 🔎 Proposed fix with error handling const organization = await createOrganization(createOrgParams);
if (file) {
await organization.setLogo({ file });
} else if (defaultLogoUrl) {
- const response = await fetch(defaultLogoUrl);
- const blob = await response.blob();
- const logoFile = new File([blob], 'logo', { type: blob.type });
- await organization.setLogo({ file: logoFile });
+ try {
+ const response = await fetch(defaultLogoUrl);
+ if (!response.ok) {
+ throw new Error('Failed to fetch logo');
+ }
+ const blob = await response.blob();
+ const logoFile = new File([blob], 'logo', { type: blob.type });
+ await organization.setLogo({ file: logoFile });
+ } catch (err) {
+ // Log error but don't block organization creation
+ console.error('Failed to set default logo:', err);
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| await setActive({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| organization, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| navigate: async ({ session }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -77,7 +93,13 @@ export const CreateOrganizationScreen = (props: CreateOrganizationScreenProps) = | |||||||||||||||||||||||||||||||||||||||||||||||||
| slugField.setValue(val); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| const onAvatarRemove = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| card.setIdle(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return setFile(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| const isSubmitButtonDisabled = !nameField.value || !isLoaded; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const defaultLogoUrl = file === undefined ? props.organizationCreationDefaults?.form.logo : undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <> | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -88,8 +110,46 @@ export const CreateOrganizationScreen = (props: CreateOrganizationScreenProps) = | |||||||||||||||||||||||||||||||||||||||||||||||||
| <Header.Title localizationKey={localizationKeys('taskChooseOrganization.createOrganization.title')} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <Header.Subtitle localizationKey={localizationKeys('taskChooseOrganization.createOrganization.subtitle')} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| </Header.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| <FormContainer sx={t => ({ padding: `${t.space.$none} ${t.space.$10} ${t.space.$8}` })}> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <Form.Root onSubmit={onSubmit}> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <OrganizationCreationDefaultsAlert organizationCreationDefaults={props.organizationCreationDefaults} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <OrganizationProfileAvatarUploader | ||||||||||||||||||||||||||||||||||||||||||||||||||
| organization={{ name: nameField.value, imageUrl: defaultLogoUrl ?? undefined }} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| onAvatarChange={async file => await setFile(file)} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| onAvatarRemove={file || defaultLogoUrl ? onAvatarRemove : null} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| avatarPreviewPlaceholder={ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <IconButton | ||||||||||||||||||||||||||||||||||||||||||||||||||
| variant='ghost' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| aria-label='Upload organization logo' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| icon={ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <Icon | ||||||||||||||||||||||||||||||||||||||||||||||||||
| size='md' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| icon={Upload} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sx={t => ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| color: t.colors.$colorMutedForeground, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| transitionDuration: t.transitionDuration.$controls, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| })} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sx={t => ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| width: t.sizes.$16, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| height: t.sizes.$16, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| borderRadius: t.radii.$md, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| borderWidth: t.borderWidths.$normal, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| borderStyle: t.borderStyles.$dashed, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| borderColor: t.colors.$borderAlpha200, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| backgroundColor: t.colors.$neutralAlpha50, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ':hover': { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| backgroundColor: t.colors.$neutralAlpha50, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| svg: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| transform: 'scale(1.2)', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| })} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <Form.ControlRow elementId={nameField.id}> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <Form.PlainInput | ||||||||||||||||||||||||||||||||||||||||||||||||||
| {...nameField.props} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsafe type casting bypasses validation and could cause runtime errors.
Line 55 uses
as unknown as OrganizationCreationDefaultsJSONto force-cast the API response without any validation. If the API returns malformed data or the response structure changes, this will bypass type safety and could cause runtime errors when the resource properties are accessed.🔎 Proposed fix with runtime validation
static async retrieve(): Promise<OrganizationCreationDefaultsResource> { return await BaseResource._fetch({ path: '/me/organization_creation_defaults', method: 'GET', }).then(res => { - const data = res?.response as unknown as OrganizationCreationDefaultsJSON; + const data = res?.response; + // Basic runtime validation + if (!data || typeof data !== 'object') { + throw new Error('Invalid organization creation defaults response'); + } return new OrganizationCreationDefaults(data); }); }🤖 Prompt for AI Agents