- {profile.nip05 || shortenKey(data.pubkey)} + {profile.nip05 || displayNpub(data.pubkey, 16)}
diff --git a/src/shared/userProfile.tsx b/src/shared/widgets/other/userProfile.tsx similarity index 98% rename from src/shared/userProfile.tsx rename to src/shared/widgets/other/userProfile.tsx index c66799ac..4305018d 100644 --- a/src/shared/userProfile.tsx +++ b/src/shared/widgets/other/userProfile.tsx @@ -6,8 +6,8 @@ import { useArk } from '@libs/ark'; import { NIP05 } from '@shared/nip05'; +import { displayNpub } from '@utils/formater'; import { useProfile } from '@utils/hooks/useProfile'; -import { displayNpub } from '@utils/shortenKey'; export function UserProfile({ pubkey }: { pubkey: string }) { const { ark } = useArk(); diff --git a/src/shared/widgets/user.tsx b/src/shared/widgets/user.tsx index 8d0b4714..a5d836b4 100644 --- a/src/shared/widgets/user.tsx +++ b/src/shared/widgets/user.tsx @@ -13,8 +13,7 @@ import { UnknownNote, } from '@shared/notes'; import { TitleBar } from '@shared/titleBar'; -import { UserProfile } from '@shared/userProfile'; -import { WidgetWrapper } from '@shared/widgets'; +import { UserProfile, WidgetWrapper } from '@shared/widgets'; import { FETCH_LIMIT } from '@utils/constants'; import { Widget } from '@utils/types'; diff --git a/src/utils/date.ts b/src/utils/date.ts deleted file mode 100644 index 77657a17..00000000 --- a/src/utils/date.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const nHoursAgo = (hrs: number): number => - Math.floor((Date.now() - hrs * 60 * 60 * 1000) / 1000); diff --git a/src/utils/createdAt.ts b/src/utils/formater.ts similarity index 51% rename from src/utils/createdAt.ts rename to src/utils/formater.ts index a752ade2..b33fcbf7 100644 --- a/src/utils/createdAt.ts +++ b/src/utils/formater.ts @@ -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' }); diff --git a/src/utils/number.ts b/src/utils/number.ts deleted file mode 100644 index 32994d2f..00000000 --- a/src/utils/number.ts +++ /dev/null @@ -1,2 +0,0 @@ -// convert number to K, M, B, T, etc. -export const compactNumber = Intl.NumberFormat('en', { notation: 'compact' }); diff --git a/src/utils/shortenKey.ts b/src/utils/shortenKey.ts deleted file mode 100644 index 42f7bad3..00000000 --- a/src/utils/shortenKey.ts +++ /dev/null @@ -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); -} diff --git a/src/utils/transform.ts b/src/utils/transform.ts deleted file mode 100644 index 0013007a..00000000 --- a/src/utils/transform.ts +++ /dev/null @@ -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; -} diff --git a/src/utils/types.d.ts b/src/utils/types.d.ts index cccaf9af..01ceb2d0 100644 --- a/src/utils/types.d.ts +++ b/src/utils/types.d.ts @@ -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 {