mirror of
https://github.com/stackblitz/bolt.new
synced 2025-02-06 04:48:04 +00:00
feat(git): import from url
This commit is contained in:
parent
83d201ab14
commit
bb83bb414d
106
app/components/git/GitUrlImport.client.tsx
Normal file
106
app/components/git/GitUrlImport.client.tsx
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
import { useSearchParams } from '@remix-run/react';
|
||||||
|
import { generateId, type Message } from 'ai';
|
||||||
|
import ignore from 'ignore';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { ClientOnly } from 'remix-utils/client-only';
|
||||||
|
import { BaseChat } from '~/components/chat/BaseChat';
|
||||||
|
import { Chat } from '~/components/chat/Chat.client';
|
||||||
|
import { useGit } from '~/lib/hooks/useGit';
|
||||||
|
import { useChatHistory } from '~/lib/persistence';
|
||||||
|
|
||||||
|
const IGNORE_PATTERNS = [
|
||||||
|
'node_modules/**',
|
||||||
|
'.git/**',
|
||||||
|
'.github/**',
|
||||||
|
'.vscode/**',
|
||||||
|
'**/*.jpg',
|
||||||
|
'**/*.jpeg',
|
||||||
|
'**/*.png',
|
||||||
|
'dist/**',
|
||||||
|
'build/**',
|
||||||
|
'.next/**',
|
||||||
|
'coverage/**',
|
||||||
|
'.cache/**',
|
||||||
|
'.vscode/**',
|
||||||
|
'.idea/**',
|
||||||
|
'**/*.log',
|
||||||
|
'**/.DS_Store',
|
||||||
|
'**/npm-debug.log*',
|
||||||
|
'**/yarn-debug.log*',
|
||||||
|
'**/yarn-error.log*',
|
||||||
|
'**/*lock.json',
|
||||||
|
'**/*lock.yaml',
|
||||||
|
];
|
||||||
|
|
||||||
|
export function GitUrlImport() {
|
||||||
|
const [searchParams] = useSearchParams();
|
||||||
|
const { ready: historyReady, importChat } = useChatHistory();
|
||||||
|
const { ready: gitReady, gitClone } = useGit();
|
||||||
|
const [imported, setImported] = useState(false);
|
||||||
|
|
||||||
|
const importRepo = async (repoUrl?: string) => {
|
||||||
|
if (!gitReady && !historyReady) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (repoUrl) {
|
||||||
|
const ig = ignore().add(IGNORE_PATTERNS);
|
||||||
|
const { workdir, data } = await gitClone(repoUrl);
|
||||||
|
|
||||||
|
if (importChat) {
|
||||||
|
const filePaths = Object.keys(data).filter((filePath) => !ig.ignores(filePath));
|
||||||
|
console.log(filePaths);
|
||||||
|
|
||||||
|
const textDecoder = new TextDecoder('utf-8');
|
||||||
|
const message: Message = {
|
||||||
|
role: 'assistant',
|
||||||
|
content: `Cloning the repo ${repoUrl} into ${workdir}
|
||||||
|
<boltArtifact id="imported-files" title="Git Cloned Files" type="bundled" >
|
||||||
|
${filePaths
|
||||||
|
.map((filePath) => {
|
||||||
|
const { data: content, encoding } = data[filePath];
|
||||||
|
|
||||||
|
if (encoding === 'utf8') {
|
||||||
|
return `<boltAction type="file" filePath="${filePath}">
|
||||||
|
${content}
|
||||||
|
</boltAction>`;
|
||||||
|
} else if (content instanceof Uint8Array) {
|
||||||
|
return `<boltAction type="file" filePath="${filePath}">
|
||||||
|
${textDecoder.decode(content)}
|
||||||
|
</boltAction>`;
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.join('\n')}
|
||||||
|
</boltArtifact>`,
|
||||||
|
id: generateId(),
|
||||||
|
createdAt: new Date(),
|
||||||
|
};
|
||||||
|
console.log(JSON.stringify(message));
|
||||||
|
|
||||||
|
importChat(`Git Project:${repoUrl.split('/').slice(-1)[0]}`, [message]);
|
||||||
|
|
||||||
|
// console.log(files);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!historyReady || !gitReady || imported) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = searchParams.get('url');
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
window.location.href = '/';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
importRepo(url);
|
||||||
|
setImported(true);
|
||||||
|
}, [searchParams, historyReady, gitReady, imported]);
|
||||||
|
|
||||||
|
return <ClientOnly fallback={<BaseChat />}>{() => <Chat />}</ClientOnly>;
|
||||||
|
}
|
23
app/routes/git.tsx
Normal file
23
app/routes/git.tsx
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import type { LoaderFunctionArgs } from '@remix-run/cloudflare';
|
||||||
|
import { json, type MetaFunction } from '@remix-run/cloudflare';
|
||||||
|
import { ClientOnly } from 'remix-utils/client-only';
|
||||||
|
import { BaseChat } from '~/components/chat/BaseChat';
|
||||||
|
import { GitUrlImport } from '~/components/git/GitUrlImport.client';
|
||||||
|
import { Header } from '~/components/header/Header';
|
||||||
|
|
||||||
|
export const meta: MetaFunction = () => {
|
||||||
|
return [{ title: 'Bolt' }, { name: 'description', content: 'Talk with Bolt, an AI assistant from StackBlitz' }];
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function loader(args: LoaderFunctionArgs) {
|
||||||
|
return json({ url: args.params.url });
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Index() {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col h-full w-full">
|
||||||
|
<Header />
|
||||||
|
<ClientOnly fallback={<BaseChat />}>{() => <GitUrlImport />}</ClientOnly>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user