better structure
This commit is contained in:
22
src/utils/date.tsx
Normal file
22
src/utils/date.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
// get X days ago with user provided date
|
||||
export function getDayAgo(numOfDays, date = new Date()) {
|
||||
const days = new Date(date.getTime());
|
||||
days.setDate(date.getDate() - numOfDays);
|
||||
|
||||
return days;
|
||||
}
|
||||
|
||||
// get X hours ago with user provided date
|
||||
export function getHourAgo(numOfHours, date = new Date()) {
|
||||
const hours = new Date(date.getTime());
|
||||
hours.setHours(date.getHours() - numOfHours);
|
||||
|
||||
return hours;
|
||||
}
|
||||
|
||||
// convert date to unix timestamp
|
||||
export function dateToUnix(_date?: Date) {
|
||||
const date = _date || new Date();
|
||||
|
||||
return Math.floor(date.getTime() / 1000);
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
// get X days ago with user provided date
|
||||
export function daysAgo(numOfDays, date = new Date()) {
|
||||
const daysAgo = new Date(date.getTime());
|
||||
daysAgo.setDate(date.getDate() - numOfDays);
|
||||
|
||||
return daysAgo;
|
||||
}
|
||||
|
||||
// get X hours ago with user provided date
|
||||
export function hoursAgo(numOfHours, date = new Date()) {
|
||||
const hoursAgo = new Date(date.getTime());
|
||||
hoursAgo.setHours(date.getHours() - numOfHours);
|
||||
|
||||
return hoursAgo;
|
||||
}
|
||||
|
||||
// convert date to unix timestamp
|
||||
export function dateToUnix(_date?: Date) {
|
||||
const date = _date || new Date();
|
||||
|
||||
return Math.floor(date.getTime() / 1000);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getActiveAccount } from '@lume/utils/storage';
|
||||
import { getActiveAccount } from '@utils/storage';
|
||||
|
||||
import useSWR from 'swr';
|
||||
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
import { RelayContext } from '@lume/shared/relayProvider';
|
||||
import { READONLY_RELAYS } from '@lume/stores/constants';
|
||||
import { getChannel } from '@lume/utils/storage';
|
||||
|
||||
import { useContext } from 'react';
|
||||
import useSWR from 'swr';
|
||||
import useSWRSubscription from 'swr/subscription';
|
||||
|
||||
const fetcher = async ([, id]) => {
|
||||
const result = await getChannel(id);
|
||||
if (result) {
|
||||
return JSON.parse(result.metadata);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export function useChannelProfile(id: string, channelPubkey: string) {
|
||||
const pool: any = useContext(RelayContext);
|
||||
|
||||
const { data: cache, isLoading } = useSWR(['channel-cache-profile', id], fetcher);
|
||||
const { data, error } = useSWRSubscription(
|
||||
!isLoading && cache ? ['channel-profile', id] : null,
|
||||
([, key], { next }) => {
|
||||
// subscribe to channel
|
||||
const unsubscribe = pool.subscribe(
|
||||
[
|
||||
{
|
||||
'#e': [key],
|
||||
authors: [channelPubkey],
|
||||
kinds: [41],
|
||||
},
|
||||
],
|
||||
READONLY_RELAYS,
|
||||
(event: { content: string }) => {
|
||||
next(null, JSON.parse(event.content));
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
{
|
||||
unsubscribeOnEose: true,
|
||||
}
|
||||
);
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
if (!data || error) {
|
||||
return cache;
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import { nip04 } from 'nostr-tools';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
|
||||
export function useDecryptMessage(userKey: string, userPriv: string, data: any) {
|
||||
const [content, setContent] = useState(null);
|
||||
|
||||
const extractSenderKey = useCallback(() => {
|
||||
const keyInTags = data.tags.find(([k, v]) => k === 'p' && v && v !== '')[1];
|
||||
if (keyInTags === userKey) {
|
||||
return data.pubkey;
|
||||
} else {
|
||||
return keyInTags;
|
||||
}
|
||||
}, [data.pubkey, data.tags, userKey]);
|
||||
|
||||
const decrypt = useCallback(async () => {
|
||||
const senderKey = extractSenderKey();
|
||||
const result = await nip04.decrypt(userPriv, senderKey, data.content);
|
||||
// update state with decrypt content
|
||||
setContent(result);
|
||||
}, [extractSenderKey, userPriv, data.content]);
|
||||
|
||||
useEffect(() => {
|
||||
decrypt().catch(console.error);
|
||||
}, [decrypt]);
|
||||
|
||||
return content ? content : null;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { METADATA_SERVICE } from '@lume/stores/constants';
|
||||
import { createPleb, getPleb } from '@lume/utils/storage';
|
||||
import { METADATA_SERVICE } from '@stores/constants';
|
||||
|
||||
import { createPleb, getPleb } from '@utils/storage';
|
||||
|
||||
import useSWR from 'swr';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user