mirror of
https://github.com/lumehq/lume.git
synced 2025-10-06 21:52:57 +02:00
move channel list logic to atom
This commit is contained in:
@@ -14,7 +14,7 @@ export default function AppActions() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`pl-[68px]} flex h-full items-center gap-2`}>
|
<div className={`flex h-full items-center gap-2 pl-[68px]`}>
|
||||||
<button
|
<button
|
||||||
onClick={() => goBack()}
|
onClick={() => goBack()}
|
||||||
className="group inline-flex h-6 w-6 items-center justify-center rounded-md hover:bg-zinc-900"
|
className="group inline-flex h-6 w-6 items-center justify-center rounded-md hover:bg-zinc-900"
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
import { ChannelListItem } from '@components/channels/channelListItem';
|
import { ChannelListItem } from '@components/channels/channelListItem';
|
||||||
import { CreateChannelModal } from '@components/channels/createChannelModal';
|
import { CreateChannelModal } from '@components/channels/createChannelModal';
|
||||||
|
|
||||||
import { DEFAULT_CHANNELS } from '@stores/constants';
|
import { channelsAtom } from '@stores/channel';
|
||||||
|
|
||||||
|
import { useAtomValue } from 'jotai';
|
||||||
|
|
||||||
export default function ChannelList() {
|
export default function ChannelList() {
|
||||||
|
const list = useAtomValue(channelsAtom);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col gap-px">
|
<div className="flex flex-col gap-px">
|
||||||
{DEFAULT_CHANNELS.map((item) => (
|
{list.map((item: { event_id: string }) => (
|
||||||
<ChannelListItem key={item.event_id} data={item} />
|
<ChannelListItem key={item.event_id} data={item} />
|
||||||
))}
|
))}
|
||||||
<CreateChannelModal />
|
<CreateChannelModal />
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { RelayContext } from '@components/relaysProvider';
|
import { RelayContext } from '@components/relaysProvider';
|
||||||
|
|
||||||
|
import { defaultChannelsAtom } from '@stores/channel';
|
||||||
import { FULL_RELAYS } from '@stores/constants';
|
import { FULL_RELAYS } from '@stores/constants';
|
||||||
|
|
||||||
import { dateToUnix } from '@utils/getDate';
|
import { dateToUnix } from '@utils/getDate';
|
||||||
@@ -8,6 +9,7 @@ import { createChannel } from '@utils/storage';
|
|||||||
import * as Dialog from '@radix-ui/react-dialog';
|
import * as Dialog from '@radix-ui/react-dialog';
|
||||||
import useLocalStorage from '@rehooks/local-storage';
|
import useLocalStorage from '@rehooks/local-storage';
|
||||||
import { Cancel, Plus } from 'iconoir-react';
|
import { Cancel, Plus } from 'iconoir-react';
|
||||||
|
import { useSetAtom } from 'jotai';
|
||||||
import { getEventHash, signEvent } from 'nostr-tools';
|
import { getEventHash, signEvent } from 'nostr-tools';
|
||||||
import { useContext, useState } from 'react';
|
import { useContext, useState } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
@@ -16,6 +18,7 @@ export const CreateChannelModal = () => {
|
|||||||
const [pool]: any = useContext(RelayContext);
|
const [pool]: any = useContext(RelayContext);
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const [activeAccount]: any = useLocalStorage('account', {});
|
const [activeAccount]: any = useLocalStorage('account', {});
|
||||||
|
const setChannel = useSetAtom(defaultChannelsAtom);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
@@ -39,6 +42,15 @@ export const CreateChannelModal = () => {
|
|||||||
pool.publish(event, FULL_RELAYS);
|
pool.publish(event, FULL_RELAYS);
|
||||||
// insert to database
|
// insert to database
|
||||||
createChannel(event.id, event.content, event.created_at);
|
createChannel(event.id, event.content, event.created_at);
|
||||||
|
// update jotai state
|
||||||
|
setChannel((prev: any) => [
|
||||||
|
...prev,
|
||||||
|
{
|
||||||
|
event_id: event.id,
|
||||||
|
metadata: { name: data.name, picture: data.picture, about: data.about },
|
||||||
|
created_at: event.created_at,
|
||||||
|
},
|
||||||
|
]);
|
||||||
// close modal
|
// close modal
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
// reset form
|
// reset form
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ import ChannelList from '@components/channels/channelList';
|
|||||||
|
|
||||||
import * as Collapsible from '@radix-ui/react-collapsible';
|
import * as Collapsible from '@radix-ui/react-collapsible';
|
||||||
import { NavArrowUp } from 'iconoir-react';
|
import { NavArrowUp } from 'iconoir-react';
|
||||||
import { useState } from 'react';
|
import { Suspense, useState } from 'react';
|
||||||
|
import Skeleton from 'react-loading-skeleton';
|
||||||
|
|
||||||
export default function Channels() {
|
export default function Channels() {
|
||||||
const [open, setOpen] = useState(true);
|
const [open, setOpen] = useState(true);
|
||||||
@@ -21,7 +22,9 @@ export default function Channels() {
|
|||||||
<h3 className="text-[11px] font-bold uppercase tracking-widest text-zinc-600">Channels</h3>
|
<h3 className="text-[11px] font-bold uppercase tracking-widest text-zinc-600">Channels</h3>
|
||||||
</Collapsible.Trigger>
|
</Collapsible.Trigger>
|
||||||
<Collapsible.Content>
|
<Collapsible.Content>
|
||||||
<ChannelList />
|
<Suspense fallback={<Skeleton />}>
|
||||||
|
<ChannelList />
|
||||||
|
</Suspense>
|
||||||
</Collapsible.Content>
|
</Collapsible.Content>
|
||||||
</div>
|
</div>
|
||||||
</Collapsible.Root>
|
</Collapsible.Root>
|
||||||
|
|||||||
@@ -70,18 +70,10 @@ export function Page() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let ignore = false;
|
// reset channel reply
|
||||||
|
resetChannelReply();
|
||||||
if (!ignore) {
|
// reset channel messages
|
||||||
// reset channel reply
|
resetChannelMessages();
|
||||||
resetChannelReply();
|
|
||||||
// reset channel messages
|
|
||||||
resetChannelMessages();
|
|
||||||
}
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
ignore = true;
|
|
||||||
};
|
|
||||||
}, [resetChannelReply, resetChannelMessages]);
|
}, [resetChannelReply, resetChannelMessages]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,8 +1,19 @@
|
|||||||
|
import { DEFAULT_CHANNELS } from '@stores/constants';
|
||||||
|
|
||||||
import { atom } from 'jotai';
|
import { atom } from 'jotai';
|
||||||
import { atomWithReset } from 'jotai/utils';
|
import { atomWithReset } from 'jotai/utils';
|
||||||
|
|
||||||
|
// channel list
|
||||||
|
export const defaultChannelsAtom = atom(DEFAULT_CHANNELS);
|
||||||
|
export const channelsAtom = atom(async (get) => {
|
||||||
|
const { getChannels } = await import('@utils/storage');
|
||||||
|
const result: any = await getChannels(100, 0);
|
||||||
|
return get(defaultChannelsAtom).concat(result);
|
||||||
|
});
|
||||||
|
|
||||||
// channel reply id
|
// channel reply id
|
||||||
export const channelReplyAtom = atomWithReset({ id: null, pubkey: null, content: null });
|
export const channelReplyAtom = atomWithReset({ id: null, pubkey: null, content: null });
|
||||||
|
|
||||||
// channel messages
|
// channel messages
|
||||||
export const channelMessagesAtom = atomWithReset([]);
|
export const channelMessagesAtom = atomWithReset([]);
|
||||||
export const sortedChannelMessagesAtom = atom((get) => {
|
export const sortedChannelMessagesAtom = atom((get) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user