fix(onboarding): trim project name, standardize error narrowing in org/project hooks

- use-create-project.ts: Add trimming of project name at mutation start and validate empty name
- use-create-project.ts: Replace all name references with trimmed value in action calls and context updates
- use-create-org.ts: Standardize error narrowing by extracting result.data to local variables instead of using 'as' casts

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Théo LAGACHE
2026-06-19 10:40:25 +02:00
co-authored by Claude Sonnet 4.6
parent afd0894c24
commit 1fc04d3b0b
2 changed files with 11 additions and 9 deletions
@@ -23,18 +23,18 @@ export const useCreateOrg = () => {
organizationId: existingOrg.id,
data: { name: trimmed, slug: slugify(trimmed), users: [] },
});
if (!result?.data?.success) {
const err = result?.data as { success: false; actionError?: any };
throw new Error(err?.actionError?.message ?? "Failed to update organisation");
const updateData = result?.data;
if (!updateData?.success) {
throw new Error(updateData?.actionError?.message ?? "Failed to update organisation");
}
await updateContext({
flowData: { ...state?.context.flowData, org: { id: existingOrg.id, name: trimmed } },
});
} else {
const result = await createOrganizationAction({ name: trimmed });
if (!result?.data?.success) {
const err = result?.data as { success: false; actionError?: any };
throw new Error(err?.actionError?.message ?? "Failed to create organisation");
const createData = result?.data;
if (!createData?.success) {
throw new Error(createData?.actionError?.message ?? "Failed to create organisation");
}
const org = result.data.value;
if (!org) throw new Error("Failed to create organisation");
@@ -16,13 +16,15 @@ export const useCreateProject = () => {
return useMutation({
mutationFn: async ({ name, description, databaseIds }: ProjectInput) => {
const trimmed = name.trim();
if (!trimmed) throw new Error("Project name is required");
const orgId = (state?.context.flowData.org as any)?.id as string | undefined;
if (!orgId) throw new Error("No organisation ID found");
const existingProject = state?.context.flowData.project as OnboardingProjectData | undefined;
if (existingProject?.id) {
const result = await updateProjectAction({
data: { name, databases: databaseIds },
data: { name: trimmed, databases: databaseIds },
organizationId: orgId,
projectId: existingProject.id,
});
@@ -33,12 +35,12 @@ export const useCreateProject = () => {
await updateContext({
flowData: {
...state?.context.flowData,
project: { id: existingProject.id, name, description, databaseIds },
project: { id: existingProject.id, name: trimmed, description, databaseIds },
},
});
} else {
const result = await createProjectAction({
data: { name, databases: databaseIds },
data: { name: trimmed, databases: databaseIds },
organizationId: orgId,
});
const createData = result?.data;