rowboat/apps/rowboat/app/projects/lib/project-creation-utils.ts
arkml 9a980f2f9f
Project modals (#190)
* added the project create and select modals

* Added logo and link to home page

* minor changes to side bar

* added import json and blank template options

* added website like home page

* change homepage to be more like website

* fixed landing page text box

* minor size changes to the home page

* added prebuilt agents section

* Minor changes to the prebuilt agent card

* removed the project selection and new project options from side bar

* fixed prebuilt agents

* fixed import json

* my assistants has pagination

* add dark mode support to home page

* addressed review comments

* increase build agents textbox lines

* fixed PR comments

* move prebuilt assistant under build view

* minor changes to home page

* minor changes to home page

* removed my assistants from side bar

* removed sidebar items

* fixed review comments

* fixed review comments

* Add "use client" directive to project-creation-utils.ts

This file uses localStorage and calls server actions directly, requiring client-side execution.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix import of pipeline agents

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-11 23:46:30 +05:30

121 lines
No EOL
3.2 KiB
TypeScript

"use client";
import { createProject, createProjectFromWorkflowJson } from "@/app/actions/project_actions";
export interface CreateProjectOptions {
name: string;
template?: string;
prompt?: string;
router: any; // NextJS router instance
onSuccess?: (projectId: string) => void;
onError?: (error: any) => void;
}
export interface CreateProjectFromJsonOptions {
name: string;
workflowJson: string;
router: any; // NextJS router instance
onSuccess?: (projectId: string) => void;
onError?: (error: any) => void;
}
/**
* Consolidated function to create a project with consistent error handling and navigation
*/
export async function createProjectWithOptions(options: CreateProjectOptions): Promise<void> {
try {
const formData = new FormData();
formData.append('name', options.name);
if (options.template) {
formData.append('template', options.template);
}
const response = await createProject(formData);
if ('id' in response) {
// Store prompt in localStorage if provided
if (options.prompt?.trim()) {
localStorage.setItem(`project_prompt_${response.id}`, options.prompt);
}
// Call success callback if provided
if (options.onSuccess) {
options.onSuccess(response.id);
}
// Navigate to workflow page
options.router.push(`/projects/${response.id}/workflow`);
} else {
// Handle error response
const error = (response as any).billingError || 'Failed to create project';
if (options.onError) {
options.onError(error);
} else {
throw new Error(error);
}
}
} catch (error) {
console.error('Error creating project:', error);
if (options.onError) {
options.onError(error);
} else {
throw error;
}
}
}
/**
* Consolidated function to create a project from JSON workflow
*/
export async function createProjectFromJsonWithOptions(options: CreateProjectFromJsonOptions): Promise<void> {
try {
const formData = new FormData();
formData.append('name', options.name);
formData.append('workflowJson', options.workflowJson);
const response = await createProjectFromWorkflowJson(formData);
if ('id' in response) {
// Call success callback if provided
if (options.onSuccess) {
options.onSuccess(response.id);
}
// Navigate to workflow page
options.router.push(`/projects/${response.id}/workflow`);
} else {
// Handle error response
const error = (response as any).billingError || 'Failed to create project';
if (options.onError) {
options.onError(error);
} else {
throw new Error(error);
}
}
} catch (error) {
console.error('Error creating project from JSON:', error);
if (options.onError) {
options.onError(error);
} else {
throw error;
}
}
}
/**
* Consolidated function to create a project from template selection
*/
export async function createProjectFromTemplate(
templateId: string,
templateName: string,
router: any,
onError?: (error: any) => void
): Promise<void> {
return createProjectWithOptions({
name: templateName,
template: templateId,
router,
onError
});
}