From 132ea7f887b9b92907d4f970626ae27d023336db Mon Sep 17 00:00:00 2001 From: reya Date: Sun, 10 Dec 2023 16:53:07 +0700 Subject: [PATCH] clean up --- src/app.tsx | 12 +---- .../auth/components}/avatarUploader.tsx | 0 src/app/auth/create.tsx | 3 +- src/app/chats/components/chatListItem.tsx | 3 +- src/app/new/components/mentionPopupItem.tsx | 2 +- src/app/settings/components/contactCard.tsx | 2 +- src/app/settings/components/postCard.tsx | 2 +- src/app/settings/components/profileCard.tsx | 2 +- src/app/settings/components/relayCard.tsx | 2 +- src/app/settings/components/zapCard.tsx | 2 +- src/app/users/components/profile.tsx | 2 +- src/app/users/components/stats.tsx | 2 +- src/shared/accounts/active.tsx | 12 ++++- src/shared/bannerUploader.tsx | 52 ------------------- src/shared/navigation.tsx | 2 +- src/shared/networkStatusIndicator.tsx | 16 ------ src/shared/nip05.tsx | 2 +- src/shared/notes/actions/zap.tsx | 3 +- src/shared/notes/notify.tsx | 2 +- src/shared/user.tsx | 3 +- src/shared/widgets/index.ts | 1 + .../widgets/other/nostrBandUserProfile.tsx | 4 +- .../{ => widgets/other}/userProfile.tsx | 2 +- src/shared/widgets/user.tsx | 3 +- src/utils/date.ts | 2 - src/utils/{createdAt.ts => formater.ts} | 22 +++++++- src/utils/number.ts | 2 - src/utils/shortenKey.ts | 20 ------- src/utils/transform.ts | 44 ---------------- src/utils/types.d.ts | 16 ------ 30 files changed, 53 insertions(+), 189 deletions(-) rename src/{shared => app/auth/components}/avatarUploader.tsx (100%) delete mode 100644 src/shared/bannerUploader.tsx delete mode 100644 src/shared/networkStatusIndicator.tsx rename src/shared/{ => widgets/other}/userProfile.tsx (98%) delete mode 100644 src/utils/date.ts rename src/utils/{createdAt.ts => formater.ts} (51%) delete mode 100644 src/utils/number.ts delete mode 100644 src/utils/shortenKey.ts delete mode 100644 src/utils/transform.ts diff --git a/src/app.tsx b/src/app.tsx index 3165aa5b..7b6cd157 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -1,4 +1,3 @@ -import { message } from '@tauri-apps/plugin-dialog'; import { fetch } from '@tauri-apps/plugin-http'; import { RouterProvider, createBrowserRouter, defer, redirect } from 'react-router-dom'; @@ -20,15 +19,8 @@ export default function App() { const { ark } = useArk(); const accountLoader = async () => { - try { - // redirect to welcome screen if none user exist - const totalAccount = await ark.checkAccount(); - if (totalAccount === 0) return redirect('/auth/welcome'); - - return null; - } catch (e) { - await message(e, { title: 'An unexpected error has occurred', type: 'error' }); - } + if (!ark.account) return redirect('/auth/welcome'); + return null; }; const relayLoader = async ({ params }) => { diff --git a/src/shared/avatarUploader.tsx b/src/app/auth/components/avatarUploader.tsx similarity index 100% rename from src/shared/avatarUploader.tsx rename to src/app/auth/components/avatarUploader.tsx diff --git a/src/app/auth/create.tsx b/src/app/auth/create.tsx index 100ac88d..872300a7 100644 --- a/src/app/auth/create.tsx +++ b/src/app/auth/create.tsx @@ -11,9 +11,10 @@ import { useForm } from 'react-hook-form'; import { useNavigate } from 'react-router-dom'; import { toast } from 'sonner'; +import { AvatarUploader } from '@app/auth/components/avatarUploader'; + import { useArk } from '@libs/ark'; -import { AvatarUploader } from '@shared/avatarUploader'; import { ArrowLeftIcon, InfoIcon, LoaderIcon } from '@shared/icons'; import { User } from '@shared/user'; diff --git a/src/app/chats/components/chatListItem.tsx b/src/app/chats/components/chatListItem.tsx index 68d827a9..2fa56470 100644 --- a/src/app/chats/components/chatListItem.tsx +++ b/src/app/chats/components/chatListItem.tsx @@ -7,9 +7,8 @@ import { twMerge } from 'tailwind-merge'; import { useDecryptMessage } from '@app/chats/hooks/useDecryptMessage'; -import { formatCreatedAt } from '@utils/createdAt'; +import { displayNpub, formatCreatedAt } from '@utils/formater'; import { useProfile } from '@utils/hooks/useProfile'; -import { displayNpub } from '@utils/shortenKey'; export const ChatListItem = memo(function ChatListItem({ event }: { event: NDKEvent }) { const { isLoading, user } = useProfile(event.pubkey); diff --git a/src/app/new/components/mentionPopupItem.tsx b/src/app/new/components/mentionPopupItem.tsx index f91fe830..46645bff 100644 --- a/src/app/new/components/mentionPopupItem.tsx +++ b/src/app/new/components/mentionPopupItem.tsx @@ -2,8 +2,8 @@ import * as Avatar from '@radix-ui/react-avatar'; import { minidenticon } from 'minidenticons'; import { useMemo } from 'react'; +import { displayNpub } from '@utils/formater'; import { useProfile } from '@utils/hooks/useProfile'; -import { displayNpub } from '@utils/shortenKey'; export function MentionPopupItem({ pubkey, embed }: { pubkey: string; embed?: string }) { const { isLoading, user } = useProfile(pubkey, embed); diff --git a/src/app/settings/components/contactCard.tsx b/src/app/settings/components/contactCard.tsx index eeda0f86..7815e426 100644 --- a/src/app/settings/components/contactCard.tsx +++ b/src/app/settings/components/contactCard.tsx @@ -5,7 +5,7 @@ import { useArk } from '@libs/ark'; import { EditIcon, LoaderIcon } from '@shared/icons'; -import { compactNumber } from '@utils/number'; +import { compactNumber } from '@utils/formater'; export function ContactCard() { const { ark } = useArk(); diff --git a/src/app/settings/components/postCard.tsx b/src/app/settings/components/postCard.tsx index dd344214..2a890abb 100644 --- a/src/app/settings/components/postCard.tsx +++ b/src/app/settings/components/postCard.tsx @@ -6,7 +6,7 @@ import { useArk } from '@libs/ark'; import { LoaderIcon } from '@shared/icons'; -import { compactNumber } from '@utils/number'; +import { compactNumber } from '@utils/formater'; export function PostCard() { const { ark } = useArk(); diff --git a/src/app/settings/components/profileCard.tsx b/src/app/settings/components/profileCard.tsx index bfec4ffd..5967adbf 100644 --- a/src/app/settings/components/profileCard.tsx +++ b/src/app/settings/components/profileCard.tsx @@ -6,8 +6,8 @@ import { useArk } from '@libs/ark'; import { EditIcon, LoaderIcon } from '@shared/icons'; +import { displayNpub } from '@utils/formater'; import { useProfile } from '@utils/hooks/useProfile'; -import { displayNpub } from '@utils/shortenKey'; export function ProfileCard() { const { ark } = useArk(); diff --git a/src/app/settings/components/relayCard.tsx b/src/app/settings/components/relayCard.tsx index b05d3cf7..6a115de5 100644 --- a/src/app/settings/components/relayCard.tsx +++ b/src/app/settings/components/relayCard.tsx @@ -5,7 +5,7 @@ import { useArk } from '@libs/ark'; import { EditIcon, LoaderIcon } from '@shared/icons'; -import { compactNumber } from '@utils/number'; +import { compactNumber } from '@utils/formater'; export function RelayCard() { const { ark } = useArk(); diff --git a/src/app/settings/components/zapCard.tsx b/src/app/settings/components/zapCard.tsx index e8f4a12b..4d5fa823 100644 --- a/src/app/settings/components/zapCard.tsx +++ b/src/app/settings/components/zapCard.tsx @@ -5,7 +5,7 @@ import { useArk } from '@libs/ark'; import { LoaderIcon } from '@shared/icons'; -import { compactNumber } from '@utils/number'; +import { compactNumber } from '@utils/formater'; export function ZapCard() { const { ark } = useArk(); diff --git a/src/app/users/components/profile.tsx b/src/app/users/components/profile.tsx index 910c909a..7fb7129b 100644 --- a/src/app/users/components/profile.tsx +++ b/src/app/users/components/profile.tsx @@ -10,8 +10,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/app/users/components/stats.tsx b/src/app/users/components/stats.tsx index a46b18f5..a9ceacd1 100644 --- a/src/app/users/components/stats.tsx +++ b/src/app/users/components/stats.tsx @@ -3,7 +3,7 @@ import { fetch } from '@tauri-apps/plugin-http'; import { LoaderIcon } from '@shared/icons'; -import { compactNumber } from '@utils/number'; +import { compactNumber } from '@utils/formater'; export function UserStats({ pubkey }: { pubkey: string }) { const { status, data } = useQuery({ diff --git a/src/shared/accounts/active.tsx b/src/shared/accounts/active.tsx index 23e95665..55c3d446 100644 --- a/src/shared/accounts/active.tsx +++ b/src/shared/accounts/active.tsx @@ -1,18 +1,21 @@ import * as Avatar from '@radix-ui/react-avatar'; import { minidenticon } from 'minidenticons'; import { Link } from 'react-router-dom'; +import { twMerge } from 'tailwind-merge'; import { useArk } from '@libs/ark'; import { AccountMoreActions } from '@shared/accounts/more'; -import { NetworkStatusIndicator } from '@shared/networkStatusIndicator'; +import { useNetworkStatus } from '@utils/hooks/useNetworkStatus'; import { useProfile } from '@utils/hooks/useProfile'; export function ActiveAccount() { const { ark } = useArk(); const { user } = useProfile(ark.account.pubkey); + const isOnline = useNetworkStatus(); + const svgURI = 'data:image/svg+xml;utf8,' + encodeURIComponent(minidenticon(ark.account.pubkey, 90, 50)); @@ -37,7 +40,12 @@ export function ActiveAccount() { /> - + diff --git a/src/shared/bannerUploader.tsx b/src/shared/bannerUploader.tsx deleted file mode 100644 index 17282caf..00000000 --- a/src/shared/bannerUploader.tsx +++ /dev/null @@ -1,52 +0,0 @@ -import { message } from '@tauri-apps/plugin-dialog'; -import { Dispatch, SetStateAction, useState } from 'react'; - -import { useArk } from '@libs/ark'; - -import { LoaderIcon, PlusIcon } from '@shared/icons'; - -export function BannerUploader({ - setBanner, -}: { - setBanner: Dispatch>; -}) { - const { ark } = useArk(); - const [loading, setLoading] = useState(false); - - const uploadBanner = async () => { - try { - // start loading - setLoading(true); - - const image = await ark.upload({}); - - if (image) { - setBanner(image); - setLoading(false); - } - - return; - } catch (e) { - // stop loading - setLoading(false); - await message(`Upload failed, error: ${e}`, { title: 'Lume', type: 'error' }); - } - }; - - return ( - - ); -} diff --git a/src/shared/navigation.tsx b/src/shared/navigation.tsx index 5de0aa1e..19ec9e30 100644 --- a/src/shared/navigation.tsx +++ b/src/shared/navigation.tsx @@ -4,7 +4,7 @@ import { twMerge } from 'tailwind-merge'; import { ActiveAccount } from '@shared/accounts/active'; import { ChatsIcon, ComposeIcon, HomeIcon, NwcIcon, RelayIcon } from '@shared/icons'; -import { compactNumber } from '@utils/number'; +import { compactNumber } from '@utils/formater'; export function Navigation() { const newMessages = 0; diff --git a/src/shared/networkStatusIndicator.tsx b/src/shared/networkStatusIndicator.tsx deleted file mode 100644 index 0f46526a..00000000 --- a/src/shared/networkStatusIndicator.tsx +++ /dev/null @@ -1,16 +0,0 @@ -import { twMerge } from 'tailwind-merge'; - -import { useNetworkStatus } from '@utils/hooks/useNetworkStatus'; - -export function NetworkStatusIndicator() { - const isOnline = useNetworkStatus(); - - return ( - - ); -} diff --git a/src/shared/nip05.tsx b/src/shared/nip05.tsx index 10c7c22c..4e5ea6dc 100644 --- a/src/shared/nip05.tsx +++ b/src/shared/nip05.tsx @@ -56,7 +56,7 @@ export const NIP05 = memo(function NIP05({ }); if (status === 'pending') { -
; +
; } return ( diff --git a/src/shared/notes/actions/zap.tsx b/src/shared/notes/actions/zap.tsx index 04d3a5bb..fe5fb0bb 100644 --- a/src/shared/notes/actions/zap.tsx +++ b/src/shared/notes/actions/zap.tsx @@ -13,10 +13,9 @@ import { useArk } from '@libs/ark'; import { CancelIcon, ZapIcon } from '@shared/icons'; +import { compactNumber, displayNpub } from '@utils/formater'; import { useProfile } from '@utils/hooks/useProfile'; import { sendNativeNotification } from '@utils/notification'; -import { compactNumber } from '@utils/number'; -import { displayNpub } from '@utils/shortenKey'; export function NoteZap({ event }: { event: NDKEvent }) { const { ark } = useArk(); diff --git a/src/shared/notes/notify.tsx b/src/shared/notes/notify.tsx index 5fb9e403..53d4964a 100644 --- a/src/shared/notes/notify.tsx +++ b/src/shared/notes/notify.tsx @@ -8,7 +8,7 @@ import { ChildNote, TextKind } from '@shared/notes'; import { User } from '@shared/user'; import { WIDGET_KIND } from '@utils/constants'; -import { formatCreatedAt } from '@utils/createdAt'; +import { formatCreatedAt } from '@utils/formater'; import { useWidget } from '@utils/hooks/useWidget'; export function NotifyNote({ event }: { event: NDKEvent }) { diff --git a/src/shared/user.tsx b/src/shared/user.tsx index d348cf3c..8f6ea016 100644 --- a/src/shared/user.tsx +++ b/src/shared/user.tsx @@ -8,9 +8,8 @@ import { RepostIcon } from '@shared/icons'; import { NIP05 } from '@shared/nip05'; import { MoreActions } from '@shared/notes'; -import { formatCreatedAt } from '@utils/createdAt'; +import { displayNpub, formatCreatedAt } from '@utils/formater'; import { useProfile } from '@utils/hooks/useProfile'; -import { displayNpub } from '@utils/shortenKey'; export const User = memo(function User({ pubkey, diff --git a/src/shared/widgets/index.ts b/src/shared/widgets/index.ts index cbef4a5b..721da81a 100644 --- a/src/shared/widgets/index.ts +++ b/src/shared/widgets/index.ts @@ -15,3 +15,4 @@ export * from './other/toggleWidgetList'; export * from './other/widgetList'; export * from './other/addGroupFeeds'; export * from './other/addHashtagFeeds'; +export * from './other/userProfile'; diff --git a/src/shared/widgets/other/nostrBandUserProfile.tsx b/src/shared/widgets/other/nostrBandUserProfile.tsx index 37b30f5c..3c631fcc 100644 --- a/src/shared/widgets/other/nostrBandUserProfile.tsx +++ b/src/shared/widgets/other/nostrBandUserProfile.tsx @@ -6,7 +6,7 @@ import { useArk } from '@libs/ark'; import { FollowIcon } from '@shared/icons'; -import { shortenKey } from '@utils/shortenKey'; +import { displayNpub } from '@utils/formater'; export interface Profile { pubkey: string; @@ -66,7 +66,7 @@ export function NostrBandUserProfile({ data }: { data: Profile }) { {profile.display_name || profile.name}

- {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 {