Server: add note field in the UI, fixes #1

This commit is contained in:
Val Packett 2023-07-20 16:37:34 -03:00
parent 92d58ad1ad
commit 5b315fed61
4 changed files with 19 additions and 10 deletions

View file

@ -154,9 +154,12 @@ export class TiddlyPWASyncApp {
}
@adminAuth
handleCreate(_: unknown) {
handleCreate({ note }: Record<string, unknown>) {
if (note !== undefined && typeof note !== 'string') {
return Response.json({ error: 'EPROTO' }, { headers: respHdrs, status: 400 });
}
const token = base64.encode(crypto.getRandomValues(new Uint8Array(32)));
this.db.createWiki(token);
this.db.createWiki(token, note);
return Response.json({ token }, { headers: respHdrs, status: 201 });
}

2
server/data.d.ts vendored
View file

@ -29,7 +29,7 @@ export interface Datastore {
getWiki(token: string): Wiki | undefined;
getWikiByPrefix(halftoken: string): Wiki | undefined;
listWikis(): Array<Wiki>;
createWiki(token: string): void;
createWiki(token: string, note?: string): void;
updateWikiAuthcode(token: string, authcode?: string): void;
updateWikiSalt(token: string, salt: string): void;
deleteWiki(token: string): void;

View file

@ -9,7 +9,7 @@ export const homePage = html`
<style>
* { box-sizing: border-box; }
html { background: #252525; color: #fbfbfb; -webkit-text-size-adjust: none; text-size-adjust: none; accent-color: limegreen; }
body { margin: 2rem auto; min-width: 300px; max-width: 99ch; line-height: 1.5; word-wrap: break-word; font-family: system-ui, sans-serif; }
body { margin: 2rem auto; min-width: 300px; max-width: 120ch; line-height: 1.5; word-wrap: break-word; font-family: system-ui, sans-serif; }
a { color: limegreen; }
a:hover { color: lime; }
h1 { font: 1.25rem monospace; text-align: center; color: limegreen; margin-bottom: 2rem; }
@ -20,7 +20,7 @@ export const homePage = html`
table { border-collapse: collapse; margin: 1rem 0; }
td { padding: 0.25rem 0.6rem; }
tr:nth-child(even) { background: rgba(255,255,255,.08); }
#wikirows td:first-of-type, #wikirows td:nth-of-type(2) { font-family: monospace; }
#wikirows td:nth-of-type(2), #wikirows td:nth-of-type(3) { font-family: monospace; }
</style>
</head>
<body>
@ -37,6 +37,7 @@ export const homePage = html`
<table>
<thead>
<tr>
<td>Note</td>
<td>Token</td>
<td>Salt</td>
<td>Content Size</td>
@ -89,8 +90,11 @@ export const homePage = html`
const { wikis } = await resp.json();
const wikirows = document.getElementById('wikirows')
wikirows.replaceChildren();
for (const { token, salt, tidsize, appsize } of wikis) {
for (const { token, note, salt, tidsize, appsize } of wikis) {
const tr = document.createElement('tr');
const noteTd = document.createElement('td');
noteTd.innerText = note;
tr.appendChild(noteTd);
const tokenTd = document.createElement('td');
tokenTd.innerText = token;
tr.appendChild(tokenTd);
@ -159,7 +163,7 @@ export const homePage = html`
});
};
createBtn.onclick = () => {
serverReq({ op: 'create' }).then(() => refreshBtn.click());
serverReq({ op: 'create', note: prompt('Leave a note about this wiki if you want?') || '' }).then(() => refreshBtn.click());
}
});
</script>

View file

@ -82,7 +82,9 @@ export class SQLiteDatastore extends DB implements Datastore {
}
listWikis() {
return this.queryEntries<{ token: string; authcode?: string; salt?: string; tidsize: number; appsize: number }>(sql`
return this.queryEntries<
{ token: string; authcode?: string; salt?: string; note?: string; tidsize: number; appsize: number }
>(sql`
SELECT token, authcode, salt, note, (
SELECT sum(length(thash) + length(title) + length(tiv) + length(data) + length(iv))
FROM tiddlers
@ -93,8 +95,8 @@ export class SQLiteDatastore extends DB implements Datastore {
`);
}
createWiki(token: string) {
this.query(sql`INSERT INTO wikis (token) VALUES (:token)`, { token });
createWiki(token: string, note?: string) {
this.query(sql`INSERT INTO wikis (token, note) VALUES (:token, :note)`, { token, note });
}
updateWikiAuthcode(token: string, authcode?: string) {