From ac5e78a6b8250f039c340746b999743e8d8f3cbc Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Tue, 11 Apr 2023 14:46:00 +0700 Subject: [PATCH] added join channel function --- src-tauri/src/main.rs | 21 ++++++++++ src/components/channels/browseChannelItem.tsx | 39 +++++++++++++++---- src/components/channels/channelList.tsx | 18 +++++++++ src/components/channels/channelListItem.tsx | 36 +++++++++++++++++ src/utils/bindings.ts | 5 +++ 5 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 src/components/channels/channelListItem.tsx diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index d9847d66..b957e8ea 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -111,6 +111,12 @@ struct GetActiveChannelData { active: bool, } +#[derive(Deserialize, Type)] +struct UpdateChannelData { + event_id: String, + active: bool, +} + #[tauri::command] #[specta::specta] async fn get_accounts(db: DbState<'_>) -> Result, ()> { @@ -264,6 +270,19 @@ async fn create_channel(db: DbState<'_>, data: CreateChannelData) -> Result, data: UpdateChannelData) -> Result { + db.channel() + .update( + channel::event_id::equals(data.event_id), // Unique filter + vec![channel::active::set(data.active)], // Vec of updates + ) + .exec() + .await + .map_err(|_| ()) +} + #[tauri::command] #[specta::specta] async fn get_channels(db: DbState<'_>, data: GetChannelData) -> Result, ()> { @@ -335,6 +354,7 @@ async fn main() { get_latest_notes, get_note_by_id, create_channel, + update_channel, get_channels, get_active_channels, create_chat, @@ -382,6 +402,7 @@ async fn main() { get_note_by_id, count_total_notes, create_channel, + update_channel, get_channels, get_active_channels, create_chat, diff --git a/src/components/channels/browseChannelItem.tsx b/src/components/channels/browseChannelItem.tsx index 7187257c..e12e39a8 100644 --- a/src/components/channels/browseChannelItem.tsx +++ b/src/components/channels/browseChannelItem.tsx @@ -3,20 +3,37 @@ import { ImageWithFallback } from '@components/imageWithFallback'; import { DEFAULT_AVATAR } from '@stores/constants'; import { useRouter } from 'next/router'; +import { useCallback } from 'react'; export const BrowseChannelItem = ({ data }: { data: any }) => { const router = useRouter(); const channel = JSON.parse(data.content); - const openChannel = (id) => { - router.push({ - pathname: '/channels/[id]', - query: { id: id }, - }); - }; + const openChannel = useCallback( + (id: string) => { + router.push({ + pathname: '/channels/[id]', + query: { id: id }, + }); + }, + [router] + ); + + const joinChannel = useCallback( + async (id: string) => { + const { updateChannel } = await import('@utils/bindings'); + updateChannel({ event_id: id, active: true }) + .then(() => openChannel(id)) + .catch(console.error); + }, + [openChannel] + ); return ( -
openChannel(data.eventId)} className="flex items-center gap-2 px-3 py-2 hover:bg-zinc-950"> +
openChannel(data.eventId)} + className="group relative flex items-center gap-2 border-b border-zinc-800 px-3 py-2.5 hover:bg-black/20" + >
{ {channel.name} {channel.about}
+
+ +
); }; diff --git a/src/components/channels/channelList.tsx b/src/components/channels/channelList.tsx index b6af8320..626c729e 100644 --- a/src/components/channels/channelList.tsx +++ b/src/components/channels/channelList.tsx @@ -1,9 +1,24 @@ +import { ChannelListItem } from '@components/channels/channelListItem'; import { CreateChannelModal } from '@components/channels/createChannelModal'; import { GlobeIcon } from '@radix-ui/react-icons'; import Link from 'next/link'; +import { useEffect, useState } from 'react'; export default function ChannelList() { + const [list, setList] = useState([]); + + useEffect(() => { + const fetchChannels = async () => { + const { getActiveChannels } = await import('@utils/bindings'); + return await getActiveChannels({ active: true }); + }; + + fetchChannels() + .then((res) => setList(res)) + .catch(console.error); + }, []); + return (
Browse channels
+ {list.map((item) => ( + + ))}
); diff --git a/src/components/channels/channelListItem.tsx b/src/components/channels/channelListItem.tsx new file mode 100644 index 00000000..230728a9 --- /dev/null +++ b/src/components/channels/channelListItem.tsx @@ -0,0 +1,36 @@ +import { ImageWithFallback } from '@components/imageWithFallback'; + +import { DEFAULT_AVATAR } from '@stores/constants'; + +import { useRouter } from 'next/router'; + +export const ChannelListItem = ({ data }: { data: any }) => { + const router = useRouter(); + const channel = JSON.parse(data.content); + + const openChannel = (id: string) => { + router.push({ + pathname: '/channels/[id]', + query: { id: id }, + }); + }; + + return ( +
openChannel(data.eventId)} + className="inline-flex items-center gap-2 rounded-md px-2.5 py-1.5 hover:bg-zinc-900" + > +
+ +
+
+
{channel.name}
+
+
+ ); +}; diff --git a/src/utils/bindings.ts b/src/utils/bindings.ts index c6f0d671..7a9794fb 100644 --- a/src/utils/bindings.ts +++ b/src/utils/bindings.ts @@ -48,6 +48,10 @@ export function createChannel(data: CreateChannelData) { return invoke('create_channel', { data }); } +export function updateChannel(data: UpdateChannelData) { + return invoke('update_channel', { data }); +} + export function getChannels(data: GetChannelData) { return invoke('get_channels', { data }); } @@ -96,6 +100,7 @@ export type GetChatData = { account_id: number }; export type GetChannelData = { limit: number; offset: number }; export type CreateChannelData = { event_id: string; content: string; account_id: number }; export type GetPlebPubkeyData = { pubkey: string }; +export type UpdateChannelData = { event_id: string; active: boolean }; export type GetActiveChannelData = { active: boolean }; export type GetPlebData = { account_id: number }; export type CreateAccountData = { pubkey: string; privkey: string; metadata: string };