This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
forked from getAlby/nostr-wallet-connect
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from getAlby/task-finish-screen
feat: add finish screen for setup
- Loading branch information
Showing
11 changed files
with
178 additions
and
161 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { useEffect } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
import Container from "src/components/Container"; | ||
import { useInfo } from "src/hooks/useInfo"; | ||
import useSetupStore from "src/state/SetupStore"; | ||
import { useCSRF } from "src/hooks/useCSRF"; | ||
import { handleRequestError } from "src/utils/handleRequestError"; | ||
import { request } from "src/utils/request"; | ||
import Loading from "src/components/Loading"; | ||
import { NodeInfo } from "src/types"; | ||
import React from "react"; | ||
|
||
export function SetupFinish() { | ||
const navigate = useNavigate(); | ||
const { nodeInfo, unlockPassword } = useSetupStore(); | ||
|
||
const { mutate: refetchInfo } = useInfo(); | ||
const { data: csrf } = useCSRF(); | ||
const [connectionError, setConnectionError] = React.useState(false); | ||
const hasFetchedRef = React.useRef(false); | ||
|
||
useEffect(() => { | ||
// ensure setup call is only called once | ||
if (!csrf || hasFetchedRef.current) { | ||
return; | ||
} | ||
hasFetchedRef.current = true; | ||
|
||
(async () => { | ||
const succeeded = await finishSetup(csrf, nodeInfo, unlockPassword); | ||
if (succeeded) { | ||
await refetchInfo(); | ||
navigate("/"); | ||
} else { | ||
setConnectionError(true); | ||
} | ||
})(); | ||
}, [csrf, nodeInfo, refetchInfo, navigate, unlockPassword]); | ||
|
||
if (connectionError) { | ||
return ( | ||
<Container> | ||
<h1 className="font-semibold text-lg font-headline mt-16 mb-8 dark:text-white"> | ||
Connection Failed | ||
</h1> | ||
|
||
<p>Navigate back to check your configuration, then try again.</p> | ||
</Container> | ||
); | ||
} | ||
|
||
return ( | ||
<Container> | ||
<h1 className="font-semibold text-lg font-headline mt-16 mb-8 dark:text-white"> | ||
Connecting... | ||
</h1> | ||
|
||
<Loading /> | ||
</Container> | ||
); | ||
} | ||
|
||
const finishSetup = async ( | ||
csrf: string, | ||
nodeInfo: NodeInfo, | ||
unlockPassword: string | ||
): Promise<boolean> => { | ||
try { | ||
await request("/api/setup", { | ||
method: "POST", | ||
headers: { | ||
"X-CSRF-Token": csrf, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
...nodeInfo, | ||
unlockPassword, | ||
}), | ||
}); | ||
await request("/api/start", { | ||
method: "POST", | ||
headers: { | ||
"X-CSRF-Token": csrf, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
unlockPassword, | ||
}), | ||
}); | ||
return true; | ||
} catch (error) { | ||
handleRequestError("Failed to connect", error); | ||
return false; | ||
} | ||
}; |
This file contains 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
Oops, something went wrong.