refactor: update visitor references to guest in WebSocket service and related files
This commit is contained in:
+10
-10
@@ -114,7 +114,7 @@ export type ImWidgetConfig = {
|
||||
width?: string
|
||||
}
|
||||
|
||||
const VISITOR_STORAGE_KEY = "cs_agent_im_visitor_id"
|
||||
const GUEST_STORAGE_KEY = "cs_agent_im_guest_id"
|
||||
const API_BASE_URL =
|
||||
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() || ""
|
||||
const OPEN_IM_CHANNEL_ID =
|
||||
@@ -122,21 +122,21 @@ const OPEN_IM_CHANNEL_ID =
|
||||
const OPEN_IM_EXTERNAL_SOURCE =
|
||||
process.env.NEXT_PUBLIC_OPEN_IM_EXTERNAL_SOURCE?.trim() || "web_chat"
|
||||
|
||||
function buildVisitorId() {
|
||||
return `visitor_${generateUUID()}`
|
||||
function buildGuestId() {
|
||||
return `guest_${generateUUID()}`
|
||||
}
|
||||
|
||||
export function getImVisitorId() {
|
||||
export function getGuestId() {
|
||||
if (typeof window === "undefined") {
|
||||
return "visitor_ssr"
|
||||
return ""
|
||||
}
|
||||
const existing = window.localStorage.getItem(VISITOR_STORAGE_KEY)?.trim()
|
||||
const existing = window.localStorage.getItem(GUEST_STORAGE_KEY)?.trim()
|
||||
if (existing) {
|
||||
return existing
|
||||
}
|
||||
const visitorId = buildVisitorId()
|
||||
window.localStorage.setItem(VISITOR_STORAGE_KEY, visitorId)
|
||||
return visitorId
|
||||
const guestId = buildGuestId()
|
||||
window.localStorage.setItem(GUEST_STORAGE_KEY, guestId)
|
||||
return guestId
|
||||
}
|
||||
|
||||
function getRuntimeImConfig() {
|
||||
@@ -158,7 +158,7 @@ function createImHeaders() {
|
||||
const config = getRuntimeImConfig()
|
||||
const headers: Record<string, string> = {
|
||||
"X-External-Source": config.externalSource,
|
||||
"X-External-Id": config.externalId || getImVisitorId(),
|
||||
"X-External-Id": config.externalId || getGuestId(),
|
||||
"X-Channel-Id": config.channelId,
|
||||
}
|
||||
if (config.externalName) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { createWebSocketBaseUrl } from "@/lib/api/websocket"
|
||||
import { getImVisitorId, type ImMessage } from "@/lib/api/im"
|
||||
import { getGuestId, type ImMessage } from "@/lib/api/im"
|
||||
import { readKefuWidgetConfig } from "@/lib/kefu-widget-config"
|
||||
import type {
|
||||
RealtimeConversationPatch,
|
||||
@@ -20,7 +20,7 @@ export function createImRealtimeConnection() {
|
||||
? apiBaseUrl.replace(/^http/, "ws").replace(/\/$/, "")
|
||||
: createWebSocketBaseUrl()
|
||||
const resolvedExternalId = encodeURIComponent(
|
||||
(config.externalId ?? "").trim() || getImVisitorId()
|
||||
(config.externalId ?? "").trim() || getGuestId()
|
||||
)
|
||||
const externalSource = encodeURIComponent(
|
||||
(config.externalSource ?? "web_chat").trim() || "web_chat"
|
||||
|
||||
Reference in New Issue
Block a user