/** * The actual `ulid` library depends on a secure random number generator, which looks like it * may require a native dependency to polyfill in React Native. * Since this is only used as a local temporary ID, I think we can get away with a simpler * implementation that doesn't require any native dependencies. */ const ULID_CHARS = '0123456789ABCDEFGHJKMNPQRSTVWXYZ' // Crockford's Base32 function encodeBase32(value: number, length: number) { let result = '' for (let i = 0; i < length; i++) { result = ULID_CHARS[value % 32] + result value = Math.floor(value / 32) } return result } export function generatePlaceholderUlid({ offsetMs = 0 }: { offsetMs?: number } = {}): string { const timestamp = Date.now() + offsetMs // Milliseconds since epoch // 48 bits for timestamp (6 Base32 characters) const timeComponent = encodeBase32(timestamp, 10) // 48 bits = 6 bytes = 10 Base32 characters (approx, needs more precise bit shifting) // 80 bits for randomness (16 Base32 characters) let randomComponent = '' for (let i = 0; i < 16; i++) { // Generate 16 random Base32 chars randomComponent += ULID_CHARS[Math.floor(Math.random() * 32)] } return timeComponent + randomComponent }