@@ -10,7 +10,6 @@ import dynamic from 'next/dynamic';
1010import { Project , createProject , saveMessage , getProjectMessages , generateProjectTitle , getProject } from '@/lib/database' ;
1111import { Message , toAISDKMessages , toMessageImage } from '@/lib/messages' ;
1212import { LLMModelConfig } from '@/lib/models' ;
13- import { loadModels } from '@/lib/models-loader' ;
1413import { FragmentSchema , fragmentSchema as schema } from '@/lib/schema' ;
1514import { createSupabaseBrowserClient } from '@/lib/supabase-browser' ;
1615import templates , { TemplateId } from '@/lib/templates' ;
@@ -23,8 +22,8 @@ import { useCallback, useEffect, useState } from 'react';
2322import { useLocalStorage } from 'usehooks-ts' ;
2423import { useUserTeam } from '@/lib/user-team-provider' ;
2524import { HeroPillSecond } from '@/components/announcement' ;
26- import { useAnalytics } from '@/lib/analytics-service' ;
2725import { SupabaseClient } from '@supabase/supabase-js' ;
26+ import models from '@/lib/models.json' ;
2827
2928const PricingModal = dynamic ( ( ) => import ( '@/components/pricing' ) . then ( mod => ( { default : mod . PricingModal } ) ) , {
3029 ssr : false ,
@@ -47,10 +46,12 @@ export default function Home() {
4746 model : 'claude-3-5-sonnet-latest' ,
4847 } ,
4948 )
50- const [ modelsList , setModelsList ] = useState < any > ( null )
49+ const [ useMorphApply , setUseMorphApply ] = useLocalStorage (
50+ 'useMorphApply' ,
51+ process . env . NEXT_PUBLIC_USE_MORPH_APPLY === 'true' ,
52+ )
5153
5254 const posthog = usePostHog ( )
53- const analytics = useAnalytics ( )
5455
5556 const [ result , setResult ] = useState < ExecutionResult > ( )
5657 const [ sessionStartTime ] = useState ( Date . now ( ) )
@@ -82,33 +83,30 @@ export default function Home() {
8283 const { session } = useAuth ( setAuthDialogCallback , setAuthViewCallback )
8384 const { userTeam } = useUserTeam ( )
8485
85- // Load models list asynchronously
86- useEffect ( ( ) => {
87- loadModels ( ) . then ( setModelsList )
88- } , [ ] )
89-
90-
9186 const handleChatSelected = async ( chatId : string ) => {
9287 const project = await getProject ( supabase , chatId ) ;
9388 if ( project ) {
9489 setCurrentProject ( project ) ;
9590 }
9691 } ;
9792
98- const filteredModels = modelsList ? .models ? .filter ( ( model : any ) => {
93+ const filteredModels = models . models . filter ( ( model : any ) => {
9994 if ( process . env . NEXT_PUBLIC_HIDE_LOCAL_MODELS ) {
10095 return model . providerId !== 'ollama'
10196 }
10297 return true
103- } ) || [ ]
98+ } )
10499
105100 const currentModel = filteredModels . find (
106101 ( model : any ) => model . id === languageModel . model ,
107102 ) ;
108103
104+ // Determine which API to use based on morph toggle and existing fragment
105+ const shouldUseMorph = useMorphApply && fragment && fragment . code && fragment . file_path
106+ const apiEndpoint = shouldUseMorph ? '/api/morph-chat' : '/api/chat'
109107
110108 const { object, submit, isLoading, stop, error } = useObject ( {
111- api : '/api/chat' ,
109+ api : apiEndpoint ,
112110 schema,
113111 onError : ( error : Error ) => {
114112 setErrorsEncountered ( prev => prev + 1 )
@@ -147,9 +145,7 @@ export default function Home() {
147145 if ( ! error && fragment ) {
148146 setIsPreviewLoading ( true ) ;
149147 // Enhanced analytics tracking
150- const generationTime = Date . now ( ) - Date . now ( ) // Would track actual generation time
151148 if ( fragment . code && fragment . template ) {
152- analytics . trackFragmentGenerated ( fragment as FragmentSchema , generationTime , 1 )
153149 }
154150 setFragmentsGenerated ( prev => prev + 1 )
155151
@@ -179,7 +175,6 @@ export default function Home() {
179175
180176 // Enhanced sandbox tracking
181177 const creationTime = Date . now ( ) - Date . now ( ) // Would track actual creation time
182- analytics . trackSandboxCreation ( fragment ?. template || 'unknown' , creationTime , response . ok )
183178
184179 posthog . capture ( 'sandbox_created' , { url : result . url } )
185180
@@ -263,15 +258,15 @@ export default function Home() {
263258 return ( ) => {
264259 if ( session ?. user ?. id ) {
265260 const sessionDuration = Date . now ( ) - sessionStartTime
266- analytics . trackSessionEnd (
267- sessionDuration ,
268- fragmentsGenerated ,
269- messagesCount ,
270- errorsEncountered
271- )
261+ posthog . capture ( 'session_end' , {
262+ duration : sessionDuration ,
263+ fragments_generated : fragmentsGenerated ,
264+ messages_count : messagesCount ,
265+ errors_encountered : errorsEncountered
266+ } )
272267 }
273268 }
274- } , [ session ?. user ?. id , sessionStartTime , fragmentsGenerated , messagesCount , errorsEncountered , analytics ] )
269+ } , [ session ?. user ?. id , sessionStartTime , fragmentsGenerated , messagesCount , errorsEncountered ] )
275270
276271 function setMessage ( message : Partial < Message > , index ?: number ) {
277272 setMessages ( ( previousMessages ) => {
@@ -325,6 +320,7 @@ export default function Home() {
325320 template : templateToSend ,
326321 model : currentModel ,
327322 config : languageModel ,
323+ ...( shouldUseMorph && fragment ? { currentFragment : fragment } : { } ) ,
328324 } )
329325
330326 if ( ! currentProject ) {
@@ -347,17 +343,9 @@ export default function Home() {
347343 const promptLength = currentInput . length
348344 const hasImages = currentFiles . length > 0
349345
350- analytics . trackPromptSubmission (
351- currentInput ,
352- languageModel . model || 'unknown' ,
353- promptLength ,
354- hasImages ,
355- messages . length > 0 ? 'conversation' : 'none'
356- )
357-
358346 // Track template selection
359347 if ( selectedTemplate !== 'auto' ) {
360- analytics . trackTemplateSelected ( selectedTemplate , 'manual' )
348+ posthog . capture ( 'template_selected' , { template : selectedTemplate , source : 'manual' } )
361349 }
362350
363351 posthog . capture ( 'chat_submit' , {
@@ -374,6 +362,7 @@ export default function Home() {
374362 template : templates ,
375363 model : currentModel ,
376364 config : languageModel ,
365+ ...( shouldUseMorph && fragment ? { currentFragment : fragment } : { } ) ,
377366 } )
378367 }
379368
@@ -392,7 +381,11 @@ export default function Home() {
392381
393382 if ( previousModel && newModel && previousModel !== newModel ) {
394383 // Track model switching
395- analytics . trackModelSwitch ( previousModel , newModel , 'experiment' )
384+ posthog . capture ( 'model_switch' , {
385+ previousModel,
386+ newModel,
387+ source : 'experiment'
388+ } )
396389
397390 // Revenue tracking handled by analytics service
398391 }
@@ -408,9 +401,7 @@ export default function Home() {
408401 }
409402
410403 // Enhanced social tracking
411- analytics . trackFeatureUsed ( `social_${ target } ` , { target } )
412-
413- posthog . capture ( `${ target } _click` )
404+ posthog . capture ( `${ target } _click` , { target } )
414405 }
415406
416407 function handleClearChat ( ) {
@@ -594,6 +585,8 @@ export default function Home() {
594585 onLanguageModelChange = { handleLanguageModelChange }
595586 apiKeyConfigurable = { ! process . env . NEXT_PUBLIC_NO_API_KEY_INPUT }
596587 baseURLConfigurable = { ! process . env . NEXT_PUBLIC_NO_BASE_URL_INPUT }
588+ useMorphApply = { useMorphApply }
589+ onUseMorphApplyChange = { setUseMorphApply }
597590 />
598591 </ div >
599592 </ div >
0 commit comments