Skip to content

Commit ce414dc

Browse files
committed
feat(contexts): enhance EmbedInstanceContext to require apiUrl and format it before storage
1 parent e510b5f commit ce414dc

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/contexts/EmbedInstanceContext.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,21 @@ export function EmbedInstanceProvider({ children }: { children: React.ReactNode
2828
const validateAndFetchInstance = async () => {
2929
const token = searchParams.get("token");
3030
const instanceName = searchParams.get("instanceName");
31+
const apiUrl = searchParams.get("apiUrl");
3132

32-
if (!token || !instanceName) {
33-
setError("Token e instanceName são obrigatórios");
33+
if (!token || !instanceName || !apiUrl) {
34+
setError("Token, instanceName e apiUrl são obrigatórios");
3435
setIsLoading(false);
3536
return;
3637
}
3738

3839
try {
39-
const apiUrl = "https://integracaov2.icommarketing.com.br";
40-
localStorage.setItem(TOKEN_ID.API_URL, apiUrl);
40+
// Format URL (remove trailing slash if present)
41+
const formattedUrl = apiUrl.endsWith("/") ? apiUrl.slice(0, -1) : apiUrl;
42+
localStorage.setItem(TOKEN_ID.API_URL, formattedUrl);
4143
localStorage.setItem(TOKEN_ID.INSTANCE_TOKEN, token);
4244

43-
const { data } = await axios.get(`${apiUrl}/instance/fetchInstances?instanceName=${instanceName}`, {
45+
const { data } = await axios.get(`${formattedUrl}/instance/fetchInstances?instanceName=${instanceName}`, {
4446
headers: {
4547
apikey: token,
4648
},

src/pages/instance/Chat/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
1111
import { useInstance } from "@/contexts/InstanceContext";
1212

1313
import { useFindChats } from "@/lib/queries/chat/findChats";
14+
import { getToken, TOKEN_ID } from "@/lib/queries/token";
1415

1516
import { Chat as ChatType } from "@/types/evolution.types";
1617

@@ -76,7 +77,12 @@ function Chat() {
7677
useEffect(() => {
7778
if (!instance?.name) return;
7879

79-
const serverUrl = "https://integracaov2.icommarketing.com.br";
80+
const serverUrl = getToken(TOKEN_ID.API_URL);
81+
if (!serverUrl) {
82+
console.error("API URL not found in localStorage");
83+
return;
84+
}
85+
8086
const socket = connectSocket(serverUrl);
8187

8288
// Function to update chats from websocket events

0 commit comments

Comments
 (0)