Skip to content

Commit 78269f5

Browse files
committed
chore: use Temporal and enhance username in quote footer
1 parent 85f432f commit 78269f5

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

src/ps/commands/quotes.tsx

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { escapeHTML, formatText, toRoomID } from 'ps-client/tools';
2+
import { Temporal } from '@js-temporal/polyfill';
23

34
import { PSQuoteRoomPrefs } from '@/cache';
45
import { isGlobalBot, prefix } from '@/config/ps';
@@ -192,9 +193,11 @@ function FormatQuote({
192193
)}
193194
</div>
194195
{addedBy && dateAdded && (
195-
<div style={{ fontSize: 10, opacity: 0.6, marginTop: 6 }}>
196-
Added by {addedBy} on {dateAdded}
197-
</div>
196+
<>
197+
<span style={{ fontSize: 10, opacity: 0.6, marginTop: 6 }}>Added by&nbsp;</span>
198+
<UsernamePS name={addedBy} style={{ fontSize: 10 }} clickable />
199+
<span style={{ fontSize: 10, opacity: 0.6, marginTop: 6 }}>&nbsp;on {dateAdded}</span>
200+
</>
198201
)}
199202
</>
200203
);
@@ -236,9 +239,14 @@ function MultiQuotes({
236239
})}${total > list.length ? ` of ${total} total` : ''})`;
237240
const title = baseTitle ? `${baseTitle} ${suffix}` : pageNum ? `Page ${pageNum} of ${pageCount} ${suffix}` : `All Quotes`;
238241
const quotes = list
239-
.map(([index, quote]) => (
240-
<FormatQuote quote={quote.quote} header={`#${index}`} addedBy={quote.addedBy} dateAdded={quote.at.toDateString()} />
241-
))
242+
.map(([index, quote]) => {
243+
const dateAdded = Temporal.Instant.fromEpochMilliseconds(quote.at.getTime())
244+
.toZonedDateTimeISO('UTC')
245+
.toPlainDate()
246+
.toLocaleString('en-GB');
247+
248+
return <FormatQuote quote={quote.quote} header={`#${index}`} addedBy={quote.addedBy} dateAdded={dateAdded} />;
249+
})
242250
.space(<hr />, !useDropdown);
243251

244252
const content = useDropdown ? (
@@ -249,7 +257,7 @@ function MultiQuotes({
249257
<h3 style={{ display: 'inline-block' }}>{title}</h3>
250258
</summary>
251259
<hr />
252-
{quotes}
260+
<>{quotes}</>
253261
</details>
254262
<hr />
255263
</>
@@ -318,15 +326,15 @@ export const command: PSCommand = {
318326
);
319327
if (!matchingQuotes.length) return broadcast($T('COMMANDS.QUOTES.NO_QUOTES_FOUND_MATCHING', { search: arg }));
320328
const [index, randQuote] = matchingQuotes.random()!;
329+
const dateAdded = Temporal.Instant.fromEpochMilliseconds(randQuote.at.getTime())
330+
.toZonedDateTimeISO('UTC')
331+
.toPlainDate()
332+
.toLocaleString('en-GB');
333+
321334
broadcastHTML(
322335
<>
323336
<hr />
324-
<FormatQuote
325-
quote={randQuote.quote}
326-
header={`#${+index + 1}`}
327-
addedBy={randQuote.addedBy}
328-
dateAdded={randQuote.at.toDateString()}
329-
/>
337+
<FormatQuote quote={randQuote.quote} header={`#${+index + 1}`} addedBy={randQuote.addedBy} dateAdded={dateAdded} />
330338
<hr />
331339
</>,
332340
{ name: `viewquote-${message.parent.status.userid}` }
@@ -346,7 +354,7 @@ export const command: PSCommand = {
346354
async run({ message, arg, broadcastHTML, $T }) {
347355
const parsedQuote = parseQuote(arg);
348356
const addedBy = message.author.name;
349-
const at = new Date(message.time).toDateString();
357+
const at = Temporal.Now.plainDateISO('UTC').toLocaleString('en-GB');
350358

351359
const rendered = jsxToHTML(<FormatQuote quote={parsedQuote} addedBy={addedBy} dateAdded={at} />);
352360

@@ -422,15 +430,15 @@ export const command: PSCommand = {
422430
const quotes = await getAllQuotes(room);
423431
if (!quotes.length) return broadcast($T('COMMANDS.QUOTES.NO_QUOTES_FOUND'));
424432
const lastQuote = quotes[quotes.length - 1];
433+
const dateAdded = Temporal.Instant.fromEpochMilliseconds(lastQuote.at.getTime())
434+
.toZonedDateTimeISO('UTC')
435+
.toPlainDate()
436+
.toLocaleString('en-GB');
437+
425438
broadcastHTML(
426439
<>
427440
<hr />
428-
<FormatQuote
429-
quote={lastQuote.quote}
430-
header={`#${quotes.length}`}
431-
addedBy={lastQuote.addedBy}
432-
dateAdded={lastQuote.at.toDateString()}
433-
/>
441+
<FormatQuote quote={lastQuote.quote} header={`#${quotes.length}`} addedBy={lastQuote.addedBy} dateAdded={dateAdded} />
434442
<hr />
435443
</>,
436444
{ name: `viewquote-${message.parent.status.userid}` }
@@ -552,6 +560,10 @@ export const command: PSCommand = {
552560
toDelete = matching[0];
553561
indexToDelete = quotes.indexOf(toDelete);
554562
}
563+
const dateAdded = Temporal.Instant.fromEpochMilliseconds(quotes[indexToDelete].at.getTime())
564+
.toZonedDateTimeISO('UTC')
565+
.toPlainDate()
566+
.toLocaleString('en-GB');
555567

556568
broadcastHTML(
557569
<>
@@ -560,7 +572,7 @@ export const command: PSCommand = {
560572
quote={quotes[indexToDelete].quote}
561573
header={`Deleting #${indexToDelete + 1}:`}
562574
addedBy={quotes[indexToDelete].addedBy}
563-
dateAdded={new Date(quotes[indexToDelete].at).toDateString()}
575+
dateAdded={dateAdded}
564576
/>
565577
<hr />
566578
</>
@@ -610,10 +622,15 @@ export const command: PSCommand = {
610622
throw new ChatError('Invalid quote index.' as ToTranslate);
611623
}
612624
const quote = quotes[index - 1];
625+
const dateAdded = Temporal.Instant.fromEpochMilliseconds(quote.at.getTime())
626+
.toZonedDateTimeISO('UTC')
627+
.toPlainDate()
628+
.toLocaleString('en-GB');
629+
613630
return broadcastHTML(
614631
<>
615632
<hr />
616-
<FormatQuote quote={quote.quote} header={`#${index}`} addedBy={quote.addedBy} dateAdded={quote.at.toDateString()} />
633+
<FormatQuote quote={quote.quote} header={`#${index}`} addedBy={quote.addedBy} dateAdded={dateAdded} />
617634
<hr />
618635
</>,
619636
{ name: `viewquote-${message.parent.status.userid}` }

0 commit comments

Comments
 (0)