diff --git a/packages/playground/src/App.tsx b/packages/playground/src/App.tsx index 42d93bb..4c1c46f 100644 --- a/packages/playground/src/App.tsx +++ b/packages/playground/src/App.tsx @@ -7,10 +7,11 @@ import { ThemeProvider } from '@/components/darkmode-toggle/theme-provider' import { StatementProvider } from '@/contexts/statement-context-provider' import { FilesProvider } from '@/contexts/files-context-provider' import { SchemaProvider } from '@/contexts/schema-context-provider' +import { ChatProvider } from '@/contexts/chat-context-provider' +import { EditorExampleWithSelect } from '@/examples/editor-example-with-select' import { EditorExample } from '@/examples/editor-example' -import { ChatProvider } from './contexts/chat-context-provider' -import { EditorExampleWithSelect } from './examples/editor-example-with-select' +import { UrlStateProvider } from '@/hooks/use-url-state' const queryClient = new QueryClient({ defaultOptions: { @@ -44,17 +45,15 @@ function App() { const params = new URLSearchParams(window.location.search) const example = params.get('example') const withSelect = params.get('with_select') - const editorTheme = params.get('theme') ?? 'oneDark' if (example !== null) { if (withSelect !== null) { return ( - + + + ) } diff --git a/packages/playground/src/contexts/files-context-provider.tsx b/packages/playground/src/contexts/files-context-provider.tsx index fd47907..fc24788 100644 --- a/packages/playground/src/contexts/files-context-provider.tsx +++ b/packages/playground/src/contexts/files-context-provider.tsx @@ -1,4 +1,4 @@ -import React, { useMemo } from 'react' +import React, { useMemo, useState } from 'react' import { addFile, delFile, @@ -18,9 +18,9 @@ function FilesLoader() { } export function FilesProvider(props: { children: React.ReactNode }) { - const [allFiles, setAllFiles] = React.useState([]) - const [openedFiles, setOpenedFiles] = React.useState([]) - const [activeFileId, setActiveFileId] = React.useState(null) + const [allFiles, setAllFiles] = useState([]) + const [openedFiles, setOpenedFiles] = useState([]) + const [activeFileId, setActiveFileId] = useState(null) const ctxValue = useMemo( () => ({ diff --git a/packages/playground/src/contexts/schema-context-provider.tsx b/packages/playground/src/contexts/schema-context-provider.tsx index 30e4a51..319dace 100644 --- a/packages/playground/src/contexts/schema-context-provider.tsx +++ b/packages/playground/src/contexts/schema-context-provider.tsx @@ -1,4 +1,4 @@ -import React, { useMemo } from 'react' +import React, { useMemo, useState } from 'react' import { getSchema } from '@/api/tidbcloud/schema-api' import { SchemaContext, SchemaRes } from './schema-context' @@ -10,7 +10,7 @@ function SchemaLoader() { } export function SchemaProvider(props: { children: React.ReactNode }) { - const [schema, setSchema] = React.useState(null) + const [schema, setSchema] = useState(null) const ctxValue = useMemo( () => ({ loadSchema: getSchema, schema, setSchema }), diff --git a/packages/playground/src/contexts/statement-context-provider.tsx b/packages/playground/src/contexts/statement-context-provider.tsx index 1c6975e..7915d75 100644 --- a/packages/playground/src/contexts/statement-context-provider.tsx +++ b/packages/playground/src/contexts/statement-context-provider.tsx @@ -1,4 +1,4 @@ -import React, { useMemo } from 'react' +import React, { useMemo, useState } from 'react' import { SqlStatement } from '@tidbcloud/codemirror-extension-sql-parser' @@ -6,7 +6,7 @@ import { runSQL } from '@/api/tidbcloud/statement-api' import { StatementContext } from './statement-context' export function StatementProvider(props: { children: React.ReactNode }) { - const [runResult, setRunResult] = React.useState({}) + const [runResult, setRunResult] = useState({}) const runStatement = (_fileId: string, statement: SqlStatement) => { return runSQL({ database: statement.database, sql: statement.content }) diff --git a/packages/playground/src/examples/editor-example-with-select.tsx b/packages/playground/src/examples/editor-example-with-select.tsx index 047b9a9..21ca97a 100644 --- a/packages/playground/src/examples/editor-example-with-select.tsx +++ b/packages/playground/src/examples/editor-example-with-select.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react' +import { useEffect } from 'react' import { EnterFullScreenIcon, GitHubLogoIcon } from '@radix-ui/react-icons' @@ -15,16 +15,13 @@ import { Button } from '@/components/ui/button' import { useTheme } from '@/components/darkmode-toggle/theme-provider' import { EditorExample } from './editor-example' +import { useExampleUrlState } from './url-state' + +function ExampleSelect() { + const { example, setExample } = useExampleUrlState() -function ExampleSelect({ - value, - onChange -}: { - value: string - onChange: (v: string) => void -}) { return ( - @@ -46,15 +43,11 @@ function ExampleSelect({ ) } -function ThemeSelect({ - value, - onChange -}: { - value: string - onChange: (v: string) => void -}) { +function ThemeSelect() { + const { theme, setTheme } = useExampleUrlState() + return ( - @@ -70,34 +63,11 @@ function ThemeSelect({ ) } -function updateUrlParam(key: string, value: string) { - const url = new URL(window.location.href) - const params = new URLSearchParams(url.search) - params.set(key, value) - window.history.replaceState({}, '', `${url.pathname}?${params.toString()}`) -} +export function EditorExampleWithSelect() { + const { example, theme: editorTheme } = useExampleUrlState() -export function EditorExampleWithSelect({ - defExample, - defTheme -}: { - defExample: string - defTheme: string -}) { - const [example, setExample] = useState(defExample) - const [editorTheme, setEditorTheme] = useState(defTheme) const { setTheme: setAppTheme } = useTheme() - function onExampleChange(v: string) { - setExample(v) - updateUrlParam('example', v) - } - - function onThemeChange(v: string) { - setEditorTheme(v) - updateUrlParam('theme', v) - } - useEffect(() => { setAppTheme( editorTheme === 'oneDark' || editorTheme === 'dark' ? 'dark' : 'light' @@ -113,8 +83,8 @@ export function EditorExampleWithSelect({
- - + +