Skip to content

Commit

Permalink
feat(upload folder): init wip upload mvp for testing PE-4643
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Aug 12, 2024
1 parent b23f342 commit 435a108
Show file tree
Hide file tree
Showing 8 changed files with 734 additions and 254 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test": "echo \"TODO: add tests\" && exit 0"
},
"dependencies": {
"@ardrive/turbo-sdk": "latest",
"@ardrive/turbo-sdk": "link:../ardrive-turbo-sdk",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.20.1"
Expand Down
2 changes: 2 additions & 0 deletions src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GiftPage } from "./pages/GiftPage";
import { RedeemPage } from "./pages/RedeemPage";
import { FiatTopUpPage } from "./pages/FiatTopUpPage";
import { CryptoTopUpPage } from "./pages/CryptoTopUpPage";
import { UploadPage } from "./pages/UploadPage";

export function Router() {
return (
Expand All @@ -13,6 +14,7 @@ export function Router() {
<Route path="/top-up" element={<FiatTopUpPage />} />
<Route path="/top-up/fiat" element={<FiatTopUpPage />} />
<Route path="/top-up/crypto" element={<CryptoTopUpPage />} />
<Route path="/upload" element={<UploadPage />} />

<Route
path="/"
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useCreditsForFiat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TurboFactory, USD } from "@ardrive/turbo-sdk";
import { TurboFactory, USD } from "@ardrive/turbo-sdk/web";

Check failure on line 1 in src/hooks/useCreditsForFiat.ts

View workflow job for this annotation

GitHub Actions / build (18.x, build)

Cannot find module '@ardrive/turbo-sdk/web' or its corresponding type declarations.

Check failure on line 1 in src/hooks/useCreditsForFiat.ts

View workflow job for this annotation

GitHub Actions / build (20.x, build)

Cannot find module '@ardrive/turbo-sdk/web' or its corresponding type declarations.
import { useState, useRef, useEffect } from "react";
import { turboConfig, wincPerCredit } from "../constants";

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useWincForOneGiB.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TurboFactory } from "@ardrive/turbo-sdk";
import { TurboFactory } from "@ardrive/turbo-sdk/web";

Check failure on line 1 in src/hooks/useWincForOneGiB.ts

View workflow job for this annotation

GitHub Actions / build (18.x, build)

Cannot find module '@ardrive/turbo-sdk/web' or its corresponding type declarations.

Check failure on line 1 in src/hooks/useWincForOneGiB.ts

View workflow job for this annotation

GitHub Actions / build (20.x, build)

Cannot find module '@ardrive/turbo-sdk/web' or its corresponding type declarations.
import { useState, useEffect } from "react";
import { turboConfig } from "../constants";

Expand Down
2 changes: 1 addition & 1 deletion src/pages/CryptoTopUpPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TurboFactory,
ARToTokenAmount,
ArconnectSigner,
} from "@ardrive/turbo-sdk";
} from "@ardrive/turbo-sdk/web";

Check failure on line 16 in src/pages/CryptoTopUpPage.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, build)

Cannot find module '@ardrive/turbo-sdk/web' or its corresponding type declarations.

Check failure on line 16 in src/pages/CryptoTopUpPage.tsx

View workflow job for this annotation

GitHub Actions / build (20.x, build)

Cannot find module '@ardrive/turbo-sdk/web' or its corresponding type declarations.
import { getArconnect } from "../utils/arconnect";
import { NewToArDriveInfo } from "../components/NewToArDriveInfo";
import { useWincForOneGiB } from "../hooks/useWincForOneGiB";
Expand Down
2 changes: 1 addition & 1 deletion src/pages/FiatTopUpPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "../constants";
import { Page } from "./Page";
import { useLocation } from "react-router-dom";
import { TurboFactory, USD } from "@ardrive/turbo-sdk";
import { TurboFactory, USD } from "@ardrive/turbo-sdk/web";

Check failure on line 12 in src/pages/FiatTopUpPage.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, build)

Cannot find module '@ardrive/turbo-sdk/web' or its corresponding type declarations.

Check failure on line 12 in src/pages/FiatTopUpPage.tsx

View workflow job for this annotation

GitHub Actions / build (20.x, build)

Cannot find module '@ardrive/turbo-sdk/web' or its corresponding type declarations.
import { NewToArDriveInfo } from "../components/NewToArDriveInfo";
import { getArconnect } from "../utils/arconnect";
import { useCreditsForFiat } from "../hooks/useCreditsForFiat";
Expand Down
115 changes: 115 additions & 0 deletions src/pages/UploadPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { useEffect, useState } from "react";
import { ErrMsgCallbackAsProps } from "../types";
import { ardriveAppUrl, turboConfig } from "../constants";
import { Page } from "./Page";
import {
TurboAuthenticatedClient,
TurboFactory,
ArconnectSigner,
} from "@ardrive/turbo-sdk/web";
import { getArconnect } from "../utils/arconnect";
import { NewToArDriveInfo } from "../components/NewToArDriveInfo";

function UploadForm({ errorCallback }: ErrMsgCallbackAsProps) {
const [selectedFiles, setSelectedFiles] = useState<FileList | null>(null);

const [turbo, setTurbo] = useState<undefined | TurboAuthenticatedClient>(
undefined,
);
const [sending, setSending] = useState<boolean>(false);

useEffect(() => {
getArconnect().then(async () => {
const arconnect_signer = new ArconnectSigner(window.arweaveWallet);

const turbo = TurboFactory.authenticated({
signer: arconnect_signer,
...turboConfig,
});
setTurbo(turbo);
});
}, []);

// TODO: Query params if needed
// const location = useLocation();
// useEffect(() => {

// }, [location.search]);

const canSubmitForm = !!turbo && !!selectedFiles && !sending;

const handleSubmit = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
e.preventDefault();

if (!canSubmitForm) {
return;
}

setSending(true);
turbo
.uploadFolder({
files: Array.from(selectedFiles),
maxConcurrentUploads: 1,
})
.catch((err: unknown) => {
console.log("err", err);
errorCallback(
`Error uploading: ${err instanceof Error ? err.message : err}`,
);
})
.then((res: unknown) => {
console.log("res", res);
setSending(false);

// TODO: Success Modal or Page
alert("Great Upload!!\n" + JSON.stringify(res));
});
};

return (
<>
<h1>Upload a folder or file with your wallet</h1>
<p>
If you do not have an Arweave wallet, you can create one in{" "}
{/* TODO: Create wallet from turbo sdk/app */}
<a href={ardriveAppUrl}>ArDrive App</a>.
</p>
<form className="form">
<div className="form-section">
{/* TODO: Current balance of wallet in AR and Turbo Credits */}

<label className="form-label">Upload</label>
<input
type="file"
multiple
onChange={(e) => setSelectedFiles(e.target.files)}
/>
{/* <input
// webkitdirectory
type="file"
multiple
onChange={(e) => setSelectedFiles(e.target.files)}
/> */}
</div>

{sending && <p>Now Sending Fund Transaction...</p>}

{/* TODO: Estimate price */}
<button
type="submit"
className="proceed-button"
onClick={(e) => handleSubmit(e)}
disabled={!canSubmitForm}
>
Send Upload
</button>
</form>

<NewToArDriveInfo />
</>
);
}

export const UploadPage = () => (
<Page page={(e) => <UploadForm errorCallback={e} />} />
);
Loading

0 comments on commit 435a108

Please sign in to comment.