Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion client/src/lib/components/Toggle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
label: string;
checked?: boolean;
}
import { uuid } from '$lib/uuid';

let { label, checked = $bindable(), class: extraClass = '', ...rest }: Props = $props();

const id = `toggle-${crypto.randomUUID()}`;
const id = `toggle-${uuid()}`;
</script>

<div class="flex items-center gap-2 {extraClass}">
Expand Down
4 changes: 3 additions & 1 deletion client/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { uuid } from './uuid';

declare const umami: {
track: (event_name: string, data?: Record<string, any>) => void;
identify: (unique_id: string) => void;
Expand All @@ -11,7 +13,7 @@ export function initializeAnalytics(): void {
const umamiIdKey = 'mvw_uuid';
let uniqueId = localStorage.getItem(umamiIdKey);
if (!uniqueId) {
uniqueId = crypto.randomUUID();
uniqueId = uuid();
localStorage.setItem(umamiIdKey, uniqueId);
}
umami.identify(uniqueId);
Expand Down
23 changes: 23 additions & 0 deletions client/src/lib/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function uuid(): string {
if (typeof globalThis !== 'undefined' && globalThis.crypto && typeof globalThis.crypto.randomUUID === 'function') {
return globalThis.crypto.randomUUID();
}

if (typeof globalThis !== 'undefined' && globalThis.crypto && typeof globalThis.crypto.getRandomValues === 'function') {
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
// RFC4122 v4
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
}

// Last-resort fallback (not cryptographically secure)
const hexChars = '0123456789abcdef';
const s: string[] = new Array(36);
for (let i = 0; i < 36; i++) s[i] = hexChars[(Math.random() * 16) | 0];
s[14] = '4';
s[19] = hexChars[(parseInt(s[19], 16) & 0x3) | 0x8];
s[8] = s[13] = s[18] = s[23] = '-';
return s.join('');
}