Skip to content

Commit a511e18

Browse files
committed
stuff
1 parent 69dffd5 commit a511e18

4 files changed

Lines changed: 53 additions & 12 deletions

File tree

components/home/AddChannelModal.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
useUserPerms,
1616
useUserServerPerms,
1717
} from '@/lib/store';
18-
import { ChannelPermissions } from '@/types/permissions';
1918

2019
export default function AddChannelModal({
2120
showModal,
@@ -38,8 +37,6 @@ export default function AddChannelModal({
3837

3938
const getUserServerPerms = useGetUserPermsForServer();
4039

41-
const userServerPerms = useUserServerPerms();
42-
4340
const {
4441
register,
4542
handleSubmit,
@@ -65,9 +62,6 @@ export default function AddChannelModal({
6562
}, [user, getUserServerPerms, supabase, serverId]);
6663

6764
const onSubmit = async (formData: CreateChannelInput) => {
68-
//
69-
// console.log(serverId);
70-
// console.log(userPerms & ChannelPermissions.MANAGE_MESSAGES);
7165
const { data, error } = await createChannel(
7266
supabase,
7367
serverId,

components/home/ServerList.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { ChannelPermissions, ServerPermissions } from '@/types/permissions';
1818

1919
export default function ServerList() {
2020
//TODO: Display default page (when user belongs to and has no servers)
21-
//TODO: Only show add channel if user has perms for it
2221

2322
const [showAddServer, setShowAddServer] = useState(false);
2423
const [showAddChannelModal, setShowAddChannelModal] = useState(false);
@@ -32,7 +31,6 @@ export default function ServerList() {
3231

3332
const getUserServerPerms = useGetUserPermsForServer();
3433
const userServerPerms = useUserServerPerms();
35-
console.log(userServerPerms & ServerPermissions.OWNER);
3634

3735
useEffect(() => {
3836
if (getServers) {

hooks/useRealtimeStore.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
4141
.channel('server_users')
4242
.on<ServerUser>(
4343
'postgres_changes',
44-
{ event: 'INSERT', schema: 'public', table: 'server_users', filter: `profile_id=eq.${user?.id}` },
44+
{
45+
event: 'INSERT',
46+
schema: 'public',
47+
table: 'server_users',
48+
filter: `profile_id=eq.${user?.id}`,
49+
},
4550
async (payload) => {
4651
console.log('insert');
4752

@@ -50,7 +55,12 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
5055
)
5156
.on<ServerUser>(
5257
'postgres_changes',
53-
{ event: 'DELETE', schema: 'public', table: 'server_users', filter: `profile_id=eq.${user?.id}` },
58+
{
59+
event: 'DELETE',
60+
schema: 'public',
61+
table: 'server_users',
62+
filter: `profile_id=eq.${user?.id}`,
63+
},
5464
async (payload) => {
5565
console.log('remove user from server');
5666

@@ -77,6 +87,21 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
7787
}
7888
)
7989
.subscribe();
90+
91+
supabase
92+
.channel('channels')
93+
.on<Server>(
94+
'postgres_changes',
95+
{ event: 'INSERT', schema: 'public', table: 'channels' },
96+
async (payload) => {
97+
console.log('update');
98+
99+
if (user) {
100+
getServers(supabase, user.id);
101+
}
102+
}
103+
)
104+
.subscribe();
80105
}
81106

82107
// add return right here!
@@ -93,7 +118,7 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
93118
event: 'INSERT',
94119
schema: 'public',
95120
table: 'messages',
96-
filter: `channel_id=eq.${channel?.channel_id}`,
121+
filter: `channel_id=eq.${channel.channel_id}`,
97122
},
98123
async (payload) => {
99124
console.log('New message event');

lib/store.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Database } from '@/types/database.supabase';
1010
import { SupabaseClient } from '@supabase/supabase-js';
1111
import {
1212
getCurrentUserServerPermissions,
13+
getServer,
1314
getServerForUser,
1415
getServersForUser,
1516
} from '@/services/server.service';
@@ -26,6 +27,7 @@ export interface ServerState {
2627
addServer: (supabase: SupabaseClient<Database>, serverUserId: number) => void;
2728
removeServer: (serverId: number) => void;
2829
getServers: (supabase: SupabaseClient<Database>, userId: string) => void;
30+
updateServer: (supabase: SupabaseClient<Database>, serverId: number) => void;
2931
}
3032

3133
const useServerStore = create<ServerState>()((set) => ({
@@ -65,6 +67,28 @@ const useServerStore = create<ServerState>()((set) => ({
6567
set({ servers: data as ServersForUser[] }, true);
6668
}
6769
},
70+
updateServer: async (supabase, serverId) => {
71+
const { data, error } = await getServer(supabase, serverId);
72+
73+
if (error) {
74+
console.error(error);
75+
return;
76+
}
77+
78+
if (data) {
79+
set((state) => ({
80+
servers: state.servers.map((server) => {
81+
// Once we hit a message that matches the id, we can return the updated message instead of the old one
82+
if (server.server_id === data[0].id) {
83+
return data as MessageWithServerProfile;
84+
}
85+
86+
// Otherwise fallback to the old one
87+
return message;
88+
}),
89+
}));
90+
}
91+
},
6892
}));
6993
export interface MessagesState {
7094
messages: MessageWithServerProfile[];
@@ -187,7 +211,7 @@ const useUserPermsStore = create<UserPermsState>()((set) => ({
187211
if (error) {
188212
console.log(error);
189213
}
190-
// console.log('data', data);
214+
191215
if (data) {
192216
set({ userServerPerms: data });
193217
}

0 commit comments

Comments
 (0)