diff --git a/src/App.tsx b/src/App.tsx index a166c6f..70a4fdd 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import React, { useEffect, useState } from 'react'; import { createKeyPair, Session, wasm } from 'p2panda-js'; -import { BambooLog } from '~/components/BambooLog'; +// import { BambooLog } from '~/components/BambooLog'; import { Chatlog } from '~/components/Chatlog'; import { ENDPOINT, CHAT_SCHEMA } from '~/configs'; import { Instructions } from '~/components/Instructions'; @@ -13,22 +13,48 @@ import '~/styles.css'; const session = new Session(ENDPOINT); let syncInterval: number; +type Document = { + _meta: { + author: string; + deleted: boolean; + edited: boolean; + entries: string[]; + id: string; + last_operation: string; + schema: string; + }; +}; + +type Bookmark = Document & { + created: string; + title: string; + url: string; +}; + const App = (): JSX.Element => { const [currentMessage, setCurrentMessage] = useState(''); const [debugEntry, setDebugEntry] = useState(null); const [keyPair, setKeyPair] = useState(null); - const [entries, setEntries] = useState([]); + const [entries, setEntries] = useState([]); const [isSyncToggled, setSyncToggled] = useState(true); + const [error, setError] = useState(null); const syncEntries = async () => { - const unsortedEntries = await session.queryEntries(CHAT_SCHEMA); + setError(null); + + let unsortedEntries: Bookmark[] = []; + try { + unsortedEntries = await session.query({ schema: CHAT_SCHEMA }); + } catch (err) { + setError(err.message); + setSyncToggled(false); + console.log('Error fetching entries', err); + } setEntries( - unsortedEntries.sort( - ({ operation: operationA }, { operation: operationB }) => { - return operationA.fields.date > operationB.fields.date ? 1 : -1; - }, - ), + unsortedEntries.sort(({ created: createdA }, { created: createdB }) => { + return new Date(createdA) > new Date(createdB) ? 1 : -1; + }), ); }; @@ -60,10 +86,13 @@ const App = (): JSX.Element => { // Publish entries and refresh chat log to get the new message in the log const handlePublish = async (message: string) => { + const [url, ...titleSegments] = message.split(' '); + const title = titleSegments.join(' '); await session.create( { - message, - date: new Date().toISOString(), + url: url, + title: title, + created: new Date().toISOString(), }, { schema: CHAT_SCHEMA, session, keyPair }, ); @@ -71,9 +100,9 @@ const App = (): JSX.Element => { }; // Filter my personal entries - const myEntries = entries.filter(({ encoded: entry }) => { - return entry.author === keyPair.publicKey(); - }); + // const myEntries = entries.filter(({ _meta: { author } }) => { + // return author === keyPair.publicKey(); + // }); return (
@@ -94,8 +123,8 @@ const App = (): JSX.Element => { setDebugEntry={setDebugEntry} isSyncToggled={isSyncToggled} toggleSync={() => setSyncToggled(!isSyncToggled)} + error={error} /> -
); diff --git a/src/components/BambooLog/LogEntry.tsx b/src/components/BambooLog/LogEntry.tsx index 962ecb0..4181300 100644 --- a/src/components/BambooLog/LogEntry.tsx +++ b/src/components/BambooLog/LogEntry.tsx @@ -25,14 +25,14 @@ export const LogEntry = ({ entry }: Props): JSX.Element => {
{entry.seqNum}
-
{formatEntryHash(entry.encoded.entryHash)}
+
{formatEntryHash(entry._meta.id)}
{operation.action}
  • Schema: {formatCheckHash(operation.schema)}
  • -
  • Message: {formatMessage(operation.fields.message)}
  • +
  • Message: {formatMessage(operation.fields.url)}
  • BackLink: {formatCheckHash(entryHashBacklink)}
  • SkipLink: {formatCheckHash(entryHashSkiplink)}
diff --git a/src/components/BambooLog/index.tsx b/src/components/BambooLog/index.tsx index 48284d6..6d46e63 100644 --- a/src/components/BambooLog/index.tsx +++ b/src/components/BambooLog/index.tsx @@ -16,7 +16,7 @@ export const BambooLog = ({ log, setDebugEntry }: Props): JSX.Element => ( {log.map((entry) => (
setDebugEntry(entry)} > diff --git a/src/components/Chatlog/index.tsx b/src/components/Chatlog/index.tsx index ce4af08..9b04eb6 100644 --- a/src/components/Chatlog/index.tsx +++ b/src/components/Chatlog/index.tsx @@ -11,9 +11,10 @@ type Props = { handlePublish: (message: string) => Promise; isSyncToggled: boolean; toggleSync: () => void; + error?: string; }; -const formatAuthor = ({ author }) => `${author.slice(0, 6)}`; +const formatAuthor = (author: string) => `${author.slice(0, 6)}`; export const Chatlog = ({ log, @@ -22,6 +23,7 @@ export const Chatlog = ({ handlePublish, isSyncToggled, toggleSync, + error, }: Props): JSX.Element => (

Message Log

{' '} @@ -35,17 +37,21 @@ export const Chatlog = ({ Sync
+ {error &&
{error}
}
- {log.slice(-10).map((entry) => ( -
setDebugEntry(entry)} - > -

- {formatAuthor(entry.encoded)}: {entry.operation.fields.message} -

-
- ))} + {log + .slice(-10) + .reverse() + .map((entry) => ( +
setDebugEntry(entry)}> +

+ {formatAuthor(entry._meta.author)}:{' '} + + {entry.title} + +

+
+ ))}
); diff --git a/src/styles.css b/src/styles.css index 477091e..f518b04 100644 --- a/src/styles.css +++ b/src/styles.css @@ -112,3 +112,10 @@ input[type="submit"] { color: chartreuse; font-weight: 800; } + +.error-message { + outline: 1px solid red; + background-color: #fafafa; + padding: 5px 1em; + margin: 1em 1px; +}