Skip to content

Commit 3e8dfee

Browse files
committed
Add votes
1 parent e998c23 commit 3e8dfee

9 files changed

Lines changed: 335 additions & 29 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Arguments:
5959
- Useful for notifying the author of a website (http anchor)
6060
- `disable`: comma-separated string of features to disable, all enabled by default
6161
- `likes`
62+
- `votes`
6263
- `zaps`
6364
- `reply` (when disabled the component becomes read-only)
6465
- `publish` (when disabled does not send event to relays, useful for testing)
@@ -106,6 +107,9 @@ document.querySelector('zap-threads').shadowRoot.appendChild(style);
106107

107108
Any questions or ideas, please open an issue!
108109

110+
## Icons
111+
- [Font Awesome](https://fontawesome.com/license/free)
112+
109113
## LICENSE
110114

111115
This is free and unencumbered software released into the public domain.
@@ -131,4 +135,4 @@ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
131135
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
132136
OTHER DEALINGS IN THE SOFTWARE.
133137

134-
For more information, please refer to <http://unlicense.org/>
138+
For more information, please refer to <http://unlicense.org/>

src/index.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { clear as clearCache, find, findAll, save, watchAll } from "./util/db.ts
1111
import { decode } from "nostr-tools/nip19";
1212
import { finalizeEvent, getPublicKey } from "nostr-tools/pure";
1313
import { Filter } from "nostr-tools/filter";
14-
import { AggregateEvent, NoteEvent, eventToNoteEvent } from "./util/models.ts";
14+
import { AggregateEvent, NoteEvent, eventToNoteEvent, eventToReactionEvent, reactionKind } from "./util/models.ts";
1515
import { SubCloser } from "nostr-tools";
1616

1717
const ZapThreads = (props: { [key: string]: string; }) => {
@@ -225,6 +225,12 @@ const ZapThreads = (props: { [key: string]: string; }) => {
225225
}
226226
} else if (e.kind === 7) {
227227
newLikeIds.add(e.id);
228+
if (e.content.trim()) {
229+
const reactionEvent = eventToReactionEvent(e);
230+
if (reactionKind(reactionEvent) !== 0) { // remove this condition if you want to track all reactions
231+
save('reactions', reactionEvent);
232+
}
233+
}
228234
} else if (e.kind === 9735) {
229235
const invoiceTag = e.tags.find(t => t[0] === "bolt11");
230236
invoiceTag && invoiceTag[1] && (newZaps[e.id] = invoiceTag[1]);
@@ -369,6 +375,9 @@ const ZapThreads = (props: { [key: string]: string; }) => {
369375
return nestedEvents().reduce((acc, n) => acc + totalChildren(n), nestedEvents().length);
370376
};
371377

378+
const reactions = watchAll(() => ['reactions']);
379+
const votes = () => reactions().filter(r => reactionKind(r) !== 0);
380+
372381
const [showAdvanced, setShowAdvanced] = createSignal(false);
373382

374383
return <>
@@ -388,7 +397,7 @@ const ZapThreads = (props: { [key: string]: string; }) => {
388397
<h2 id="ztr-title">
389398
{commentsLength() > 0 && `${commentsLength()} comment${commentsLength() == 1 ? '' : 's'}`}
390399
</h2>
391-
<Thread nestedEvents={nestedEvents} articles={articles} />
400+
<Thread nestedEvents={nestedEvents} articles={articles} votes={votes} />
392401
</>}
393402

394403
<div style="float:right; opacity: 0.2;" onClick={() => setShowAdvanced(!showAdvanced())}>{ellipsisSvg()}</div>

src/reply.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaultPicture, generateTags, satsAbbrev, shortenEncodedId, updateProfiles } from "./util/ui.ts";
1+
import { currentTime, defaultPicture, generateTags, satsAbbrev, shortenEncodedId, updateProfiles } from "./util/ui.ts";
22
import { Show, createEffect, createSignal } from "solid-js";
33
import { UnsignedEvent, Event } from "nostr-tools/core";
44
import { EventSigner, pool, signersStore, store } from "./util/stores.ts";
@@ -100,7 +100,7 @@ export const ReplyEditor = (props: { replyTo?: string; onDone?: Function; }) =>
100100

101101
const unsignedEvent: UnsignedEvent = {
102102
kind: 1,
103-
created_at: Math.round(Date.now() / 1000),
103+
created_at: currentTime(),
104104
content: content,
105105
pubkey: signer.pk,
106106
tags: generateTags(content),
@@ -140,7 +140,7 @@ export const ReplyEditor = (props: { replyTo?: string; onDone?: Function; }) =>
140140
const url = normalizeURL(anchor().value);
141141
const unsignedRootEvent: UnsignedEvent = {
142142
pubkey: signer.pk,
143-
created_at: Math.round(Date.now() / 1000),
143+
created_at: currentTime(),
144144
kind: 8812,
145145
tags: [['r', url]],
146146
content: `Comments on ${url} ↴`
@@ -277,4 +277,4 @@ export const RootComment = () => {
277277
<ReplyEditor />
278278
</div>
279279
</div>;
280-
};
280+
};

src/styles/index.css

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,21 @@ ul.ztr-comment-actions,
187187
align-items: center;
188188
list-style: none;
189189
margin: 0;
190-
padding: 0 0 0 3.5em;
190+
padding: 0 0 0 2.625em;
191191
font-weight: 600;
192192
user-select: none;
193193
}
194194

195+
ul.ztr-comment-actions {
196+
margin-top: -1em;
197+
}
198+
195199
ul.ztr-comment-actions li {
196200
display: inline-flex;
197201
align-items: center;
198202
text-align: center;
199-
padding: 0 2em 0 0;
203+
padding: 1em;
204+
margin: -0.125em;
200205
cursor: pointer;
201206
}
202207

@@ -211,6 +216,31 @@ svg {
211216
height: 1.1em;
212217
}
213218

219+
li.ztr-comment-action-upvote {
220+
margin-left: 0.5em;
221+
margin-right: 0.5em;
222+
}
223+
224+
li.ztr-comment-action-downvote {
225+
margin-left: 0.5em;
226+
margin-right: 0.5em;
227+
}
228+
229+
li.ztr-comment-action-reply {
230+
margin-left: 0.7em;
231+
margin-right: 0.7em;
232+
}
233+
234+
li.ztr-comment-action-zap {
235+
margin-left: 0.7em;
236+
margin-right: 0.7em;
237+
}
238+
239+
li.ztr-comment-action-like {
240+
margin-left: 0.7em;
241+
margin-right: 0.7em;
242+
}
243+
214244
.ztr-comment-action-reply:hover svg {
215245
fill: #92379c;
216246
}
@@ -235,6 +265,43 @@ svg {
235265
color: #e35428;
236266
}
237267

268+
.ztr-comment-action-upvote:hover svg {
269+
fill: #0288d1;
270+
}
271+
272+
.ztr-comment-action-upvote:hover span {
273+
color: #0288d1;
274+
}
275+
276+
.ztr-comment-action-upvote.selected {
277+
svg {
278+
fill: #0288d1;
279+
}
280+
}
281+
282+
.ztr-comment-action-downvote:hover svg {
283+
fill: #0288d1;
284+
}
285+
286+
.ztr-comment-action-downvote:hover span {
287+
color: #0288d1;
288+
}
289+
290+
.ztr-comment-action-downvote.selected {
291+
svg {
292+
fill: #0288d1;
293+
}
294+
}
295+
296+
li.ztr-comment-action-votes {
297+
justify-content: center;
298+
span {
299+
margin-left: -0.7em;
300+
margin-right: -0.7em;
301+
font-size: 0.85em;
302+
}
303+
}
304+
238305
.ztr-reply-form {
239306
padding: 0 0.5em 0 3.5em;
240307
}
@@ -401,4 +468,4 @@ svg {
401468
.ztr-comment-action-reply:hover span {
402469
color: #96609c;
403470
}
404-
}
471+
}

0 commit comments

Comments
 (0)