style: refactor context
This commit is contained in:
parent
6b759795d5
commit
5ad148b8f9
@ -1,7 +1,7 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { createContext, useContext } from 'use-context-selector'
|
|
||||||
import type { ModelAndParameter } from '../types'
|
import type { ModelAndParameter } from '../types'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export type DebugWithMultipleModelContextType = {
|
export type DebugWithMultipleModelContextType = {
|
||||||
multipleModelConfigs: ModelAndParameter[]
|
multipleModelConfigs: ModelAndParameter[]
|
||||||
@ -9,13 +9,9 @@ export type DebugWithMultipleModelContextType = {
|
|||||||
onDebugWithMultipleModelChange: (singleModelConfig: ModelAndParameter) => void
|
onDebugWithMultipleModelChange: (singleModelConfig: ModelAndParameter) => void
|
||||||
checkCanSend?: () => boolean
|
checkCanSend?: () => boolean
|
||||||
}
|
}
|
||||||
const DebugWithMultipleModelContext = createContext<DebugWithMultipleModelContextType>({
|
const [,useDebugWithMultipleModelContext, DebugWithMultipleModelContext] = createSelectorCtx<DebugWithMultipleModelContextType>()
|
||||||
multipleModelConfigs: [],
|
|
||||||
onMultipleModelConfigsChange: () => {},
|
|
||||||
onDebugWithMultipleModelChange: () => {},
|
|
||||||
})
|
|
||||||
|
|
||||||
export const useDebugWithMultipleModelContext = () => useContext(DebugWithMultipleModelContext)
|
export { useDebugWithMultipleModelContext }
|
||||||
|
|
||||||
type DebugWithMultipleModelContextProviderProps = {
|
type DebugWithMultipleModelContextProviderProps = {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
|
@ -13,7 +13,7 @@ import InfiniteScroll from 'react-infinite-scroll-component'
|
|||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import utc from 'dayjs/plugin/utc'
|
import utc from 'dayjs/plugin/utc'
|
||||||
import timezone from 'dayjs/plugin/timezone'
|
import timezone from 'dayjs/plugin/timezone'
|
||||||
import { createContext, useContext } from 'use-context-selector'
|
import { useContext } from 'use-context-selector'
|
||||||
import { useShallow } from 'zustand/react/shallow'
|
import { useShallow } from 'zustand/react/shallow'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import type { ChatItemInTree } from '../../base/chat/types'
|
import type { ChatItemInTree } from '../../base/chat/types'
|
||||||
@ -45,6 +45,7 @@ import { CopyIcon } from '@/app/components/base/copy-icon'
|
|||||||
import { buildChatItemTree, getThreadMessages } from '@/app/components/base/chat/utils'
|
import { buildChatItemTree, getThreadMessages } from '@/app/components/base/chat/utils'
|
||||||
import { getProcessedFilesFromResponse } from '@/app/components/base/file-uploader/utils'
|
import { getProcessedFilesFromResponse } from '@/app/components/base/file-uploader/utils'
|
||||||
import { correctProvider } from '@/utils'
|
import { correctProvider } from '@/utils'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
dayjs.extend(utc)
|
dayjs.extend(utc)
|
||||||
dayjs.extend(timezone)
|
dayjs.extend(timezone)
|
||||||
@ -62,7 +63,7 @@ type IDrawerContext = {
|
|||||||
appDetail?: App
|
appDetail?: App
|
||||||
}
|
}
|
||||||
|
|
||||||
const DrawerContext = createContext<IDrawerContext>({} as IDrawerContext)
|
const [,, DrawerContext] = createSelectorCtx<IDrawerContext>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Icon component with numbers
|
* Icon component with numbers
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import type { RefObject } from 'react'
|
import type { RefObject } from 'react'
|
||||||
import { createContext, useContext } from 'use-context-selector'
|
|
||||||
import type {
|
import type {
|
||||||
Callback,
|
Callback,
|
||||||
ChatConfig,
|
ChatConfig,
|
||||||
@ -15,8 +14,9 @@ import type {
|
|||||||
AppMeta,
|
AppMeta,
|
||||||
ConversationItem,
|
ConversationItem,
|
||||||
} from '@/models/share'
|
} from '@/models/share'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export interface ChatWithHistoryContextValue {
|
export type ChatWithHistoryContextValue = {
|
||||||
appInfoError?: any
|
appInfoError?: any
|
||||||
appInfoLoading?: boolean
|
appInfoLoading?: boolean
|
||||||
appMeta?: AppMeta
|
appMeta?: AppMeta
|
||||||
@ -51,29 +51,4 @@ export interface ChatWithHistoryContextValue {
|
|||||||
themeBuilder?: ThemeBuilder
|
themeBuilder?: ThemeBuilder
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ChatWithHistoryContext = createContext<ChatWithHistoryContextValue>({
|
export const [, useChatWithHistoryContext, ChatWithHistoryContext] = createSelectorCtx<ChatWithHistoryContextValue>()
|
||||||
currentConversationId: '',
|
|
||||||
appPrevChatList: [],
|
|
||||||
pinnedConversationList: [],
|
|
||||||
conversationList: [],
|
|
||||||
showConfigPanelBeforeChat: false,
|
|
||||||
newConversationInputs: {},
|
|
||||||
newConversationInputsRef: { current: {} },
|
|
||||||
handleNewConversationInputsChange: () => {},
|
|
||||||
inputsForms: [],
|
|
||||||
handleNewConversation: () => {},
|
|
||||||
handleStartChat: () => {},
|
|
||||||
handleChangeConversation: () => {},
|
|
||||||
handlePinConversation: () => {},
|
|
||||||
handleUnpinConversation: () => {},
|
|
||||||
handleDeleteConversation: () => {},
|
|
||||||
conversationRenaming: false,
|
|
||||||
handleRenameConversation: () => {},
|
|
||||||
handleNewConversationCompleted: () => {},
|
|
||||||
chatShouldReloadKey: '',
|
|
||||||
isMobile: false,
|
|
||||||
isInstalledApp: false,
|
|
||||||
handleFeedback: () => {},
|
|
||||||
currentChatInstanceRef: { current: { handleStop: () => {} } },
|
|
||||||
})
|
|
||||||
export const useChatWithHistoryContext = () => useContext(ChatWithHistoryContext)
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import type { ReactNode } from 'react'
|
import type { ReactNode } from 'react'
|
||||||
import { createContext, useContext } from 'use-context-selector'
|
|
||||||
import type { ChatProps } from './index'
|
import type { ChatProps } from './index'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export type ChatContextValue = Pick<ChatProps, 'config'
|
export type ChatContextValue = Pick<ChatProps, 'config'
|
||||||
| 'isResponding'
|
| 'isResponding'
|
||||||
@ -18,9 +18,7 @@ export type ChatContextValue = Pick<ChatProps, 'config'
|
|||||||
| 'onFeedback'
|
| 'onFeedback'
|
||||||
>
|
>
|
||||||
|
|
||||||
const ChatContext = createContext<ChatContextValue>({
|
const [, useChatContext, ChatContext] = createSelectorCtx<ChatContextValue>()
|
||||||
chatList: [],
|
|
||||||
})
|
|
||||||
|
|
||||||
type ChatContextProviderProps = {
|
type ChatContextProviderProps = {
|
||||||
children: ReactNode
|
children: ReactNode
|
||||||
@ -61,6 +59,6 @@ export const ChatContextProvider = ({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useChatContext = () => useContext(ChatContext)
|
export { useChatContext }
|
||||||
|
|
||||||
export default ChatContext
|
export default ChatContext
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import type { RefObject } from 'react'
|
import type { RefObject } from 'react'
|
||||||
import { createContext, useContext } from 'use-context-selector'
|
|
||||||
import type {
|
import type {
|
||||||
ChatConfig,
|
ChatConfig,
|
||||||
ChatItem,
|
ChatItem,
|
||||||
@ -14,8 +13,9 @@ import type {
|
|||||||
AppMeta,
|
AppMeta,
|
||||||
ConversationItem,
|
ConversationItem,
|
||||||
} from '@/models/share'
|
} from '@/models/share'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export interface EmbeddedChatbotContextValue {
|
export type EmbeddedChatbotContextValue = {
|
||||||
appInfoError?: any
|
appInfoError?: any
|
||||||
appInfoLoading?: boolean
|
appInfoLoading?: boolean
|
||||||
appMeta?: AppMeta
|
appMeta?: AppMeta
|
||||||
@ -45,24 +45,4 @@ export interface EmbeddedChatbotContextValue {
|
|||||||
themeBuilder?: ThemeBuilder
|
themeBuilder?: ThemeBuilder
|
||||||
}
|
}
|
||||||
|
|
||||||
export const EmbeddedChatbotContext = createContext<EmbeddedChatbotContextValue>({
|
export const [, useEmbeddedChatbotContext, EmbeddedChatbotContext] = createSelectorCtx<EmbeddedChatbotContextValue>()
|
||||||
currentConversationId: '',
|
|
||||||
appPrevChatList: [],
|
|
||||||
pinnedConversationList: [],
|
|
||||||
conversationList: [],
|
|
||||||
showConfigPanelBeforeChat: false,
|
|
||||||
newConversationInputs: {},
|
|
||||||
newConversationInputsRef: { current: {} },
|
|
||||||
handleNewConversationInputsChange: () => {},
|
|
||||||
inputsForms: [],
|
|
||||||
handleNewConversation: () => {},
|
|
||||||
handleStartChat: () => {},
|
|
||||||
handleChangeConversation: () => {},
|
|
||||||
handleNewConversationCompleted: () => {},
|
|
||||||
chatShouldReloadKey: '',
|
|
||||||
isMobile: false,
|
|
||||||
isInstalledApp: false,
|
|
||||||
handleFeedback: () => {},
|
|
||||||
currentChatInstanceRef: { current: { handleStop: () => {} } },
|
|
||||||
})
|
|
||||||
export const useEmbeddedChatbotContext = () => useContext(EmbeddedChatbotContext)
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import {
|
import {
|
||||||
createContext,
|
|
||||||
useRef,
|
useRef,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import type {
|
import type {
|
||||||
@ -7,8 +6,9 @@ import type {
|
|||||||
FeaturesStore,
|
FeaturesStore,
|
||||||
} from './store'
|
} from './store'
|
||||||
import { createFeaturesStore } from './store'
|
import { createFeaturesStore } from './store'
|
||||||
|
import { createCtx } from '@/utils/context'
|
||||||
|
|
||||||
export const FeaturesContext = createContext<FeaturesStore | null>(null)
|
export const [, , FeaturesContext] = createCtx<FeaturesStore>()
|
||||||
|
|
||||||
type FeaturesProviderProps = {
|
type FeaturesProviderProps = {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
import {
|
import {
|
||||||
createContext,
|
|
||||||
useContext,
|
|
||||||
useRef,
|
useRef,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import {
|
import {
|
||||||
@ -10,6 +8,7 @@ import {
|
|||||||
import type {
|
import type {
|
||||||
FileEntity,
|
FileEntity,
|
||||||
} from './types'
|
} from './types'
|
||||||
|
import { createCtx } from '@/utils/context'
|
||||||
|
|
||||||
type Shape = {
|
type Shape = {
|
||||||
files: FileEntity[]
|
files: FileEntity[]
|
||||||
@ -30,20 +29,13 @@ export const createFileStore = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
type FileStore = ReturnType<typeof createFileStore>
|
type FileStore = ReturnType<typeof createFileStore>
|
||||||
export const FileContext = createContext<FileStore | null>(null)
|
export const [, useFileStore, FileContext] = createCtx<FileStore>()
|
||||||
|
|
||||||
export function useStore<T>(selector: (state: Shape) => T): T {
|
export function useStore<T>(selector: (state: Shape) => T): T {
|
||||||
const store = useContext(FileContext)
|
const store = useFileStore()
|
||||||
if (!store)
|
|
||||||
throw new Error('Missing FileContext.Provider in the tree')
|
|
||||||
|
|
||||||
return useZustandStore(store, selector)
|
return useZustandStore(store, selector)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useFileStore = () => {
|
|
||||||
return useContext(FileContext)!
|
|
||||||
}
|
|
||||||
|
|
||||||
type FileProviderProps = {
|
type FileProviderProps = {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
value?: FileEntity[]
|
value?: FileEntity[]
|
||||||
|
@ -17,6 +17,7 @@ import {
|
|||||||
|
|
||||||
import type { OffsetOptions, Placement } from '@floating-ui/react'
|
import type { OffsetOptions, Placement } from '@floating-ui/react'
|
||||||
import cn from '@/utils/classnames'
|
import cn from '@/utils/classnames'
|
||||||
|
import { createCtx } from '@/utils/context'
|
||||||
export type PortalToFollowElemOptions = {
|
export type PortalToFollowElemOptions = {
|
||||||
/*
|
/*
|
||||||
* top, bottom, left, right
|
* top, bottom, left, right
|
||||||
@ -78,18 +79,9 @@ export function usePortalToFollowElem({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ContextType = ReturnType<typeof usePortalToFollowElem> | null
|
type ContextType = ReturnType<typeof usePortalToFollowElem>
|
||||||
|
|
||||||
const PortalToFollowElemContext = React.createContext<ContextType>(null)
|
const [, usePortalToFollowElemContext, PortalToFollowElemContext] = createCtx<ContextType>()
|
||||||
|
|
||||||
export function usePortalToFollowElemContext() {
|
|
||||||
const context = React.useContext(PortalToFollowElemContext)
|
|
||||||
|
|
||||||
if (context == null)
|
|
||||||
throw new Error('PortalToFollowElem components must be wrapped in <PortalToFollowElem />')
|
|
||||||
|
|
||||||
return context
|
|
||||||
}
|
|
||||||
|
|
||||||
export function PortalToFollowElem({
|
export function PortalToFollowElem({
|
||||||
children,
|
children,
|
||||||
|
@ -8,8 +8,8 @@ import {
|
|||||||
InformationCircleIcon,
|
InformationCircleIcon,
|
||||||
XCircleIcon,
|
XCircleIcon,
|
||||||
} from '@heroicons/react/20/solid'
|
} from '@heroicons/react/20/solid'
|
||||||
import { createContext, useContext } from 'use-context-selector'
|
|
||||||
import classNames from '@/utils/classnames'
|
import classNames from '@/utils/classnames'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export type IToastProps = {
|
export type IToastProps = {
|
||||||
type?: 'success' | 'error' | 'warning' | 'info'
|
type?: 'success' | 'error' | 'warning' | 'info'
|
||||||
@ -23,8 +23,7 @@ type IToastContext = {
|
|||||||
notify: (props: IToastProps) => void
|
notify: (props: IToastProps) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ToastContext = createContext<IToastContext>({} as IToastContext)
|
export const [, useToastContext, ToastContext] = createSelectorCtx<IToastContext>()
|
||||||
export const useToastContext = () => useContext(ToastContext)
|
|
||||||
const Toast = ({
|
const Toast = ({
|
||||||
type = 'info',
|
type = 'info',
|
||||||
message,
|
message,
|
||||||
@ -46,7 +45,7 @@ const Toast = ({
|
|||||||
type === 'info' ? 'bg-blue-50' : '',
|
type === 'info' ? 'bg-blue-50' : '',
|
||||||
)}>
|
)}>
|
||||||
<div className="flex">
|
<div className="flex">
|
||||||
<div className="flex-shrink-0">
|
<div className="shrink-0">
|
||||||
{type === 'success' && <CheckCircleIcon className="w-5 h-5 text-green-400" aria-hidden="true" />}
|
{type === 'success' && <CheckCircleIcon className="w-5 h-5 text-green-400" aria-hidden="true" />}
|
||||||
{type === 'error' && <XCircleIcon className="w-5 h-5 text-red-400" aria-hidden="true" />}
|
{type === 'error' && <XCircleIcon className="w-5 h-5 text-red-400" aria-hidden="true" />}
|
||||||
{type === 'warning' && <ExclamationTriangleIcon className="w-5 h-5 text-yellow-400" aria-hidden="true" />}
|
{type === 'warning' && <ExclamationTriangleIcon className="w-5 h-5 text-yellow-400" aria-hidden="true" />}
|
||||||
|
@ -3,7 +3,7 @@ import type { FC } from 'react'
|
|||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
import { ArrowLeftIcon } from '@heroicons/react/24/solid'
|
import { ArrowLeftIcon } from '@heroicons/react/24/solid'
|
||||||
import { createContext, useContext } from 'use-context-selector'
|
import { useContext } from 'use-context-selector'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter } from 'next/navigation'
|
||||||
import { omit } from 'lodash-es'
|
import { omit } from 'lodash-es'
|
||||||
@ -25,8 +25,9 @@ import type { DocForm } from '@/models/datasets'
|
|||||||
import { useDatasetDetailContext } from '@/context/dataset-detail'
|
import { useDatasetDetailContext } from '@/context/dataset-detail'
|
||||||
import FloatRightContainer from '@/app/components/base/float-right-container'
|
import FloatRightContainer from '@/app/components/base/float-right-container'
|
||||||
import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
|
import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export const DocumentContext = createContext<{ datasetId?: string; documentId?: string; docForm: string }>({ docForm: '' })
|
export const [,, DocumentContext] = createSelectorCtx<{ datasetId?: string; documentId?: string; docForm: string }>()
|
||||||
|
|
||||||
type DocumentTitleProps = {
|
type DocumentTitleProps = {
|
||||||
extension?: string
|
extension?: string
|
||||||
|
@ -9,7 +9,6 @@ import {
|
|||||||
useState,
|
useState,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import {
|
import {
|
||||||
createContext,
|
|
||||||
useContextSelector,
|
useContextSelector,
|
||||||
} from 'use-context-selector'
|
} from 'use-context-selector'
|
||||||
import { PLUGIN_TYPE_SEARCH_MAP } from './plugin-type-switch'
|
import { PLUGIN_TYPE_SEARCH_MAP } from './plugin-type-switch'
|
||||||
@ -23,6 +22,7 @@ import {
|
|||||||
useMarketplaceCollectionsAndPlugins,
|
useMarketplaceCollectionsAndPlugins,
|
||||||
useMarketplacePlugins,
|
useMarketplacePlugins,
|
||||||
} from './hooks'
|
} from './hooks'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export type MarketplaceContextValue = {
|
export type MarketplaceContextValue = {
|
||||||
intersected: boolean
|
intersected: boolean
|
||||||
@ -44,25 +44,7 @@ export type MarketplaceContextValue = {
|
|||||||
isLoading: boolean
|
isLoading: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const MarketplaceContext = createContext<MarketplaceContextValue>({
|
export const [,, MarketplaceContext] = createSelectorCtx<MarketplaceContextValue>()
|
||||||
intersected: true,
|
|
||||||
setIntersected: () => {},
|
|
||||||
searchPluginText: '',
|
|
||||||
handleSearchPluginTextChange: () => {},
|
|
||||||
filterPluginTags: [],
|
|
||||||
handleFilterPluginTagsChange: () => {},
|
|
||||||
activePluginType: PLUGIN_TYPE_SEARCH_MAP.all,
|
|
||||||
handleActivePluginTypeChange: () => {},
|
|
||||||
plugins: undefined,
|
|
||||||
resetPlugins: () => {},
|
|
||||||
sort: DEFAULT_SORT,
|
|
||||||
handleSortChange: () => {},
|
|
||||||
marketplaceCollectionsFromClient: [],
|
|
||||||
setMarketplaceCollectionsFromClient: () => {},
|
|
||||||
marketplaceCollectionPluginsMapFromClient: {},
|
|
||||||
setMarketplaceCollectionPluginsMapFromClient: () => {},
|
|
||||||
isLoading: false,
|
|
||||||
})
|
|
||||||
|
|
||||||
type MarketplaceContextProviderProps = {
|
type MarketplaceContextProviderProps = {
|
||||||
children: ReactNode
|
children: ReactNode
|
||||||
|
@ -7,7 +7,6 @@ import {
|
|||||||
useState,
|
useState,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import {
|
import {
|
||||||
createContext,
|
|
||||||
useContextSelector,
|
useContextSelector,
|
||||||
} from 'use-context-selector'
|
} from 'use-context-selector'
|
||||||
import { useSelector as useAppContextSelector } from '@/context/app-context'
|
import { useSelector as useAppContextSelector } from '@/context/app-context'
|
||||||
@ -15,6 +14,7 @@ import type { PluginDetail } from '../types'
|
|||||||
import type { FilterState } from './filter-management'
|
import type { FilterState } from './filter-management'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
|
import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export type PluginPageContextValue = {
|
export type PluginPageContextValue = {
|
||||||
containerRef: React.RefObject<HTMLDivElement>
|
containerRef: React.RefObject<HTMLDivElement>
|
||||||
@ -27,20 +27,7 @@ export type PluginPageContextValue = {
|
|||||||
options: Array<{ value: string, text: string }>
|
options: Array<{ value: string, text: string }>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PluginPageContext = createContext<PluginPageContextValue>({
|
export const [,, PluginPageContext] = createSelectorCtx<PluginPageContextValue>()
|
||||||
containerRef: { current: null },
|
|
||||||
currentPluginDetail: undefined,
|
|
||||||
setCurrentPluginDetail: () => { },
|
|
||||||
filters: {
|
|
||||||
categories: [],
|
|
||||||
tags: [],
|
|
||||||
searchQuery: '',
|
|
||||||
},
|
|
||||||
setFilters: () => { },
|
|
||||||
activeTab: '',
|
|
||||||
setActiveTab: () => { },
|
|
||||||
options: [],
|
|
||||||
})
|
|
||||||
|
|
||||||
type PluginPageContextProviderProps = {
|
type PluginPageContextProviderProps = {
|
||||||
children: ReactNode
|
children: ReactNode
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import {
|
import {
|
||||||
createContext,
|
|
||||||
useRef,
|
useRef,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import { createWorkflowStore } from './store'
|
import { createWorkflowStore } from './store'
|
||||||
|
import { createCtx } from '@/utils/context'
|
||||||
|
|
||||||
type WorkflowStore = ReturnType<typeof createWorkflowStore>
|
type WorkflowStore = ReturnType<typeof createWorkflowStore>
|
||||||
export const WorkflowContext = createContext<WorkflowStore | null>(null)
|
export const [,, WorkflowContext] = createCtx<WorkflowStore>()
|
||||||
|
|
||||||
type WorkflowProviderProps = {
|
type WorkflowProviderProps = {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
createContext,
|
|
||||||
memo,
|
memo,
|
||||||
useRef,
|
useRef,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
@ -13,9 +12,10 @@ import {
|
|||||||
} from '@lexical/list'
|
} from '@lexical/list'
|
||||||
import { createNoteEditorStore } from './store'
|
import { createNoteEditorStore } from './store'
|
||||||
import theme from './theme'
|
import theme from './theme'
|
||||||
|
import { createCtx } from '@/utils/context'
|
||||||
|
|
||||||
type NoteEditorStore = ReturnType<typeof createNoteEditorStore>
|
type NoteEditorStore = ReturnType<typeof createNoteEditorStore>
|
||||||
const NoteEditorContext = createContext<NoteEditorStore | null>(null)
|
const [,, NoteEditorContext] = createCtx<NoteEditorStore>()
|
||||||
|
|
||||||
type NoteEditorContextProviderProps = {
|
type NoteEditorContextProviderProps = {
|
||||||
value: string
|
value: string
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import { type ReactNode, createContext, useContext, useMemo, useState } from 'react'
|
import { type ReactNode, useContext, useMemo, useState } from 'react'
|
||||||
import { type StoreApi, create } from 'zustand'
|
import { type StoreApi, create } from 'zustand'
|
||||||
import { type TemporalState, temporal } from 'zundo'
|
import { type TemporalState, temporal } from 'zundo'
|
||||||
import isDeepEqual from 'fast-deep-equal'
|
import isDeepEqual from 'fast-deep-equal'
|
||||||
import type { Edge, Node } from './types'
|
import type { Edge, Node } from './types'
|
||||||
import type { WorkflowHistoryEvent } from './hooks'
|
import type { WorkflowHistoryEvent } from './hooks'
|
||||||
|
import { createCtx } from '@/utils/context'
|
||||||
|
|
||||||
export const WorkflowHistoryStoreContext = createContext<WorkflowHistoryStoreContextType>({ store: null, shortcutsEnabled: true, setShortcutsEnabled: () => {} })
|
export const [Provider,,WorkflowHistoryStoreContext] = createCtx<WorkflowHistoryStoreContextType>()
|
||||||
export const Provider = WorkflowHistoryStoreContext.Provider
|
|
||||||
|
|
||||||
export function WorkflowHistoryProvider({
|
export function WorkflowHistoryProvider({
|
||||||
nodes,
|
nodes,
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { createRef, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
import { createContext, useContext, useContextSelector } from 'use-context-selector'
|
import { useContext, useContextSelector } from 'use-context-selector'
|
||||||
import type { FC, ReactNode } from 'react'
|
import type { FC, ReactNode } from 'react'
|
||||||
import { fetchAppList } from '@/service/apps'
|
import { fetchAppList } from '@/service/apps'
|
||||||
import Loading from '@/app/components/base/loading'
|
import Loading from '@/app/components/base/loading'
|
||||||
@ -13,6 +13,7 @@ import type { ICurrentWorkspace, LangGeniusVersionResponse, UserProfileResponse
|
|||||||
import MaintenanceNotice from '@/app/components/header/maintenance-notice'
|
import MaintenanceNotice from '@/app/components/header/maintenance-notice'
|
||||||
import type { SystemFeatures } from '@/types/feature'
|
import type { SystemFeatures } from '@/types/feature'
|
||||||
import { defaultSystemFeatures } from '@/types/feature'
|
import { defaultSystemFeatures } from '@/types/feature'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export type AppContextValue = {
|
export type AppContextValue = {
|
||||||
theme: Theme
|
theme: Theme
|
||||||
@ -54,30 +55,7 @@ const initialWorkspaceInfo: ICurrentWorkspace = {
|
|||||||
in_trail: true,
|
in_trail: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
const AppContext = createContext<AppContextValue>({
|
const [,, AppContext] = createSelectorCtx<AppContextValue>()
|
||||||
theme: Theme.light,
|
|
||||||
systemFeatures: defaultSystemFeatures,
|
|
||||||
setTheme: () => { },
|
|
||||||
apps: [],
|
|
||||||
mutateApps: () => { },
|
|
||||||
userProfile: {
|
|
||||||
id: '',
|
|
||||||
name: '',
|
|
||||||
email: '',
|
|
||||||
avatar: '',
|
|
||||||
is_password_set: false,
|
|
||||||
},
|
|
||||||
currentWorkspace: initialWorkspaceInfo,
|
|
||||||
isCurrentWorkspaceManager: false,
|
|
||||||
isCurrentWorkspaceOwner: false,
|
|
||||||
isCurrentWorkspaceEditor: false,
|
|
||||||
isCurrentWorkspaceDatasetOperator: false,
|
|
||||||
mutateUserProfile: () => { },
|
|
||||||
mutateCurrentWorkspace: () => { },
|
|
||||||
pageContainerRef: createRef(),
|
|
||||||
langeniusVersionInfo: initialLangeniusVersionInfo,
|
|
||||||
useSelector,
|
|
||||||
})
|
|
||||||
|
|
||||||
export function useSelector<T>(selector: (value: AppContextValue) => T): T {
|
export function useSelector<T>(selector: (value: AppContextValue) => T): T {
|
||||||
return useContextSelector(AppContext, selector)
|
return useContextSelector(AppContext, selector)
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createContext, useContext } from 'use-context-selector'
|
|
||||||
import type { DataSet } from '@/models/datasets'
|
import type { DataSet } from '@/models/datasets'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
const DatasetDetailContext = createContext<{ indexingTechnique?: string; dataset?: DataSet; mutateDatasetRes?: () => void }>({})
|
const [, useDatasetDetailContext, DatasetDetailContext] = createSelectorCtx<{ indexingTechnique?: string; dataset?: DataSet; mutateDatasetRes?: () => void }>()
|
||||||
|
|
||||||
export const useDatasetDetailContext = () => useContext(DatasetDetailContext)
|
export { useDatasetDetailContext }
|
||||||
|
|
||||||
export default DatasetDetailContext
|
export default DatasetDetailContext
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { createContext, useContext } from 'use-context-selector'
|
|
||||||
import type { DataSet } from '@/models/datasets'
|
import type { DataSet } from '@/models/datasets'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export type DatasetsContextValue = {
|
export type DatasetsContextValue = {
|
||||||
datasets: DataSet[]
|
datasets: DataSet[]
|
||||||
@ -9,12 +9,8 @@ export type DatasetsContextValue = {
|
|||||||
currentDataset?: DataSet
|
currentDataset?: DataSet
|
||||||
}
|
}
|
||||||
|
|
||||||
const DatasetsContext = createContext<DatasetsContextValue>({
|
const [, useDatasetsContext, DatasetsContext] = createSelectorCtx<DatasetsContextValue>()
|
||||||
datasets: [],
|
|
||||||
mutateDatasets: () => {},
|
|
||||||
currentDataset: undefined,
|
|
||||||
})
|
|
||||||
|
|
||||||
export const useDatasetsContext = () => useContext(DatasetsContext)
|
export { useDatasetsContext }
|
||||||
|
|
||||||
export default DatasetsContext
|
export default DatasetsContext
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { createContext, useContext } from 'use-context-selector'
|
import type { PromptMode } from '@/models/debug'
|
||||||
import { PromptMode } from '@/models/debug'
|
|
||||||
import type {
|
import type {
|
||||||
AnnotationReplyConfig,
|
AnnotationReplyConfig,
|
||||||
BlockStatus,
|
BlockStatus,
|
||||||
@ -20,13 +19,12 @@ import type {
|
|||||||
} from '@/models/debug'
|
} from '@/models/debug'
|
||||||
import type { ExternalDataTool } from '@/models/common'
|
import type { ExternalDataTool } from '@/models/common'
|
||||||
import type { DataSet } from '@/models/datasets'
|
import type { DataSet } from '@/models/datasets'
|
||||||
import type { VisionSettings } from '@/types/app'
|
import type { ModelModeType, VisionSettings } from '@/types/app'
|
||||||
import { ModelModeType, RETRIEVE_TYPE, Resolution, TransferMethod } from '@/types/app'
|
|
||||||
import { ANNOTATION_DEFAULT, DEFAULT_AGENT_SETTING, DEFAULT_CHAT_PROMPT_CONFIG, DEFAULT_COMPLETION_PROMPT_CONFIG } from '@/config'
|
|
||||||
import type { FormValue } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
import type { FormValue } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||||
import type { Collection } from '@/app/components/tools/types'
|
import type { Collection } from '@/app/components/tools/types'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
interface IDebugConfiguration {
|
type IDebugConfiguration = {
|
||||||
appId: string
|
appId: string
|
||||||
isAPIKeySet: boolean
|
isAPIKeySet: boolean
|
||||||
isTrailFinished: boolean
|
isTrailFinished: boolean
|
||||||
@ -101,153 +99,8 @@ interface IDebugConfiguration {
|
|||||||
setRerankSettingModalOpen: (rerankSettingModalOpen: boolean) => void
|
setRerankSettingModalOpen: (rerankSettingModalOpen: boolean) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const DebugConfigurationContext = createContext<IDebugConfiguration>({
|
const [, useDebugConfigurationContext, DebugConfigurationContext] = createSelectorCtx<IDebugConfiguration>()
|
||||||
appId: '',
|
|
||||||
isAPIKeySet: false,
|
|
||||||
isTrailFinished: false,
|
|
||||||
mode: '',
|
|
||||||
modelModeType: ModelModeType.chat,
|
|
||||||
promptMode: PromptMode.simple,
|
|
||||||
setPromptMode: () => { },
|
|
||||||
isAdvancedMode: false,
|
|
||||||
isAgent: false,
|
|
||||||
isFunctionCall: false,
|
|
||||||
isOpenAI: false,
|
|
||||||
collectionList: [],
|
|
||||||
canReturnToSimpleMode: false,
|
|
||||||
setCanReturnToSimpleMode: () => { },
|
|
||||||
chatPromptConfig: DEFAULT_CHAT_PROMPT_CONFIG,
|
|
||||||
completionPromptConfig: DEFAULT_COMPLETION_PROMPT_CONFIG,
|
|
||||||
currentAdvancedPrompt: [],
|
|
||||||
showHistoryModal: () => { },
|
|
||||||
conversationHistoriesRole: {
|
|
||||||
user_prefix: 'user',
|
|
||||||
assistant_prefix: 'assistant',
|
|
||||||
},
|
|
||||||
setConversationHistoriesRole: () => { },
|
|
||||||
setCurrentAdvancedPrompt: () => { },
|
|
||||||
hasSetBlockStatus: {
|
|
||||||
context: false,
|
|
||||||
history: false,
|
|
||||||
query: false,
|
|
||||||
},
|
|
||||||
conversationId: '',
|
|
||||||
setConversationId: () => { },
|
|
||||||
introduction: '',
|
|
||||||
setIntroduction: () => { },
|
|
||||||
suggestedQuestions: [],
|
|
||||||
setSuggestedQuestions: () => { },
|
|
||||||
controlClearChatMessage: 0,
|
|
||||||
setControlClearChatMessage: () => { },
|
|
||||||
prevPromptConfig: {
|
|
||||||
prompt_template: '',
|
|
||||||
prompt_variables: [],
|
|
||||||
},
|
|
||||||
setPrevPromptConfig: () => { },
|
|
||||||
moreLikeThisConfig: {
|
|
||||||
enabled: false,
|
|
||||||
},
|
|
||||||
setMoreLikeThisConfig: () => { },
|
|
||||||
suggestedQuestionsAfterAnswerConfig: {
|
|
||||||
enabled: false,
|
|
||||||
},
|
|
||||||
setSuggestedQuestionsAfterAnswerConfig: () => { },
|
|
||||||
speechToTextConfig: {
|
|
||||||
enabled: false,
|
|
||||||
},
|
|
||||||
setSpeechToTextConfig: () => { },
|
|
||||||
textToSpeechConfig: {
|
|
||||||
enabled: false,
|
|
||||||
voice: '',
|
|
||||||
language: '',
|
|
||||||
},
|
|
||||||
setTextToSpeechConfig: () => { },
|
|
||||||
citationConfig: {
|
|
||||||
enabled: false,
|
|
||||||
},
|
|
||||||
setCitationConfig: () => { },
|
|
||||||
moderationConfig: {
|
|
||||||
enabled: false,
|
|
||||||
},
|
|
||||||
annotationConfig: {
|
|
||||||
id: '',
|
|
||||||
enabled: false,
|
|
||||||
score_threshold: ANNOTATION_DEFAULT.score_threshold,
|
|
||||||
embedding_model: {
|
|
||||||
embedding_model_name: '',
|
|
||||||
embedding_provider_name: '',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
setAnnotationConfig: () => { },
|
|
||||||
setModerationConfig: () => { },
|
|
||||||
externalDataToolsConfig: [],
|
|
||||||
setExternalDataToolsConfig: () => { },
|
|
||||||
formattingChanged: false,
|
|
||||||
setFormattingChanged: () => { },
|
|
||||||
inputs: {},
|
|
||||||
setInputs: () => { },
|
|
||||||
query: '',
|
|
||||||
setQuery: () => { },
|
|
||||||
completionParams: {
|
|
||||||
max_tokens: 16,
|
|
||||||
temperature: 1, // 0-2
|
|
||||||
top_p: 1,
|
|
||||||
presence_penalty: 1, // -2-2
|
|
||||||
frequency_penalty: 1, // -2-2
|
|
||||||
},
|
|
||||||
setCompletionParams: () => { },
|
|
||||||
modelConfig: {
|
|
||||||
provider: 'OPENAI', // 'OPENAI'
|
|
||||||
model_id: 'gpt-3.5-turbo', // 'gpt-3.5-turbo'
|
|
||||||
mode: ModelModeType.unset,
|
|
||||||
configs: {
|
|
||||||
prompt_template: '',
|
|
||||||
prompt_variables: [],
|
|
||||||
},
|
|
||||||
more_like_this: null,
|
|
||||||
opening_statement: '',
|
|
||||||
suggested_questions: [],
|
|
||||||
sensitive_word_avoidance: null,
|
|
||||||
speech_to_text: null,
|
|
||||||
text_to_speech: null,
|
|
||||||
file_upload: null,
|
|
||||||
suggested_questions_after_answer: null,
|
|
||||||
retriever_resource: null,
|
|
||||||
annotation_reply: null,
|
|
||||||
dataSets: [],
|
|
||||||
agentConfig: DEFAULT_AGENT_SETTING,
|
|
||||||
},
|
|
||||||
setModelConfig: () => { },
|
|
||||||
dataSets: [],
|
|
||||||
showSelectDataSet: () => { },
|
|
||||||
setDataSets: () => { },
|
|
||||||
datasetConfigs: {
|
|
||||||
retrieval_model: RETRIEVE_TYPE.multiWay,
|
|
||||||
reranking_model: {
|
|
||||||
reranking_provider_name: '',
|
|
||||||
reranking_model_name: '',
|
|
||||||
},
|
|
||||||
top_k: 2,
|
|
||||||
score_threshold_enabled: false,
|
|
||||||
score_threshold: 0.7,
|
|
||||||
datasets: {
|
|
||||||
datasets: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
setDatasetConfigs: () => { },
|
|
||||||
hasSetContextVar: false,
|
|
||||||
isShowVisionConfig: false,
|
|
||||||
visionConfig: {
|
|
||||||
enabled: false,
|
|
||||||
number_limits: 2,
|
|
||||||
detail: Resolution.low,
|
|
||||||
transfer_methods: [TransferMethod.remote_url],
|
|
||||||
},
|
|
||||||
setVisionConfig: () => { },
|
|
||||||
rerankSettingModalOpen: false,
|
|
||||||
setRerankSettingModalOpen: () => { },
|
|
||||||
})
|
|
||||||
|
|
||||||
export const useDebugConfigurationContext = () => useContext(DebugConfigurationContext)
|
export { useDebugConfigurationContext }
|
||||||
|
|
||||||
export default DebugConfigurationContext
|
export default DebugConfigurationContext
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { createContext, useContext } from 'use-context-selector'
|
|
||||||
import { useEventEmitter } from 'ahooks'
|
import { useEventEmitter } from 'ahooks'
|
||||||
import type { EventEmitter } from 'ahooks/lib/useEventEmitter'
|
import type { EventEmitter } from 'ahooks/lib/useEventEmitter'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
const EventEmitterContext = createContext<{ eventEmitter: EventEmitter<string> | null }>({
|
const [, useEventEmitterContextContext, EventEmitterContext] = createSelectorCtx<{ eventEmitter: EventEmitter<string> }>()
|
||||||
eventEmitter: null,
|
|
||||||
})
|
|
||||||
|
|
||||||
export const useEventEmitterContextContext = () => useContext(EventEmitterContext)
|
export{ useEventEmitterContextContext }
|
||||||
|
|
||||||
type EventEmitterContextProviderProps = {
|
type EventEmitterContextProviderProps = {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { createContext } from 'use-context-selector'
|
|
||||||
import type { InstalledApp } from '@/models/explore'
|
import type { InstalledApp } from '@/models/explore'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
type IExplore = {
|
type IExplore = {
|
||||||
controlUpdateInstalledApps: number
|
controlUpdateInstalledApps: number
|
||||||
@ -9,12 +9,6 @@ type IExplore = {
|
|||||||
setInstalledApps: (installedApps: InstalledApp[]) => void
|
setInstalledApps: (installedApps: InstalledApp[]) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const ExploreContext = createContext<IExplore>({
|
const [,, ExploreContext] = createSelectorCtx<IExplore>()
|
||||||
controlUpdateInstalledApps: 0,
|
|
||||||
setControlUpdateInstalledApps: () => { },
|
|
||||||
hasEditPermission: false,
|
|
||||||
installedApps: [],
|
|
||||||
setInstalledApps: () => { },
|
|
||||||
})
|
|
||||||
|
|
||||||
export default ExploreContext
|
export default ExploreContext
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import React, { createContext, useContext, useState } from 'react'
|
import { createCtx } from '@/utils/context'
|
||||||
|
import React, { useState } from 'react'
|
||||||
|
|
||||||
type ExternalApiPanelContextType = {
|
type ExternalApiPanelContextType = {
|
||||||
showExternalApiPanel: boolean
|
showExternalApiPanel: boolean
|
||||||
setShowExternalApiPanel: (show: boolean) => void
|
setShowExternalApiPanel: (show: boolean) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const ExternalApiPanelContext = createContext<ExternalApiPanelContextType | undefined>(undefined)
|
const [, useExternalApiPanel, ExternalApiPanelContext] = createCtx<ExternalApiPanelContextType>()
|
||||||
|
|
||||||
export const ExternalApiPanelProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
export const ExternalApiPanelProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||||
const [showExternalApiPanel, setShowExternalApiPanel] = useState(false)
|
const [showExternalApiPanel, setShowExternalApiPanel] = useState(false)
|
||||||
@ -19,10 +20,4 @@ export const ExternalApiPanelProvider: React.FC<{ children: React.ReactNode }> =
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useExternalApiPanel = () => {
|
export { useExternalApiPanel }
|
||||||
const context = useContext(ExternalApiPanelContext)
|
|
||||||
if (context === undefined)
|
|
||||||
throw new Error('useExternalApiPanel must be used within an ExternalApiPanelProvider')
|
|
||||||
|
|
||||||
return context
|
|
||||||
}
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { createContext, useContext, useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import type { FC, ReactNode } from 'react'
|
import type { FC, ReactNode } from 'react'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
import type { ExternalAPIItem, ExternalAPIListResponse } from '@/models/datasets'
|
import type { ExternalAPIItem, ExternalAPIListResponse } from '@/models/datasets'
|
||||||
import { fetchExternalAPIList } from '@/service/datasets'
|
import { fetchExternalAPIList } from '@/service/datasets'
|
||||||
|
import { createCtx } from '@/utils/context'
|
||||||
|
|
||||||
type ExternalKnowledgeApiContextType = {
|
type ExternalKnowledgeApiContextType = {
|
||||||
externalKnowledgeApiList: ExternalAPIItem[]
|
externalKnowledgeApiList: ExternalAPIItem[]
|
||||||
@ -12,7 +13,7 @@ type ExternalKnowledgeApiContextType = {
|
|||||||
isLoading: boolean
|
isLoading: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const ExternalKnowledgeApiContext = createContext<ExternalKnowledgeApiContextType | undefined>(undefined)
|
const [,useExternalKnowledgeApi, ExternalKnowledgeApiContext] = createCtx<ExternalKnowledgeApiContextType>()
|
||||||
|
|
||||||
export type ExternalKnowledgeApiProviderProps = {
|
export type ExternalKnowledgeApiProviderProps = {
|
||||||
children: ReactNode
|
children: ReactNode
|
||||||
@ -37,10 +38,4 @@ export const ExternalKnowledgeApiProvider: FC<ExternalKnowledgeApiProviderProps>
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useExternalKnowledgeApi = () => {
|
export { useExternalKnowledgeApi }
|
||||||
const context = useContext(ExternalKnowledgeApiContext)
|
|
||||||
if (context === undefined)
|
|
||||||
throw new Error('useExternalKnowledgeApi must be used within a ExternalKnowledgeApiProvider')
|
|
||||||
|
|
||||||
return context
|
|
||||||
}
|
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
import {
|
|
||||||
createContext,
|
|
||||||
useContext,
|
|
||||||
} from 'use-context-selector'
|
|
||||||
import type { Locale } from '@/i18n'
|
import type { Locale } from '@/i18n'
|
||||||
import { getLanguage } from '@/i18n/language'
|
import { getLanguage } from '@/i18n/language'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
type II18NContext = {
|
type II18NContext = {
|
||||||
locale: Locale
|
locale: Locale
|
||||||
@ -11,13 +8,9 @@ type II18NContext = {
|
|||||||
setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => void
|
setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const I18NContext = createContext<II18NContext>({
|
const [, useI18N, I18NContext] = createSelectorCtx<II18NContext>()
|
||||||
locale: 'en-US',
|
|
||||||
i18n: {},
|
|
||||||
setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => { },
|
|
||||||
})
|
|
||||||
|
|
||||||
export const useI18N = () => useContext(I18NContext)
|
export { useI18N }
|
||||||
export const useGetLanguage = () => {
|
export const useGetLanguage = () => {
|
||||||
const { locale } = useI18N()
|
const { locale } = useI18N()
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import type { Dispatch, SetStateAction } from 'react'
|
import type { Dispatch, SetStateAction } from 'react'
|
||||||
import { useCallback, useState } from 'react'
|
import { useCallback, useState } from 'react'
|
||||||
import { createContext, useContext, useContextSelector } from 'use-context-selector'
|
import { useContextSelector } from 'use-context-selector'
|
||||||
import { useRouter, useSearchParams } from 'next/navigation'
|
import { useRouter, useSearchParams } from 'next/navigation'
|
||||||
import AccountSetting from '@/app/components/header/account-setting'
|
import AccountSetting from '@/app/components/header/account-setting'
|
||||||
import ApiBasedExtensionModal from '@/app/components/header/account-setting/api-based-extension-page/modal'
|
import ApiBasedExtensionModal from '@/app/components/header/account-setting/api-based-extension-page/modal'
|
||||||
@ -33,6 +33,7 @@ import type { OpeningStatement } from '@/app/components/base/features/types'
|
|||||||
import type { InputVar } from '@/app/components/workflow/types'
|
import type { InputVar } from '@/app/components/workflow/types'
|
||||||
import type { UpdatePluginPayload } from '@/app/components/plugins/types'
|
import type { UpdatePluginPayload } from '@/app/components/plugins/types'
|
||||||
import UpdatePlugin from '@/app/components/plugins/update-plugin'
|
import UpdatePlugin from '@/app/components/plugins/update-plugin'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export type ModalState<T> = {
|
export type ModalState<T> = {
|
||||||
payload: T
|
payload: T
|
||||||
@ -73,22 +74,9 @@ export type ModalContextState = {
|
|||||||
}> | null>>
|
}> | null>>
|
||||||
setShowUpdatePluginModal: Dispatch<SetStateAction<ModalState<UpdatePluginPayload> | null>>
|
setShowUpdatePluginModal: Dispatch<SetStateAction<ModalState<UpdatePluginPayload> | null>>
|
||||||
}
|
}
|
||||||
const ModalContext = createContext<ModalContextState>({
|
const [,useModalContext, ModalContext] = createSelectorCtx<ModalContextState>()
|
||||||
setShowAccountSettingModal: () => { },
|
|
||||||
setShowApiBasedExtensionModal: () => { },
|
|
||||||
setShowModerationSettingModal: () => { },
|
|
||||||
setShowExternalDataToolModal: () => { },
|
|
||||||
setShowPricingModal: () => { },
|
|
||||||
setShowAnnotationFullModal: () => { },
|
|
||||||
setShowModelModal: () => { },
|
|
||||||
setShowExternalKnowledgeAPIModal: () => { },
|
|
||||||
setShowModelLoadBalancingModal: () => { },
|
|
||||||
setShowModelLoadBalancingEntryModal: () => { },
|
|
||||||
setShowOpeningModal: () => { },
|
|
||||||
setShowUpdatePluginModal: () => { },
|
|
||||||
})
|
|
||||||
|
|
||||||
export const useModalContext = () => useContext(ModalContext)
|
export { useModalContext }
|
||||||
|
|
||||||
// Adding a dangling comma to avoid the generic parsing issue in tsx, see:
|
// Adding a dangling comma to avoid the generic parsing issue in tsx, see:
|
||||||
// https://github.com/microsoft/TypeScript/issues/15713
|
// https://github.com/microsoft/TypeScript/issues/15713
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { createContext, useContext, useContextSelector } from 'use-context-selector'
|
import { useContextSelector } from 'use-context-selector'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import {
|
import {
|
||||||
@ -14,10 +14,12 @@ import {
|
|||||||
} from '@/app/components/header/account-setting/model-provider-page/declarations'
|
} from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||||
import type { Model, ModelProvider } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
import type { Model, ModelProvider } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||||
import type { RETRIEVE_METHOD } from '@/types/app'
|
import type { RETRIEVE_METHOD } from '@/types/app'
|
||||||
import { Plan, type UsagePlanInfo } from '@/app/components/billing/type'
|
import type { Plan } from '@/app/components/billing/type'
|
||||||
|
import type { UsagePlanInfo } from '@/app/components/billing/type'
|
||||||
import { fetchCurrentPlanInfo } from '@/service/billing'
|
import { fetchCurrentPlanInfo } from '@/service/billing'
|
||||||
import { parseCurrentPlan } from '@/app/components/billing/utils'
|
import { parseCurrentPlan } from '@/app/components/billing/utils'
|
||||||
import { defaultPlan } from '@/app/components/billing/config'
|
import { defaultPlan } from '@/app/components/billing/config'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
type ProviderContextState = {
|
type ProviderContextState = {
|
||||||
modelProviders: ModelProvider[]
|
modelProviders: ModelProvider[]
|
||||||
@ -36,37 +38,9 @@ type ProviderContextState = {
|
|||||||
modelLoadBalancingEnabled: boolean
|
modelLoadBalancingEnabled: boolean
|
||||||
datasetOperatorEnabled: boolean
|
datasetOperatorEnabled: boolean
|
||||||
}
|
}
|
||||||
const ProviderContext = createContext<ProviderContextState>({
|
const [, useProviderContext, ProviderContext] = createSelectorCtx<ProviderContextState>()
|
||||||
modelProviders: [],
|
|
||||||
textGenerationModelList: [],
|
|
||||||
supportRetrievalMethods: [],
|
|
||||||
isAPIKeySet: true,
|
|
||||||
plan: {
|
|
||||||
type: Plan.sandbox,
|
|
||||||
usage: {
|
|
||||||
vectorSpace: 32,
|
|
||||||
buildApps: 12,
|
|
||||||
teamMembers: 1,
|
|
||||||
annotatedResponse: 1,
|
|
||||||
documentsUploadQuota: 50,
|
|
||||||
},
|
|
||||||
total: {
|
|
||||||
vectorSpace: 200,
|
|
||||||
buildApps: 50,
|
|
||||||
teamMembers: 1,
|
|
||||||
annotatedResponse: 10,
|
|
||||||
documentsUploadQuota: 500,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
isFetchedPlan: false,
|
|
||||||
enableBilling: false,
|
|
||||||
onPlanInfoChanged: () => { },
|
|
||||||
enableReplaceWebAppLogo: false,
|
|
||||||
modelLoadBalancingEnabled: false,
|
|
||||||
datasetOperatorEnabled: false,
|
|
||||||
})
|
|
||||||
|
|
||||||
export const useProviderContext = () => useContext(ProviderContext)
|
export { useProviderContext }
|
||||||
|
|
||||||
// Adding a dangling comma to avoid the generic parsing issue in tsx, see:
|
// Adding a dangling comma to avoid the generic parsing issue in tsx, see:
|
||||||
// https://github.com/microsoft/TypeScript/issues/15713
|
// https://github.com/microsoft/TypeScript/issues/15713
|
||||||
|
@ -1,17 +1,15 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { createContext, useContext } from 'use-context-selector'
|
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
import { fetchWorkspaces } from '@/service/common'
|
import { fetchWorkspaces } from '@/service/common'
|
||||||
import type { IWorkspace } from '@/models/common'
|
import type { IWorkspace } from '@/models/common'
|
||||||
|
import { createSelectorCtx } from '@/utils/context'
|
||||||
|
|
||||||
export type WorkspacesContextValue = {
|
export type WorkspacesContextValue = {
|
||||||
workspaces: IWorkspace[]
|
workspaces: IWorkspace[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const WorkspacesContext = createContext<WorkspacesContextValue>({
|
const [, useWorkspacesContext, WorkspacesContext] = createSelectorCtx<WorkspacesContextValue>()
|
||||||
workspaces: [],
|
|
||||||
})
|
|
||||||
|
|
||||||
type IWorkspaceProviderProps = {
|
type IWorkspaceProviderProps = {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
@ -31,6 +29,6 @@ export const WorkspaceProvider = ({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useWorkspacesContext = () => useContext(WorkspacesContext)
|
export { useWorkspacesContext }
|
||||||
|
|
||||||
export default WorkspacesContext
|
export default WorkspacesContext
|
||||||
|
45
web/utils/context.ts
Normal file
45
web/utils/context.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import { type Context, type Provider, createContext, useContext } from 'react'
|
||||||
|
import * as selector from 'use-context-selector'
|
||||||
|
|
||||||
|
const createCreateCtxFunction = (
|
||||||
|
useContextImpl: typeof useContext,
|
||||||
|
createContextImpl: typeof createContext) => {
|
||||||
|
return function<T>({ name, defaultValue }: CreateCtxOptions<T> = {}): CreateCtxReturn<T> {
|
||||||
|
const emptySymbol = Symbol(`empty ${name}`)
|
||||||
|
// @ts-expect-error it's ok here
|
||||||
|
const context = createContextImpl<T>(defaultValue ?? emptySymbol)
|
||||||
|
const useContextValue = () => {
|
||||||
|
const ctx = useContextImpl(context)
|
||||||
|
if (ctx === emptySymbol)
|
||||||
|
throw new Error(`No ${name ?? 'related'} context found.`)
|
||||||
|
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
const result = [context.Provider, useContextValue, context] as CreateCtxReturn<T>
|
||||||
|
result.context = context
|
||||||
|
result.provider = context.Provider
|
||||||
|
result.useContextValue = useContextValue
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateCtxOptions<T> = {
|
||||||
|
defaultValue?: T
|
||||||
|
name?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateCtxReturn<T> = [Provider<T>, () => T, Context<T>] & {
|
||||||
|
context: Context<T>;
|
||||||
|
provider: Provider<T>;
|
||||||
|
useContextValue: () => T;
|
||||||
|
}
|
||||||
|
|
||||||
|
// example
|
||||||
|
// const [AppProvider, useApp, AppContext] = createCtx<AppContextValue>()
|
||||||
|
|
||||||
|
export const createCtx = createCreateCtxFunction(useContext, createContext)
|
||||||
|
|
||||||
|
export const createSelectorCtx = createCreateCtxFunction(
|
||||||
|
selector.useContext,
|
||||||
|
selector.createContext as typeof createContext,
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user