mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
add account import and export
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import React, { useState, useEffect, useRef } from "react";
|
import React, { useState, useEffect, useRef } from "react";
|
||||||
import { Link, useLocation, useNavigate } from "react-router-dom";
|
import { Link, useLocation, useNavigate } from "react-router-dom";
|
||||||
import Modal from "react-modal";
|
import Modal from "react-modal";
|
||||||
import { deleteCaches, setAccount, useAccount } from "@plebbit/plebbit-react-hooks";
|
import { deleteCaches, exportAccount, importAccount, setAccount, useAccount } from "@plebbit/plebbit-react-hooks";
|
||||||
import { StyledModal } from "./styled/SettingsModal.styled";
|
import { StyledModal } from "./styled/SettingsModal.styled";
|
||||||
import useGeneralStore from "../hooks/stores/useGeneralStore";
|
import useGeneralStore from "../hooks/stores/useGeneralStore";
|
||||||
import useError from "../hooks/useError";
|
import useError from "../hooks/useError";
|
||||||
@@ -10,17 +10,14 @@ import packageJson from '../../package.json'
|
|||||||
const {version} = packageJson
|
const {version} = packageJson
|
||||||
|
|
||||||
|
|
||||||
const customOverlayStyles = {
|
|
||||||
overlay: {
|
|
||||||
backgroundColor: 'rgba(0,0,0,.25)'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const SettingsModal = ({ isOpen, closeModal }) => {
|
const SettingsModal = ({ isOpen, closeModal }) => {
|
||||||
const selectedStyle = useGeneralStore(state => state.selectedStyle);
|
const selectedStyle = useGeneralStore(state => state.selectedStyle);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const [expanded, setExpanded] = useState([]);
|
const [expanded, setExpanded] = useState([]);
|
||||||
|
const [accountJson, setAccountJson] = useState(null);
|
||||||
|
const [setImportSuccess] = useState(false);
|
||||||
|
|
||||||
const [errorMessage, setErrorMessage] = useState(null);
|
const [errorMessage, setErrorMessage] = useState(null);
|
||||||
const [successMessage, setSuccessMessage] = useState(null);
|
const [successMessage, setSuccessMessage] = useState(null);
|
||||||
useError(errorMessage, [errorMessage]);
|
useError(errorMessage, [errorMessage]);
|
||||||
@@ -28,10 +25,18 @@ const SettingsModal = ({ isOpen, closeModal }) => {
|
|||||||
|
|
||||||
const account = useAccount();
|
const account = useAccount();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (account) {
|
||||||
|
console.log(account);
|
||||||
|
}
|
||||||
|
}, [account]);
|
||||||
|
|
||||||
|
|
||||||
const gatewayRef = useRef();
|
const gatewayRef = useRef();
|
||||||
const ipfsRef = useRef();
|
const ipfsRef = useRef();
|
||||||
const pubsubRef = useRef();
|
const pubsubRef = useRef();
|
||||||
const dataPathRef = useRef();
|
const dataPathRef = useRef();
|
||||||
|
const importRef = useRef();
|
||||||
|
|
||||||
const isValidURL = (url) => {
|
const isValidURL = (url) => {
|
||||||
try {
|
try {
|
||||||
@@ -108,6 +113,8 @@ const SettingsModal = ({ isOpen, closeModal }) => {
|
|||||||
|
|
||||||
|
|
||||||
const handleCloseModal = () => {
|
const handleCloseModal = () => {
|
||||||
|
setAccountJson(null);
|
||||||
|
|
||||||
if (location.pathname.endsWith("/settings")) {
|
if (location.pathname.endsWith("/settings")) {
|
||||||
const newPath = location.pathname.slice(0, -9);
|
const newPath = location.pathname.slice(0, -9);
|
||||||
closeModal();
|
closeModal();
|
||||||
@@ -146,7 +153,27 @@ const SettingsModal = ({ isOpen, closeModal }) => {
|
|||||||
localStorage.removeItem("cacheCleared");
|
localStorage.removeItem("cacheCleared");
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
|
const handleExport = async () => {
|
||||||
|
const activeAccountJson = await exportAccount();
|
||||||
|
setAccountJson(activeAccountJson);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
const handleImport = async () => {
|
||||||
|
const accountJson = importRef.current.value;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await importAccount(accountJson);
|
||||||
|
setImportSuccess(true);
|
||||||
|
localStorage.setItem("toastMessage", "Account Imported");
|
||||||
|
window.location.reload();
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
setErrorMessage(error.message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -154,7 +181,7 @@ const SettingsModal = ({ isOpen, closeModal }) => {
|
|||||||
isOpen={isOpen}
|
isOpen={isOpen}
|
||||||
onRequestClose={handleCloseModal}
|
onRequestClose={handleCloseModal}
|
||||||
contentLabel="Settings"
|
contentLabel="Settings"
|
||||||
style={customOverlayStyles}
|
style={{ overlay: { backgroundColor: "rgba(0,0,0,.25)" }}}
|
||||||
selectedStyle={selectedStyle}
|
selectedStyle={selectedStyle}
|
||||||
expanded={expanded}
|
expanded={expanded}
|
||||||
>
|
>
|
||||||
@@ -201,13 +228,26 @@ const SettingsModal = ({ isOpen, closeModal }) => {
|
|||||||
<div className="plebbit-options-buttons"
|
<div className="plebbit-options-buttons"
|
||||||
style={{ display: expanded.includes(1) ? 'block' : 'none' }}
|
style={{ display: expanded.includes(1) ? 'block' : 'none' }}
|
||||||
>
|
>
|
||||||
<button className="save-button">Export</button>
|
|
||||||
<button className="reset-button">Import</button>
|
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
<ul className="settings-cat" style={{ display: expanded.includes(1) ? 'block' : 'none' }}>
|
<ul className="settings-cat" style={{ display: expanded.includes(1) ? 'block' : 'none' }}>
|
||||||
<li>
|
<li className="settings-option disc">
|
||||||
|
Export or Import Your Data</li>
|
||||||
|
<div className="plebbit-options-buttons"
|
||||||
|
style={{ display: expanded.includes(1) ? 'block' : 'none' }}
|
||||||
|
>
|
||||||
|
<button className="save-button"
|
||||||
|
onClick={handleExport}>Export</button>
|
||||||
|
<button className="reset-button"
|
||||||
|
onClick={handleImport}
|
||||||
|
>Import</button>
|
||||||
|
</div>
|
||||||
|
<li className="settings-tip">
|
||||||
|
To export, click "Export", then save your account data displayed below in a safe place. To import, paste your account data into the box below, then click "Import".
|
||||||
</li>
|
</li>
|
||||||
|
<div className="settings-input">
|
||||||
|
<textarea ref={importRef} value={accountJson} />
|
||||||
|
</div>
|
||||||
</ul>
|
</ul>
|
||||||
</ul>
|
</ul>
|
||||||
<ul>
|
<ul>
|
||||||
@@ -221,8 +261,10 @@ const SettingsModal = ({ isOpen, closeModal }) => {
|
|||||||
<div className="plebbit-options-buttons"
|
<div className="plebbit-options-buttons"
|
||||||
style={{ display: expanded.includes(2) ? 'block' : 'none' }}
|
style={{ display: expanded.includes(2) ? 'block' : 'none' }}
|
||||||
>
|
>
|
||||||
<button className="save-button" onClick={handleSavePlebbitOptions}>Save</button>
|
<button className="save-button"
|
||||||
<button className="reset-button"onClick={handleResetPlebbitOptions}>Reset</button>
|
onClick={handleSavePlebbitOptions}>Save</button>
|
||||||
|
<button className="reset-button"
|
||||||
|
onClick={handleResetPlebbitOptions}>Reset</button>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
<ul className="settings-cat" style={{ display: expanded.includes(2) ? 'block' : 'none' }}>
|
<ul className="settings-cat" style={{ display: expanded.includes(2) ? 'block' : 'none' }}>
|
||||||
|
|||||||
@@ -58,13 +58,13 @@ const Subscriptions = () => {
|
|||||||
|
|
||||||
const errorString = useMemo(() => {
|
const errorString = useMemo(() => {
|
||||||
for (const subplebbit of subplebbits) {
|
for (const subplebbit of subplebbits) {
|
||||||
if (subplebbit.updatingState !== 'failed') {
|
if (subplebbit?.updatingState !== 'failed') {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const subplebbit of subplebbits) {
|
for (const subplebbit of subplebbits) {
|
||||||
if (subplebbit.error) {
|
if (subplebbit?.error) {
|
||||||
return `Failed fetching subplebbit: ${subplebbit.error.toString().slice(0, 300)}`
|
return `Failed fetching subplebbit: ${subplebbit?.error.toString().slice(0, 300)}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [subplebbits])
|
}, [subplebbits])
|
||||||
|
|||||||
Reference in New Issue
Block a user