akhaliq HF Staff commited on
Commit
6b76117
Β·
1 Parent(s): 9863dfe

support PRs

Browse files
frontend/src/app/page.tsx CHANGED
@@ -22,6 +22,7 @@ export default function Home() {
22
  const [isAuthenticated, setIsAuthenticated] = useState(false);
23
  const [currentRepoId, setCurrentRepoId] = useState<string | null>(null); // Track imported/deployed space
24
  const [username, setUsername] = useState<string | null>(null); // Track current user
 
25
 
26
  // Landing page state - show landing page if no messages exist
27
  const [showLandingPage, setShowLandingPage] = useState(true);
@@ -341,6 +342,13 @@ export default function Home() {
341
  };
342
  return newMessages;
343
  });
 
 
 
 
 
 
 
344
  },
345
  // onError
346
  (error: string) => {
@@ -417,6 +425,61 @@ export default function Home() {
417
  }
418
  };
419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
  const handleDeploy = async () => {
421
  console.log('[Deploy] 🎬 handleDeploy called');
422
  console.log('[Deploy] generatedCode exists?', !!generatedCode);
@@ -713,12 +776,19 @@ export default function Home() {
713
  };
714
 
715
  // Handle landing page prompt submission
716
- const handleLandingPageStart = async (prompt: string, language: Language, modelId: string, repoId?: string) => {
717
  // Hide landing page immediately for smooth transition
718
  setShowLandingPage(false);
 
 
 
 
 
 
 
719
  // Send the message with the selected language and model
720
- // Pass repoId if provided (for imported/duplicated spaces)
721
- await handleSendMessage(prompt, language, modelId, repoId);
722
  };
723
 
724
  // Resize handlers for chat sidebar (desktop only)
 
22
  const [isAuthenticated, setIsAuthenticated] = useState(false);
23
  const [currentRepoId, setCurrentRepoId] = useState<string | null>(null); // Track imported/deployed space
24
  const [username, setUsername] = useState<string | null>(null); // Track current user
25
+ const [pendingPR, setPendingPR] = useState<{ repoId: string; language: Language } | null>(null); // Track pending PR after redesign
26
 
27
  // Landing page state - show landing page if no messages exist
28
  const [showLandingPage, setShowLandingPage] = useState(true);
 
342
  };
343
  return newMessages;
344
  });
345
+
346
+ // Check if we need to create a PR (redesign with PR option)
347
+ if (pendingPR) {
348
+ console.log('[PR] Creating pull request for:', pendingPR.repoId);
349
+ createPullRequestAfterGeneration(pendingPR.repoId, code, pendingPR.language);
350
+ setPendingPR(null); // Clear pending PR
351
+ }
352
  },
353
  // onError
354
  (error: string) => {
 
425
  }
426
  };
427
 
