|
| 1 | +import ignore from 'ignore'; |
| 2 | +import { useGit } from '~/lib/hooks/useGit'; |
| 3 | +import type { Message } from 'ai'; |
| 4 | +import WithTooltip from '~/components/ui/Tooltip'; |
| 5 | + |
| 6 | +const IGNORE_PATTERNS = [ |
| 7 | + 'node_modules/**', |
| 8 | + '.git/**', |
| 9 | + '.github/**', |
| 10 | + '.vscode/**', |
| 11 | + '**/*.jpg', |
| 12 | + '**/*.jpeg', |
| 13 | + '**/*.png', |
| 14 | + 'dist/**', |
| 15 | + 'build/**', |
| 16 | + '.next/**', |
| 17 | + 'coverage/**', |
| 18 | + '.cache/**', |
| 19 | + '.vscode/**', |
| 20 | + '.idea/**', |
| 21 | + '**/*.log', |
| 22 | + '**/.DS_Store', |
| 23 | + '**/npm-debug.log*', |
| 24 | + '**/yarn-debug.log*', |
| 25 | + '**/yarn-error.log*', |
| 26 | + '**/*lock.json', |
| 27 | + '**/*lock.yaml', |
| 28 | +]; |
| 29 | + |
| 30 | +const ig = ignore().add(IGNORE_PATTERNS); |
| 31 | +const generateId = () => Math.random().toString(36).substring(2, 15); |
| 32 | + |
| 33 | +interface GitCloneButtonProps { |
| 34 | + className?: string; |
| 35 | + importChat?: (description: string, messages: Message[]) => Promise<void>; |
| 36 | +} |
| 37 | + |
| 38 | +export default function GitCloneButton({ importChat }: GitCloneButtonProps) { |
| 39 | + const { ready, gitClone } = useGit(); |
| 40 | + const onClick = async (_e: any) => { |
| 41 | + if (!ready) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const repoUrl = prompt('Enter the Git url'); |
| 46 | + |
| 47 | + if (repoUrl) { |
| 48 | + const { workdir, data } = await gitClone(repoUrl); |
| 49 | + |
| 50 | + if (importChat) { |
| 51 | + const filePaths = Object.keys(data).filter((filePath) => !ig.ignores(filePath)); |
| 52 | + console.log(filePaths); |
| 53 | + |
| 54 | + const textDecoder = new TextDecoder('utf-8'); |
| 55 | + const message: Message = { |
| 56 | + role: 'assistant', |
| 57 | + content: `Cloning the repo ${repoUrl} into ${workdir} |
| 58 | +<boltArtifact id="imported-files" title="Git Cloned Files" type="bundled" > |
| 59 | + ${filePaths |
| 60 | + .map((filePath) => { |
| 61 | + const { data: content, encoding } = data[filePath]; |
| 62 | +
|
| 63 | + if (encoding === 'utf8') { |
| 64 | + return `<boltAction type="file" filePath="${filePath}"> |
| 65 | +${content} |
| 66 | +</boltAction>`; |
| 67 | + } else if (content instanceof Uint8Array) { |
| 68 | + return `<boltAction type="file" filePath="${filePath}"> |
| 69 | +${textDecoder.decode(content)} |
| 70 | +</boltAction>`; |
| 71 | + } else { |
| 72 | + return ''; |
| 73 | + } |
| 74 | + }) |
| 75 | + .join('\n')} |
| 76 | + </boltArtifact>`, |
| 77 | + id: generateId(), |
| 78 | + createdAt: new Date(), |
| 79 | + }; |
| 80 | + console.log(JSON.stringify(message)); |
| 81 | + |
| 82 | + importChat(`Git Project:${repoUrl.split('/').slice(-1)[0]}`, [message]); |
| 83 | + |
| 84 | + // console.log(files); |
| 85 | + } |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + return ( |
| 90 | + <WithTooltip tooltip="Clone A Git Repo"> |
| 91 | + <button |
| 92 | + onClick={(e) => { |
| 93 | + onClick(e); |
| 94 | + }} |
| 95 | + title="Clone A Git Repo" |
| 96 | + className="px-4 py-2 rounded-lg border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary hover:bg-bolt-elements-background-depth-3 transition-all flex items-center gap-2" |
| 97 | + > |
| 98 | + <span className="i-ph:git-branch" /> |
| 99 | + Clone A Git Repo |
| 100 | + </button> |
| 101 | + </WithTooltip> |
| 102 | + ); |
| 103 | +} |
0 commit comments