Conversation
Owner
ewdlop
commented
Feb 9, 2025
```jsx
// App.js (React frontend)
import React, { useState, useEffect } from 'react';
import { ethers } from 'ethers';
import { create } from 'ipfs-http-client';
import { Buffer } from 'buffer';
const App = () => {
const [contract, setContract] = useState(null);
const [account, setAccount] = useState(null);
const [repositories, setRepositories] = useState([]);
const [selectedRepo, setSelectedRepo] = useState(null);
const [commits, setCommits] = useState([]);
useEffect(() => {
initializeEthereum();
}, []);
const initializeEthereum = async () => {
if (window.ethereum) {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contractAddress = "YOUR_CONTRACT_ADDRESS";
const contractABI = []; // Add your contract ABI here
const githubContract = new ethers.Contract(
contractAddress,
contractABI,
signer
);
setContract(githubContract);
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts'
});
setAccount(accounts[0]);
}
};
const createRepository = async (name, description, isPrivate) => {
try {
const tx = await contract.createRepository(name, description, isPrivate);
await tx.wait();
// Update UI
} catch (error) {
console.error('Error creating repository:', error);
}
};
const pushCommit = async (repoName, branch, files, message) => {
try {
// Initialize IPFS client
const ipfs = create('http://localhost:5001');
// Add files to IPFS
const filesBuffer = Buffer.from(JSON.stringify(files));
const ipfsResult = await ipfs.add(filesBuffer);
const fileChangesHash = ipfsResult.path;
// Generate commit hash
const commitHash = ethers.utils.id(
JSON.stringify({
files: fileChangesHash,
message,
timestamp: Date.now()
})
);
const tx = await contract.pushCommit(
repoName,
branch,
commitHash,
message,
Date.now().toString(),
fileChangesHash
);
await tx.wait();
// Update UI
fetchCommits(repoName, branch);
} catch (error) {
console.error('Error pushing commit:', error);
}
};
const fetchCommits = async (repoName, branch) => {
try {
const commits = await contract.getCommits(repoName, branch);
setCommits(commits);
} catch (error) {
console.error('Error fetching commits:', error);
}
};
return (
<div className="app-container">
<header>
<h1>Decentralized GitHub</h1>
<p>Connected Account: {account}</p>
</header>
<div className="main-content">
<div className="sidebar">
<h2>Repositories</h2>
<button onClick={() => /* Show create repo modal */}>
New Repository
</button>
<ul>
{repositories.map(repo => (
<li
key={repo.name}
onClick={() => setSelectedRepo(repo)}
>
{repo.name}
</li>
))}
</ul>
</div>
<div className="content">
{selectedRepo && (
<div>
<h2>{selectedRepo.name}</h2>
<div className="commits-list">
{commits.map(commit => (
<div key={commit.commitHash} className="commit">
<h3>{commit.message}</h3>
<p>Author: {commit.author}</p>
<p>Time: {new Date(parseInt(commit.timestamp)).toLocaleString()}</p>
</div>
))}
</div>
</div>
)}
</div>
</div>
</div>
);
};
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.