mirror of
https://github.com/langgenius/dify.git
synced 2025-12-20 14:42:37 +00:00
* test: adding some web tests (#27792) * feat: add validation to prevent saving empty opening statement in conversation opener modal (#27843) * fix(web): improve the consistency of the inputs-form UI (#27837) * fix(web): increase z-index of PortalToFollowElemContent (#27823) * fix: installation_id is missing when in tools page (#27849) * fix: avoid passing empty uniqueIdentifier to InstallFromMarketplace (#27802) Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * test: create new test scripts and update some existing test scripts o… (#27850) * feat: change feedback to forum (#27862) * chore: translate i18n files and update type definitions (#27868) Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> * Fix/template transformer line number (#27867) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> * bump vite to 6.4.1 (#27877) * Add WEAVIATE_GRPC_ENDPOINT as designed in weaviate migration guide (#27861) Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> * Fix: correct DraftWorkflowApi.post response model (#27289) Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> * fix Version 2.0.0-beta.2: Chat annotations Api Error #25506 (#27206) Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> * fix jina reader creadential migration command (#27883) * fix agent putout the output of workflow-tool twice (#26835) (#27087) * fix jina reader transform (#27922) * fix: prevent fetch version info in enterprise edition (#27923) * fix(api): fix `VariablePool.get` adding unexpected keys to variable_dictionary (#26767) Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * refactor: implement tenant self queue for rag tasks (#27559) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> * fix: bump brotli to 1.2.0 resloved CVE-2025-6176 (#27950) Signed-off-by: kenwoodjw <blackxin55+@gmail.com> --------- Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: aka James4u <smart.jamesjin@gmail.com> Co-authored-by: Novice <novice12185727@gmail.com> Co-authored-by: yangzheli <43645580+yangzheli@users.noreply.github.com> Co-authored-by: Elliott <105957288+Elliott-byte@users.noreply.github.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: johnny0120 <johnny0120@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Gritty_dev <101377478+codomposer@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: wangjifeng <163279492+kk-wangjifeng@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Boris Polonsky <BorisPolonsky@users.noreply.github.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Cursx <33718736+Cursx@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: red_sun <56100962+redSun64@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: kenwoodjw <blackxin55+@gmail.com>
189 lines
6.8 KiB
TypeScript
189 lines
6.8 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from 'react'
|
|
import useSWR from 'swr'
|
|
import { createContext, useContext, useContextSelector } from 'use-context-selector'
|
|
import type { FC, ReactNode } from 'react'
|
|
import { fetchCurrentWorkspace, fetchLangGeniusVersion, fetchUserProfile } from '@/service/common'
|
|
import type { ICurrentWorkspace, LangGeniusVersionResponse, UserProfileResponse } from '@/models/common'
|
|
import MaintenanceNotice from '@/app/components/header/maintenance-notice'
|
|
import { noop } from 'lodash-es'
|
|
import { setZendeskConversationFields } from '@/app/components/base/zendesk/utils'
|
|
import { ZENDESK_FIELD_IDS } from '@/config'
|
|
import { useGlobalPublicStore } from './global-public-context'
|
|
|
|
export type AppContextValue = {
|
|
userProfile: UserProfileResponse
|
|
mutateUserProfile: VoidFunction
|
|
currentWorkspace: ICurrentWorkspace
|
|
isCurrentWorkspaceManager: boolean
|
|
isCurrentWorkspaceOwner: boolean
|
|
isCurrentWorkspaceEditor: boolean
|
|
isCurrentWorkspaceDatasetOperator: boolean
|
|
mutateCurrentWorkspace: VoidFunction
|
|
langGeniusVersionInfo: LangGeniusVersionResponse
|
|
useSelector: typeof useSelector
|
|
isLoadingCurrentWorkspace: boolean
|
|
}
|
|
|
|
const userProfilePlaceholder = {
|
|
id: '',
|
|
name: '',
|
|
email: '',
|
|
avatar: '',
|
|
avatar_url: '',
|
|
is_password_set: false,
|
|
}
|
|
|
|
const initialLangGeniusVersionInfo = {
|
|
current_env: '',
|
|
current_version: '',
|
|
latest_version: '',
|
|
release_date: '',
|
|
release_notes: '',
|
|
version: '',
|
|
can_auto_update: false,
|
|
}
|
|
|
|
const initialWorkspaceInfo: ICurrentWorkspace = {
|
|
id: '',
|
|
name: '',
|
|
plan: '',
|
|
status: '',
|
|
created_at: 0,
|
|
role: 'normal',
|
|
providers: [],
|
|
}
|
|
|
|
const AppContext = createContext<AppContextValue>({
|
|
userProfile: userProfilePlaceholder,
|
|
currentWorkspace: initialWorkspaceInfo,
|
|
isCurrentWorkspaceManager: false,
|
|
isCurrentWorkspaceOwner: false,
|
|
isCurrentWorkspaceEditor: false,
|
|
isCurrentWorkspaceDatasetOperator: false,
|
|
mutateUserProfile: noop,
|
|
mutateCurrentWorkspace: noop,
|
|
langGeniusVersionInfo: initialLangGeniusVersionInfo,
|
|
useSelector,
|
|
isLoadingCurrentWorkspace: false,
|
|
})
|
|
|
|
export function useSelector<T>(selector: (value: AppContextValue) => T): T {
|
|
return useContextSelector(AppContext, selector)
|
|
}
|
|
|
|
export type AppContextProviderProps = {
|
|
children: ReactNode
|
|
}
|
|
|
|
export const AppContextProvider: FC<AppContextProviderProps> = ({ children }) => {
|
|
const systemFeatures = useGlobalPublicStore(s => s.systemFeatures)
|
|
const { data: userProfileResponse, mutate: mutateUserProfile, error: userProfileError } = useSWR({ url: '/account/profile', params: {} }, fetchUserProfile)
|
|
const { data: currentWorkspaceResponse, mutate: mutateCurrentWorkspace, isLoading: isLoadingCurrentWorkspace } = useSWR({ url: '/workspaces/current', params: {} }, fetchCurrentWorkspace)
|
|
|
|
const [userProfile, setUserProfile] = useState<UserProfileResponse>(userProfilePlaceholder)
|
|
const [langGeniusVersionInfo, setLangGeniusVersionInfo] = useState<LangGeniusVersionResponse>(initialLangGeniusVersionInfo)
|
|
const [currentWorkspace, setCurrentWorkspace] = useState<ICurrentWorkspace>(initialWorkspaceInfo)
|
|
const isCurrentWorkspaceManager = useMemo(() => ['owner', 'admin'].includes(currentWorkspace.role), [currentWorkspace.role])
|
|
const isCurrentWorkspaceOwner = useMemo(() => currentWorkspace.role === 'owner', [currentWorkspace.role])
|
|
const isCurrentWorkspaceEditor = useMemo(() => ['owner', 'admin', 'editor'].includes(currentWorkspace.role), [currentWorkspace.role])
|
|
const isCurrentWorkspaceDatasetOperator = useMemo(() => currentWorkspace.role === 'dataset_operator', [currentWorkspace.role])
|
|
const updateUserProfileAndVersion = useCallback(async () => {
|
|
if (userProfileResponse && !userProfileResponse.bodyUsed) {
|
|
try {
|
|
const result = await userProfileResponse.json()
|
|
setUserProfile(result)
|
|
if (!systemFeatures.branding.enabled) {
|
|
const current_version = userProfileResponse.headers.get('x-version')
|
|
const current_env = process.env.NODE_ENV === 'development' ? 'DEVELOPMENT' : userProfileResponse.headers.get('x-env')
|
|
const versionData = await fetchLangGeniusVersion({ url: '/version', params: { current_version } })
|
|
setLangGeniusVersionInfo({ ...versionData, current_version, latest_version: versionData.version, current_env })
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.error('Failed to update user profile:', error)
|
|
if (userProfile.id === '')
|
|
setUserProfile(userProfilePlaceholder)
|
|
}
|
|
}
|
|
else if (userProfileError && userProfile.id === '') {
|
|
setUserProfile(userProfilePlaceholder)
|
|
}
|
|
}, [userProfileResponse, userProfileError, userProfile.id])
|
|
|
|
useEffect(() => {
|
|
updateUserProfileAndVersion()
|
|
}, [updateUserProfileAndVersion, userProfileResponse])
|
|
|
|
useEffect(() => {
|
|
if (currentWorkspaceResponse)
|
|
setCurrentWorkspace(currentWorkspaceResponse)
|
|
}, [currentWorkspaceResponse])
|
|
|
|
// #region Zendesk conversation fields
|
|
useEffect(() => {
|
|
if (ZENDESK_FIELD_IDS.ENVIRONMENT && langGeniusVersionInfo?.current_env) {
|
|
setZendeskConversationFields([{
|
|
id: ZENDESK_FIELD_IDS.ENVIRONMENT,
|
|
value: langGeniusVersionInfo.current_env.toLowerCase(),
|
|
}])
|
|
}
|
|
}, [langGeniusVersionInfo?.current_env])
|
|
|
|
useEffect(() => {
|
|
if (ZENDESK_FIELD_IDS.VERSION && langGeniusVersionInfo?.version) {
|
|
setZendeskConversationFields([{
|
|
id: ZENDESK_FIELD_IDS.VERSION,
|
|
value: langGeniusVersionInfo.version,
|
|
}])
|
|
}
|
|
}, [langGeniusVersionInfo?.version])
|
|
|
|
useEffect(() => {
|
|
if (ZENDESK_FIELD_IDS.EMAIL && userProfile?.email) {
|
|
setZendeskConversationFields([{
|
|
id: ZENDESK_FIELD_IDS.EMAIL,
|
|
value: userProfile.email,
|
|
}])
|
|
}
|
|
}, [userProfile?.email])
|
|
|
|
useEffect(() => {
|
|
if (ZENDESK_FIELD_IDS.WORKSPACE_ID && currentWorkspace?.id) {
|
|
setZendeskConversationFields([{
|
|
id: ZENDESK_FIELD_IDS.WORKSPACE_ID,
|
|
value: currentWorkspace.id,
|
|
}])
|
|
}
|
|
}, [currentWorkspace?.id])
|
|
// #endregion Zendesk conversation fields
|
|
|
|
return (
|
|
<AppContext.Provider value={{
|
|
userProfile,
|
|
mutateUserProfile,
|
|
langGeniusVersionInfo,
|
|
useSelector,
|
|
currentWorkspace,
|
|
isCurrentWorkspaceManager,
|
|
isCurrentWorkspaceOwner,
|
|
isCurrentWorkspaceEditor,
|
|
isCurrentWorkspaceDatasetOperator,
|
|
mutateCurrentWorkspace,
|
|
isLoadingCurrentWorkspace,
|
|
}}>
|
|
<div className='flex h-full flex-col overflow-y-auto'>
|
|
{globalThis.document?.body?.getAttribute('data-public-maintenance-notice') && <MaintenanceNotice />}
|
|
<div className='relative flex grow flex-col overflow-y-auto overflow-x-hidden bg-background-body'>
|
|
{children}
|
|
</div>
|
|
</div>
|
|
</AppContext.Provider>
|
|
)
|
|
}
|
|
|
|
export const useAppContext = () => useContext(AppContext)
|
|
|
|
export default AppContext
|