From 0aa57d5411f2a8a6939b668af923ce4d92d2163e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=81=E5=8F=B6?= Date: Sat, 23 May 2026 20:49:49 +0800 Subject: [PATCH] Add fallback ID generator for environments without crypto.randomUUID --- src/components/sql-editor/hooks/useEditorTabs.ts | 3 ++- src/lib/create-id.ts | 11 +++++++++++ src/lib/staged-changes.ts | 7 ++++--- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 src/lib/create-id.ts diff --git a/src/components/sql-editor/hooks/useEditorTabs.ts b/src/components/sql-editor/hooks/useEditorTabs.ts index 8adb2a4..49c4e78 100644 --- a/src/components/sql-editor/hooks/useEditorTabs.ts +++ b/src/components/sql-editor/hooks/useEditorTabs.ts @@ -1,5 +1,6 @@ import { useState, useCallback, useMemo, useEffect } from 'react' import type { ObjectType } from '../ObjectTree' // Used for SchemaTab.objectType +import { createId } from '@/lib/create-id' export interface QueryTab { type: 'query' @@ -430,7 +431,7 @@ export function useEditorTabs(connectionId: string) { title += ' (cancelled)' } return { - id: crypto.randomUUID(), + id: createId(), title, result, sql: options?.sql, diff --git a/src/lib/create-id.ts b/src/lib/create-id.ts new file mode 100644 index 0000000..79c9975 --- /dev/null +++ b/src/lib/create-id.ts @@ -0,0 +1,11 @@ +export function createId(): string { + if ( + typeof globalThis !== 'undefined' && + globalThis.crypto && + typeof globalThis.crypto.randomUUID === 'function' + ) { + return globalThis.crypto.randomUUID() + } + + return `id-${Date.now()}-${Math.random().toString(36).slice(2, 11)}` +} diff --git a/src/lib/staged-changes.ts b/src/lib/staged-changes.ts index e70e12d..effb191 100644 --- a/src/lib/staged-changes.ts +++ b/src/lib/staged-changes.ts @@ -1,4 +1,5 @@ import type { ColumnMetadata } from '@/components/sql-editor/hooks/useEditorTabs' +import { createId } from '@/lib/create-id' export type StagedChangeType = 'delete' | 'update' | 'insert' @@ -138,7 +139,7 @@ export function createStagedDelete( } return { - id: crypto.randomUUID(), + id: createId(), type: 'delete', tables, rowCount: selectedRows.length, @@ -263,7 +264,7 @@ export function createStagedInsert( } return { - id: crypto.randomUUID(), + id: createId(), type: 'insert', tables, rowCount: rows.length, @@ -323,7 +324,7 @@ export function createStagedUpdate( } return { - id: crypto.randomUUID(), + id: createId(), type: 'update', tables, rowCount: 1,