Skip to content

Commit

Permalink
fix api
Browse files Browse the repository at this point in the history
  • Loading branch information
tnfAngel committed Apr 16, 2024
1 parent a7b617b commit 3184dc6
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 44 deletions.
3 changes: 1 addition & 2 deletions .env.development
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
API_VERSION=1
API_URL=http://localhost:3002/api
GATEWAY_URL=http://localhost:3002
GATEWAY_URL=http://localhost:4000
AUTH_URL=http://localhost:3003
CDN_URL=http://localhost:3004
MEDIA_PROXY_URL=http://localhost:3005
4 changes: 2 additions & 2 deletions src/app/channels/[channelId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { useDispatch } from 'react-redux';

export default function SelectedChannelPage({
params
}: {
}: Readonly<{
params: { channelId: string };
}) {
}>) {
const { channelId } = params;

const dispatch = useDispatch();
Expand Down
6 changes: 3 additions & 3 deletions src/client/links.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class ClientLinks {
api = process.env['API_URL'] as string;
cdn = process.env['CDN_URL'] as string;
api = process.env['API_URL'];
cdn = process.env['CDN_URL'];
gateway = process.env['GATEWAY_URL'] ?? ('https://chat-backend.tnfangel.com' as string);
proxy = process.env['MEDIA_PROXY_URL'] as string;
proxy = process.env['MEDIA_PROXY_URL'];
}
57 changes: 29 additions & 28 deletions src/components/channel/InputArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,36 +131,37 @@ export default function InputArea({ channel }: InputBoxProps) {

client.sentMessagesIds.push(tempMessageId);

await ky
.post(`${client.links.api}/channels/${channel?.id}/messages`, {
json: { content: content, nonce: tempMessageId }
})
.json<{ id: string }>()
.then((result) => {
dispatch(
modifyMessage({
channelId: rawMessage.channelId,
messageId: rawMessage.id,
newMessage: normalizeMessage({
...rawMessage,
mode: MessageModes.Sent,
id: result.id
if (client.links.api)
await ky
.post(`${client.links.api}/channels/${channel?.id}/messages`, {
json: { content: content, nonce: tempMessageId }
})
.json<{ id: string }>()
.then((result) => {
dispatch(
modifyMessage({
channelId: rawMessage.channelId,
messageId: rawMessage.id,
newMessage: normalizeMessage({
...rawMessage,
mode: MessageModes.Sent,
id: result.id
})
})
})
);
})
.catch(() => {
dispatch(
modifyMessage({
channelId: rawMessage.channelId,
messageId: rawMessage.id,
newMessage: normalizeMessage({
...rawMessage,
mode: MessageModes.Blocked
);
})
.catch(() => {
dispatch(
modifyMessage({
channelId: rawMessage.channelId,
messageId: rawMessage.id,
newMessage: normalizeMessage({
...rawMessage,
mode: MessageModes.Blocked
})
})
})
);
});
);
});
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/components/screens/GuildScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';
import useThemeColors from '@/hooks/useThemeColors';
import type { RootState } from '@/store';
import { Center, Text, Flex, Heading, Stack } from '@chakra-ui/react';
import { Center, Flex, Heading, Stack, Text } from '@chakra-ui/react';
import { useRouter } from 'next/navigation';
import { useRef } from 'react';
import GuildSidebar from '../layout/GuildSidebar';
Expand Down Expand Up @@ -49,7 +49,9 @@ export default function GuildScreen() {
>
<Center w='100%' h='100%'>
<Stack>
<Heading as='h1' size="lg">Welcome to {guild.name}</Heading>
<Heading as='h1' size='lg'>
Welcome to {guild.name}
</Heading>
<Text>Select a channel to get started.</Text>
</Stack>
</Center>
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type { IRawChannel } from '@/types/interfaces/Channel';
import useSWRImmutable from 'swr/immutable';

export default function useChannel(channelId: string) {
const { data, isLoading, error } = useSWRImmutable<IRawChannel>(`${client.links.api}/channels/${channelId}`);
const { data, isLoading, error } = useSWRImmutable<IRawChannel>(
client.links.api ? `${client.links.api}/channels/${channelId}` : undefined
);

return { data, isLoading, error };
}
2 changes: 1 addition & 1 deletion src/hooks/useMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import useSWRImmutable from 'swr/immutable';

export default function useChannelMessage(channelId: string, messageId: string) {
const { data, isLoading, error } = useSWRImmutable<IRawMessage[]>(
`${client.links.api}/channels/${channelId}/messages/${messageId}`
client.links.api ? `${client.links.api}/channels/${channelId}/messages/${messageId}` : undefined
);

return { data, isLoading, error };
Expand Down
12 changes: 8 additions & 4 deletions src/hooks/useMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import type { IRawMessage } from '@/types/interfaces/Message';
import useSWRImmutable from 'swr/immutable';

export default function useChannelMessages(channelId: string, before?: string | null, after?: string | null) {
const url = new URL(`${client.links.api}/channels/${channelId}/messages`);
let url;

if (before) url.searchParams.set('before', before);
if (after) url.searchParams.set('after', after);
if (client.links.api) {
url = new URL(`${client.links.api}/channels/${channelId}/messages`);

const { data, isLoading, error } = useSWRImmutable<IRawMessage[]>(url.toString());
if (before) url.searchParams.set('before', before);
if (after) url.searchParams.set('after', after);
}

const { data, isLoading, error } = useSWRImmutable<IRawMessage[]>(url?.toString());

return { data, isLoading, error };
}
4 changes: 3 additions & 1 deletion src/hooks/useUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type { IRawUser } from '@/types/interfaces/User';
import useSWRImmutable from 'swr/immutable';

export default function useUser(userId: string) {
const { data, isLoading, error } = useSWRImmutable<IRawUser>(`${client.links.api}/api/users/${userId}`);
const { data, isLoading, error } = useSWRImmutable<IRawUser>(
client.links.api ? `${client.links.api}/api/users/${userId}` : undefined
);

return { data, isLoading, error };
}

0 comments on commit 3184dc6

Please sign in to comment.