mirror of
https://github.com/langgenius/dify.git
synced 2026-04-05 06:09:24 +08:00
refactor: replace useContext with use in selected batch (#34450)
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
import { render, screen } from '@testing-library/react'
|
import { render, screen } from '@testing-library/react'
|
||||||
import * as React from 'react'
|
import * as React from 'react'
|
||||||
import { useContext } from 'react'
|
import { use } from 'react'
|
||||||
import { FeaturesContext, FeaturesProvider } from '../context'
|
import { FeaturesContext, FeaturesProvider } from '../context'
|
||||||
|
|
||||||
const TestConsumer = () => {
|
const TestConsumer = () => {
|
||||||
const store = useContext(FeaturesContext)
|
const store = use(FeaturesContext)
|
||||||
if (!store)
|
if (!store)
|
||||||
return <div>no store</div>
|
return <div>no store</div>
|
||||||
|
|
||||||
@@ -34,10 +34,10 @@ describe('FeaturesProvider', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should maintain the same store reference across re-renders', () => {
|
it('should maintain the same store reference across re-renders', () => {
|
||||||
const storeRefs: Array<ReturnType<typeof useContext>> = []
|
const storeRefs: Array<React.ContextType<typeof FeaturesContext>> = []
|
||||||
|
|
||||||
const StoreRefCollector = () => {
|
const StoreRefCollector = () => {
|
||||||
const store = useContext(FeaturesContext)
|
const store = use(FeaturesContext)
|
||||||
storeRefs.push(store)
|
storeRefs.push(store)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { render, screen } from '@testing-library/react'
|
import { render, screen } from '@testing-library/react'
|
||||||
import { useContext } from 'react'
|
import { use } from 'react'
|
||||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
import DataSourceProvider, { DataSourceContext } from '../provider'
|
import DataSourceProvider, { DataSourceContext } from '../provider'
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ vi.mock('../', () => ({
|
|||||||
|
|
||||||
// Test consumer component that reads from context
|
// Test consumer component that reads from context
|
||||||
function ContextConsumer() {
|
function ContextConsumer() {
|
||||||
const store = useContext(DataSourceContext)
|
const store = use(DataSourceContext)
|
||||||
return (
|
return (
|
||||||
<div data-testid="context-value" data-has-store={store !== null}>
|
<div data-testid="context-value" data-has-store={store !== null}>
|
||||||
{store ? 'has-store' : 'no-store'}
|
{store ? 'has-store' : 'no-store'}
|
||||||
@@ -65,7 +65,7 @@ describe('DataSourceProvider', () => {
|
|||||||
const storeValues: Array<typeof mockStore | null> = []
|
const storeValues: Array<typeof mockStore | null> = []
|
||||||
|
|
||||||
function StoreCapture() {
|
function StoreCapture() {
|
||||||
const store = useContext(DataSourceContext)
|
const store = use(DataSourceContext)
|
||||||
storeValues.push(store as typeof mockStore | null)
|
storeValues.push(store as typeof mockStore | null)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import type { LocalFileSliceShape } from './slices/local-file'
|
|||||||
import type { OnlineDocumentSliceShape } from './slices/online-document'
|
import type { OnlineDocumentSliceShape } from './slices/online-document'
|
||||||
import type { OnlineDriveSliceShape } from './slices/online-drive'
|
import type { OnlineDriveSliceShape } from './slices/online-drive'
|
||||||
import type { WebsiteCrawlSliceShape } from './slices/website-crawl'
|
import type { WebsiteCrawlSliceShape } from './slices/website-crawl'
|
||||||
import { useContext } from 'react'
|
import { use } from 'react'
|
||||||
import { createStore, useStore } from 'zustand'
|
import { createStore, useStore } from 'zustand'
|
||||||
import { DataSourceContext } from './provider'
|
import { DataSourceContext } from './provider'
|
||||||
import { createCommonSlice } from './slices/common'
|
import { createCommonSlice } from './slices/common'
|
||||||
@@ -29,7 +29,7 @@ export const createDataSourceStore = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const useDataSourceStoreWithSelector = <T>(selector: (state: DataSourceShape) => T): T => {
|
export const useDataSourceStoreWithSelector = <T>(selector: (state: DataSourceShape) => T): T => {
|
||||||
const store = useContext(DataSourceContext)
|
const store = use(DataSourceContext)
|
||||||
if (!store)
|
if (!store)
|
||||||
throw new Error('Missing DataSourceContext.Provider in the tree')
|
throw new Error('Missing DataSourceContext.Provider in the tree')
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ export const useDataSourceStoreWithSelector = <T>(selector: (state: DataSourceSh
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const useDataSourceStore = () => {
|
export const useDataSourceStore = () => {
|
||||||
const store = useContext(DataSourceContext)
|
const store = use(DataSourceContext)
|
||||||
if (!store)
|
if (!store)
|
||||||
throw new Error('Missing DataSourceContext.Provider in the tree')
|
throw new Error('Missing DataSourceContext.Provider in the tree')
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { ReactNode } from 'react'
|
import type { ReactNode } from 'react'
|
||||||
import { createContext, useContext } from 'react'
|
import { createContext, use } from 'react'
|
||||||
import { cn } from '@/utils/classnames'
|
import { cn } from '@/utils/classnames'
|
||||||
import styles from './quota-panel.module.css'
|
import styles from './quota-panel.module.css'
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ const SystemQuotaCard = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const Label = ({ children, className }: { children: ReactNode, className?: string }) => {
|
const Label = ({ children, className }: { children: ReactNode, className?: string }) => {
|
||||||
const variant = useContext(VariantContext)
|
const variant = use(VariantContext)
|
||||||
return (
|
return (
|
||||||
<div className={cn(
|
<div className={cn(
|
||||||
'relative z-1 flex items-center gap-1 truncate px-1.5 pt-1 system-xs-medium',
|
'relative z-1 flex items-center gap-1 truncate px-1.5 pt-1 system-xs-medium',
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { render, screen, waitFor } from '@testing-library/react'
|
import { render, screen, waitFor } from '@testing-library/react'
|
||||||
import { useContext } from 'react'
|
import { use } from 'react'
|
||||||
import { HooksStoreContext, HooksStoreContextProvider } from '../provider'
|
import { HooksStoreContext, HooksStoreContextProvider } from '../provider'
|
||||||
|
|
||||||
const mockRefreshAll = vi.fn()
|
const mockRefreshAll = vi.fn()
|
||||||
@@ -27,7 +27,7 @@ vi.mock('../store', async () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const Consumer = () => {
|
const Consumer = () => {
|
||||||
const store = useContext(HooksStoreContext)
|
const store = use(HooksStoreContext)
|
||||||
return <div>{store ? 'has-hooks-store' : 'missing-hooks-store'}</div>
|
return <div>{store ? 'has-hooks-store' : 'missing-hooks-store'}</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import type { ReactNode } from 'react'
|
import type { ReactNode } from 'react'
|
||||||
import { createContext, useContext } from 'react'
|
import { createContext, use } from 'react'
|
||||||
|
|
||||||
type MCPToolAvailabilityContextValue = {
|
type MCPToolAvailabilityContextValue = {
|
||||||
versionSupported?: boolean
|
versionSupported?: boolean
|
||||||
@@ -26,7 +26,7 @@ export const MCPToolAvailabilityProvider = ({
|
|||||||
)
|
)
|
||||||
|
|
||||||
export const useMCPToolAvailability = (): MCPToolAvailability => {
|
export const useMCPToolAvailability = (): MCPToolAvailability => {
|
||||||
const context = useContext(MCPToolAvailabilityContext)
|
const context = use(MCPToolAvailabilityContext)
|
||||||
if (context === undefined)
|
if (context === undefined)
|
||||||
return { allowed: true }
|
return { allowed: true }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user