Initial import
This commit is contained in:
102
src/app/main/clientIdentity.ts
Executable file
102
src/app/main/clientIdentity.ts
Executable file
@@ -0,0 +1,102 @@
|
||||
import type { AppPageDescriptor } from '../../store/appStore/types';
|
||||
|
||||
export const CLIENT_ID_STORAGE_KEY = 'work-app.visitor.client-id';
|
||||
|
||||
function generateFallbackClientId() {
|
||||
return `client-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
||||
}
|
||||
|
||||
function generateClientId() {
|
||||
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
|
||||
return generateFallbackClientId();
|
||||
}
|
||||
|
||||
export function getClientId() {
|
||||
if (typeof window === 'undefined') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return window.localStorage.getItem(CLIENT_ID_STORAGE_KEY)?.trim() ?? '';
|
||||
}
|
||||
|
||||
export function clearClientId() {
|
||||
if (typeof window === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
window.localStorage.removeItem(CLIENT_ID_STORAGE_KEY);
|
||||
}
|
||||
|
||||
export function getOrCreateClientId() {
|
||||
const existingClientId = getClientId();
|
||||
|
||||
if (existingClientId) {
|
||||
return existingClientId;
|
||||
}
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
return '';
|
||||
}
|
||||
|
||||
const nextClientId = generateClientId();
|
||||
window.localStorage.setItem(CLIENT_ID_STORAGE_KEY, nextClientId);
|
||||
return nextClientId;
|
||||
}
|
||||
|
||||
export function appendClientIdHeader(headersInit?: HeadersInit) {
|
||||
const headers = new Headers(headersInit);
|
||||
const clientId = getOrCreateClientId();
|
||||
|
||||
if (clientId && !headers.has('X-Client-Id')) {
|
||||
headers.set('X-Client-Id', clientId);
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
export function buildTrackedPageUrl(page: AppPageDescriptor) {
|
||||
if (typeof window === 'undefined') {
|
||||
return '';
|
||||
}
|
||||
|
||||
const url = new URL(window.location.href);
|
||||
|
||||
if (page.topMenu === 'plans') {
|
||||
url.searchParams.set('topMenu', 'plans');
|
||||
url.searchParams.set('planFilter', page.section);
|
||||
url.searchParams.delete('planSection');
|
||||
} else {
|
||||
url.searchParams.set('topMenu', page.topMenu);
|
||||
url.searchParams.delete('planSection');
|
||||
url.searchParams.delete('planFilter');
|
||||
}
|
||||
|
||||
if (page.topMenu === 'docs') {
|
||||
url.searchParams.set('docsSection', page.section);
|
||||
} else {
|
||||
url.searchParams.delete('docsSection');
|
||||
}
|
||||
|
||||
if (page.topMenu === 'apis') {
|
||||
url.searchParams.set('apiSection', page.section);
|
||||
} else {
|
||||
url.searchParams.delete('apiSection');
|
||||
}
|
||||
|
||||
if (page.topMenu === 'chat') {
|
||||
url.searchParams.set('chatSection', page.section);
|
||||
} else {
|
||||
url.searchParams.delete('chatSection');
|
||||
}
|
||||
|
||||
if (page.topMenu === 'play') {
|
||||
url.searchParams.set('playSection', page.section);
|
||||
} else {
|
||||
url.searchParams.delete('playSection');
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
Reference in New Issue
Block a user