Fix Debug page.
parent
7f85d304a8
commit
09dc82df67
|
@ -2,25 +2,23 @@
|
|||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import * as React from "react";
|
||||
import * as EteSync from "etesync";
|
||||
import * as Etebase from "etebase";
|
||||
import Button from "@material-ui/core/Button";
|
||||
import TextField from "@material-ui/core/TextField";
|
||||
|
||||
import Container from "./widgets/Container";
|
||||
import { useSelector } from "react-redux";
|
||||
import { StoreState, CredentialsData, UserInfoData, EntriesListData } from "./store";
|
||||
import { StoreState } from "./store";
|
||||
import { useCredentials } from "./credentials";
|
||||
import { getCollectionManager } from "./etebase-helpers";
|
||||
|
||||
interface PropsType {
|
||||
etesync: CredentialsData;
|
||||
userInfo: UserInfoData;
|
||||
}
|
||||
|
||||
export default function Debug(props: PropsType) {
|
||||
const [stateJournalUid, setJournalUid] = React.useState("");
|
||||
const [entriesUids, setEntriesUids] = React.useState("");
|
||||
export default function Debug() {
|
||||
const etebase = useCredentials()!;
|
||||
const [stateCollectionUid, setCollectionUid] = React.useState("");
|
||||
const [itemsUids, setEntriesUids] = React.useState("");
|
||||
const [result, setResult] = React.useState("");
|
||||
const journals = useSelector((state: StoreState) => state.cache.journals!);
|
||||
const journalEntries = useSelector((state: StoreState) => state.cache.entries);
|
||||
const cacheCollections = useSelector((state: StoreState) => state.cache2.collections);
|
||||
const cacheItems = useSelector((state: StoreState) => state.cache2.items);
|
||||
|
||||
function handleInputChange(func: (value: string) => void) {
|
||||
return (event: React.ChangeEvent<any>) => {
|
||||
|
@ -34,9 +32,9 @@ export default function Debug(props: PropsType) {
|
|||
<TextField
|
||||
style={{ width: "100%" }}
|
||||
type="text"
|
||||
label="Journal UID"
|
||||
value={stateJournalUid}
|
||||
onChange={handleInputChange(setJournalUid)}
|
||||
label="Collection UID"
|
||||
value={stateCollectionUid}
|
||||
onChange={handleInputChange(setCollectionUid)}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
|
@ -44,42 +42,43 @@ export default function Debug(props: PropsType) {
|
|||
style={{ width: "100%" }}
|
||||
type="text"
|
||||
multiline
|
||||
label="Entry UIDs"
|
||||
value={entriesUids}
|
||||
label="Item UIDs"
|
||||
value={itemsUids}
|
||||
onChange={handleInputChange(setEntriesUids)}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
variant="contained"
|
||||
color="secondary"
|
||||
onClick={() => {
|
||||
const { etesync, userInfo } = props;
|
||||
const derived = etesync.encryptionKey;
|
||||
const userInfoCryptoManager = userInfo.getCryptoManager(etesync.encryptionKey);
|
||||
const keyPair = userInfo.getKeyPair(userInfoCryptoManager);
|
||||
const journalUid = stateJournalUid.trim();
|
||||
const journal = journals.get(journalUid);
|
||||
if (!journal) {
|
||||
setResult("Error: journal uid not found.");
|
||||
onClick={async () => {
|
||||
const colUid = stateCollectionUid.trim();
|
||||
const cachedCollection = cacheCollections.get(colUid);
|
||||
const colItems = cacheItems.get(colUid);
|
||||
if (!colItems || !cachedCollection) {
|
||||
setResult("Error: collection uid not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
const colMgr = getCollectionManager(etebase);
|
||||
const col = await colMgr.cacheLoad(Etebase.fromBase64(cachedCollection));
|
||||
const itemMgr = colMgr.getItemManager(col);
|
||||
|
||||
const wantedEntries = {};
|
||||
const wantAll = (entriesUids.trim() === "all");
|
||||
entriesUids.split("\n").forEach((ent) => wantedEntries[ent.trim()] = true);
|
||||
const wantAll = (itemsUids.trim() === "all");
|
||||
itemsUids.split("\n").forEach((ent) => wantedEntries[ent.trim()] = true);
|
||||
|
||||
const cryptoManager = journal.getCryptoManager(derived, keyPair);
|
||||
let prevUid: string | null = null;
|
||||
const retEntries = [];
|
||||
console.log(wantAll, colItems.size);
|
||||
for (const cached of colItems.values()) {
|
||||
const item = await itemMgr.cacheLoad(Etebase.fromBase64(cached));
|
||||
const meta = await item.getMeta();
|
||||
const content = await item.getContent(Etebase.OutputFormat.String);
|
||||
if (wantAll || wantedEntries[item.uid]) {
|
||||
retEntries.push(`${JSON.stringify(meta)}\n${content}`);
|
||||
}
|
||||
}
|
||||
|
||||
const entries = journalEntries.get(journalUid)! as EntriesListData;
|
||||
const syncEntries = entries.map((entry: EteSync.Entry) => {
|
||||
const syncEntry = entry.getSyncEntry(cryptoManager, prevUid);
|
||||
prevUid = entry.uid;
|
||||
|
||||
return (wantAll || wantedEntries[entry.uid]) ? syncEntry : undefined;
|
||||
}).filter((x) => x !== undefined);
|
||||
|
||||
setResult(syncEntries.map((ent) => ent?.content).join("\n\n"));
|
||||
setResult(retEntries.join("\n\n"));
|
||||
}}
|
||||
>
|
||||
Decrypt
|
||||
|
|
|
@ -135,10 +135,7 @@ export default function SyncGate() {
|
|||
path={routeResolver.getRoute("debug")}
|
||||
exact
|
||||
render={() => (
|
||||
<Debug
|
||||
etesync={etesync}
|
||||
userInfo={userInfo}
|
||||
/>
|
||||
<Debug />
|
||||
)}
|
||||
/>
|
||||
</Switch>
|
||||
|
|
Loading…
Reference in New Issue