clean up
This commit is contained in:
12
src/app.tsx
12
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 }) => {
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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() {
|
||||
/>
|
||||
</Avatar.Fallback>
|
||||
</Avatar.Root>
|
||||
<NetworkStatusIndicator />
|
||||
<span
|
||||
className={twMerge(
|
||||
'absolute bottom-0 right-0 block h-2 w-2 rounded-full ring-2 ring-neutral-100 dark:ring-neutral-900',
|
||||
isOnline ? 'bg-teal-500' : 'bg-red-500'
|
||||
)}
|
||||
/>
|
||||
</Link>
|
||||
<AccountMoreActions />
|
||||
</div>
|
||||
|
||||
@@ -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<SetStateAction<string>>;
|
||||
}) {
|
||||
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 (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => uploadBanner()}
|
||||
className="inline-flex h-full w-full flex-col items-center justify-center"
|
||||
>
|
||||
{loading ? (
|
||||
<LoaderIcon className="h-6 w-6 animate-spin text-neutral-900 dark:text-neutral-100" />
|
||||
) : (
|
||||
<PlusIcon className="h-6 w-6 text-neutral-900 dark:text-neutral-100" />
|
||||
)}
|
||||
<p className="text-sm font-semibold text-neutral-800 dark:text-neutral-200">
|
||||
Add cover
|
||||
</p>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { twMerge } from 'tailwind-merge';
|
||||
|
||||
import { useNetworkStatus } from '@utils/hooks/useNetworkStatus';
|
||||
|
||||
export function NetworkStatusIndicator() {
|
||||
const isOnline = useNetworkStatus();
|
||||
|
||||
return (
|
||||
<span
|
||||
className={twMerge(
|
||||
'absolute bottom-0 right-0 block h-2 w-2 rounded-full ring-2 ring-neutral-100 dark:ring-neutral-900',
|
||||
isOnline ? 'bg-teal-500' : 'bg-red-500'
|
||||
)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -56,7 +56,7 @@ export const NIP05 = memo(function NIP05({
|
||||
});
|
||||
|
||||
if (status === 'pending') {
|
||||
<div className="h-3 w-20 animate-pulse rounded bg-white/10" />;
|
||||
<div className="h-4 w-4 animate-pulse rounded-full bg-neutral-100 dark:bg-neutral-900" />;
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 }) {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -15,3 +15,4 @@ export * from './other/toggleWidgetList';
|
||||
export * from './other/widgetList';
|
||||
export * from './other/addGroupFeeds';
|
||||
export * from './other/addHashtagFeeds';
|
||||
export * from './other/userProfile';
|
||||
|
||||
@@ -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}
|
||||
</h3>
|
||||
<p className="max-w-[10rem] truncate text-sm text-neutral-900 dark:text-neutral-100/50">
|
||||
{profile.nip05 || shortenKey(data.pubkey)}
|
||||
{profile.nip05 || displayNpub(data.pubkey, 16)}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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();
|
||||
@@ -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';
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
export const nHoursAgo = (hrs: number): number =>
|
||||
Math.floor((Date.now() - hrs * 60 * 60 * 1000) / 1000);
|
||||
@@ -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' });
|
||||
@@ -1,2 +0,0 @@
|
||||
// convert number to K, M, B, T, etc.
|
||||
export const compactNumber = Intl.NumberFormat('en', { notation: 'compact' });
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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
16
src/utils/types.d.ts
vendored
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user