This commit is contained in:
2023-12-10 16:53:07 +07:00
parent f9402f5c4f
commit 132ea7f887
30 changed files with 53 additions and 189 deletions

View File

@@ -1,2 +0,0 @@
export const nHoursAgo = (hrs: number): number =>
Math.floor((Date.now() - hrs * 60 * 60 * 1000) / 1000);

View File

@@ -1,6 +1,7 @@
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import updateLocale from 'dayjs/plugin/updateLocale';
import { nip19 } from 'nostr-tools';
dayjs.extend(relativeTime);
dayjs.extend(updateLocale);
@@ -18,8 +19,8 @@ dayjs.updateLocale('en', {
},
});
export function formatCreatedAt(time, message = false) {
let formated;
export function formatCreatedAt(time: number, message: boolean = false) {
let formated: string;
const now = dayjs();
const inputTime = dayjs.unix(time);
@@ -41,3 +42,20 @@ export function formatCreatedAt(time, message = false) {
return formated;
}
export function displayNpub(pubkey: string, len: number, separator?: string) {
const npub = pubkey.startsWith('npub1') ? pubkey : (nip19.npubEncode(pubkey) as string);
if (npub.length <= len) return npub;
separator = separator || ' ... ';
const sepLen = separator.length,
charsToShow = len - sepLen,
frontChars = Math.ceil(charsToShow / 2),
backChars = Math.floor(charsToShow / 2);
return npub.substr(0, frontChars) + separator + npub.substr(npub.length - backChars);
}
// convert number to K, M, B, T, etc.
export const compactNumber = Intl.NumberFormat('en', { notation: 'compact' });

View File

@@ -1,2 +0,0 @@
// convert number to K, M, B, T, etc.
export const compactNumber = Intl.NumberFormat('en', { notation: 'compact' });

View File

@@ -1,20 +0,0 @@
import { nip19 } from 'nostr-tools';
export function shortenKey(pubkey: string) {
const npub = nip19.npubEncode(pubkey);
return npub.substring(0, 16).concat('...');
}
export function displayNpub(pubkey: string, len: number, separator?: string) {
const npub = pubkey.startsWith('npub1') ? pubkey : (nip19.npubEncode(pubkey) as string);
if (npub.length <= len) return npub;
separator = separator || ' ... ';
const sepLen = separator.length,
charsToShow = len - sepLen,
frontChars = Math.ceil(charsToShow / 2),
backChars = Math.floor(charsToShow / 2);
return npub.substr(0, frontChars) + separator + npub.substr(npub.length - backChars);
}

View File

@@ -1,44 +0,0 @@
import { NDKEvent, NDKTag, NostrEvent } from '@nostr-dev-kit/ndk';
// convert array to NIP-02 tag list
export function arrayToNIP02(arr: string[]) {
const nip02_arr = [];
arr.forEach((item) => {
nip02_arr.push(['p', item]);
});
return nip02_arr;
}
// get repost id from event tags
export function getRepostID(tags: NDKTag[]) {
let quoteID = null;
if (tags.length > 0) {
if (tags[0][0] === 'e') {
quoteID = tags[0][1];
} else {
quoteID = tags.find((t) => t[0] === 'e')?.[1];
}
}
return quoteID;
}
// get random n elements from array
export function getMultipleRandom(arr: string[], num: number) {
const shuffled = [...arr].sort(() => 0.5 - Math.random());
return shuffled.slice(0, num);
}
export function rawEvent(event: NDKEvent) {
return {
created_at: event.created_at,
content: event.content,
tags: event.tags,
kind: event.kind,
pubkey: event.pubkey,
id: event.id,
sig: event.sig,
} as NostrEvent;
}

16
src/utils/types.d.ts vendored
View File

@@ -9,28 +9,12 @@ export interface RichContent {
notes: string[];
}
export interface DBEvent {
id: string;
account_id: number;
event: string | NDKEvent;
author: string;
kind: number;
root_id: string;
reply_id: string;
created_at: number;
richContent?: RichContent;
}
export interface Account {
id: string;
pubkey: string;
is_active: number;
contacts: string[];
relayList: NDKRelayList;
/**
* @deprecated Use contacts instead
*/
follows: string[];
}
export interface WidgetGroup {