428
+ const createPullRequestAfterGeneration = async (repoId: string, code: string, language: Language) => {
429
+ try {
430
+ console.log('[PR] Creating PR on:', repoId);
431
+
432
+ // Update message to show PR creation in progress
433
+ setMessages((prev) => {
434
+ const newMessages = [...prev];
435
+ newMessages[newMessages.length - 1] = {
436
+ ...newMessages[newMessages.length - 1],
437
+ content: 'βœ… Code generated successfully!\n\nπŸ”„ Creating Pull Request...',
438
+ };
439
+ return newMessages;
440
+ });
441
+
442
+ const prResult = await apiClient.createPullRequest(
443
+ repoId,
444
+ code,
445
+ language,
446
+ '🎨 Redesign from AnyCoder',
447
+ undefined
448
+ );
449
+
450
+ if (prResult.success && prResult.pr_url) {
451
+ console.log('[PR] Pull Request created:', prResult.pr_url);
452
+
453
+ // Update message with PR link
454
+ setMessages((prev) => {
455
+ const newMessages = [...prev];
456
+ newMessages[newMessages.length - 1] = {
457
+ ...newMessages[newMessages.length - 1],
458
+ content: `βœ… Code generated successfully!\n\nβœ… Pull Request created! [View PR](${prResult.pr_url})`,
459
+ };
460
+ return newMessages;
461
+ });
462
+
463
+ // Open PR in new tab
464
+ window.open(prResult.pr_url, '_blank');
465
+ } else {
466
+ throw new Error(prResult.message || 'Failed to create Pull Request');
467
+ }
468
+ } catch (error: any) {
469
+ console.error('[PR] Failed to create Pull Request:', error);
470
+
471
+ // Update message with error
472
+ setMessages((prev) => {
473
+ const newMessages = [...prev];
474
+ newMessages[newMessages.length - 1] = {
475
+ ...newMessages[newMessages.length - 1],
476
+ content: `βœ… Code generated successfully!\n\n❌ Failed to create Pull Request: ${error.message || 'Unknown error'}`,
477
+ };
478
+ return newMessages;
479
+ });
480
+ }
481
+ };
482
+
483
  const handleDeploy = async () => {
484
  console.log('[Deploy] 🎬 handleDeploy called');
485
  console.log('[Deploy] generatedCode exists?', !!generatedCode);
 
776
  };
777
 
778
  // Handle landing page prompt submission
779
+ const handleLandingPageStart = async (prompt: string, language: Language, modelId: string, repoId?: string, shouldCreatePR?: boolean) => {
780
  // Hide landing page immediately for smooth transition
781
  setShowLandingPage(false);
782
+
783
+ // If shouldCreatePR is true, set pending PR state
784
+ if (shouldCreatePR && repoId) {
785
+ console.log('[PR] Setting pending PR for:', repoId);
786
+ setPendingPR({ repoId, language });
787
+ }
788
+
789
  // Send the message with the selected language and model
790
+ // Don't pass repoId to handleSendMessage when creating PR (we want to generate code first, then create PR)
791
+ await handleSendMessage(prompt, language, modelId, shouldCreatePR ? undefined : repoId);
792
  };
793
 
794
  // Resize handlers for chat sidebar (desktop only)
frontend/src/components/LandingPage.tsx CHANGED
@@ -15,7 +15,7 @@ import type { Model, Language } from '@/types';
15
  import type { OAuthUserInfo } from '@/lib/auth';
16
 
17
  interface LandingPageProps {
18
- onStart: (prompt: string, language: Language, modelId: string, repoId?: string) => void;
19
  onImport?: (code: string, language: Language, importUrl?: string) => void;
20
  isAuthenticated: boolean;
21
  initialLanguage?: Language;
@@ -436,7 +436,7 @@ Note: After generating the redesign, I will create a Pull Request on the origina
436
  if (onStart) {
437
  console.log('[Redesign] Will create PR - not passing repo ID');
438
  console.log('[Redesign] Using Claude-Sonnet-4.5 for redesign');
439
- onStart(redesignPrompt, result.language || 'html', 'claude-sonnet-4.5', undefined);
440
  }
441
 
442
  console.log('[Redesign] Will create PR after code generation completes');
 
15
  import type { OAuthUserInfo } from '@/lib/auth';
16
 
17
  interface LandingPageProps {
18
+ onStart: (prompt: string, language: Language, modelId: string, repoId?: string, shouldCreatePR?: boolean) => void;
19
  onImport?: (code: string, language: Language, importUrl?: string) => void;
20
  isAuthenticated: boolean;
21
  initialLanguage?: Language;
 
436
  if (onStart) {
437
  console.log('[Redesign] Will create PR - not passing repo ID');
438
  console.log('[Redesign] Using Claude-Sonnet-4.5 for redesign');
439
+ onStart(redesignPrompt, result.language || 'html', 'claude-sonnet-4.5', repoId, true); // Pass true for shouldCreatePR
440
  }
441
 
442
  console.log('[Redesign] Will create PR after code generation completes');