Streamline the project metadata context provider (#19384)
GitOrigin-RevId: 0b75635cb9141983827dfd0fa6a58b6182d47f22
This commit is contained in:
@@ -11,7 +11,6 @@ import {
|
||||
import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
|
||||
import { useConnectionContext } from '@/features/ide-react/context/connection-context'
|
||||
import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context'
|
||||
import _ from 'lodash'
|
||||
import { getJSON, postJSON } from '@/infrastructure/fetch-json'
|
||||
import { useOnlineUsersContext } from '@/features/ide-react/context/online-users-context'
|
||||
import { useEditorContext } from '@/shared/context/editor-context'
|
||||
@@ -22,28 +21,31 @@ import { usePermissionsContext } from '@/features/ide-react/context/permissions-
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { IdeEvents } from '@/features/ide-react/create-ide-event-emitter'
|
||||
|
||||
type DocumentMetadata = {
|
||||
export type Command = {
|
||||
caption: string
|
||||
snippet: string
|
||||
meta: string
|
||||
score: number
|
||||
}
|
||||
|
||||
export type DocumentMetadata = {
|
||||
labels: string[]
|
||||
packages: Record<string, any>
|
||||
packages: Record<string, Command[]>
|
||||
packageNames: string[]
|
||||
}
|
||||
|
||||
type DocumentsMetadata = Record<string, DocumentMetadata>
|
||||
|
||||
type MetadataContextValue = {
|
||||
metadata: {
|
||||
state: {
|
||||
documents: DocumentsMetadata
|
||||
}
|
||||
getAllLabels: () => DocumentMetadata['labels']
|
||||
getAllPackages: () => DocumentMetadata['packages']
|
||||
}
|
||||
}
|
||||
|
||||
type DocMetadataResponse = { docId: string; meta: DocumentMetadata }
|
||||
|
||||
export const MetadataContext = createContext<MetadataContextValue | undefined>(
|
||||
undefined
|
||||
)
|
||||
export const MetadataContext = createContext<
|
||||
| {
|
||||
commands: Command[]
|
||||
labels: Set<string>
|
||||
packageNames: Set<string>
|
||||
}
|
||||
| undefined
|
||||
>(undefined)
|
||||
|
||||
export const MetadataProvider: FC = ({ children }) => {
|
||||
const { t } = useTranslation()
|
||||
@@ -65,7 +67,8 @@ export const MetadataProvider: FC = ({ children }) => {
|
||||
}: CustomEvent<IdeEvents['entity:deleted']>) => {
|
||||
if (entity.type === 'doc') {
|
||||
setDocuments(documents => {
|
||||
return _.omit(documents, entity.entity._id)
|
||||
delete documents[entity.entity._id]
|
||||
return { ...documents }
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -90,23 +93,6 @@ export const MetadataProvider: FC = ({ children }) => {
|
||||
}
|
||||
}, [])
|
||||
|
||||
const getAllLabels = useCallback(
|
||||
() => _.flattenDeep(Object.values(documents).map(meta => meta.labels)),
|
||||
[documents]
|
||||
)
|
||||
|
||||
const getAllPackages = useCallback(() => {
|
||||
const packageCommandMapping: Record<string, any> = {}
|
||||
for (const meta of Object.values(documents)) {
|
||||
for (const [packageName, commandSnippets] of Object.entries(
|
||||
meta.packages
|
||||
)) {
|
||||
packageCommandMapping[packageName] = commandSnippets
|
||||
}
|
||||
}
|
||||
return packageCommandMapping
|
||||
}, [documents])
|
||||
|
||||
const loadProjectMetaFromServer = useCallback(() => {
|
||||
getJSON(`/project/${projectId}/metadata`).then(
|
||||
(response: { projectMeta: DocumentsMetadata }) => {
|
||||
@@ -212,16 +198,15 @@ export const MetadataProvider: FC = ({ children }) => {
|
||||
}
|
||||
}, [eventEmitter, loadProjectMetaFromServer, showGenericMessageModal, t])
|
||||
|
||||
const value = useMemo<MetadataContextValue>(
|
||||
() => ({
|
||||
metadata: {
|
||||
state: { documents },
|
||||
getAllLabels,
|
||||
getAllPackages,
|
||||
},
|
||||
}),
|
||||
[documents, getAllLabels, getAllPackages]
|
||||
)
|
||||
const value = useMemo(() => {
|
||||
const docs = Object.values(documents)
|
||||
|
||||
return {
|
||||
commands: docs.flatMap(doc => Object.values(doc.packages).flat()),
|
||||
labels: new Set(docs.flatMap(doc => doc.labels)),
|
||||
packageNames: new Set(docs.flatMap(doc => doc.packageNames)),
|
||||
}
|
||||
}, [documents])
|
||||
|
||||
return (
|
||||
<MetadataContext.Provider value={value}>
|
||||
@@ -230,7 +215,7 @@ export const MetadataProvider: FC = ({ children }) => {
|
||||
)
|
||||
}
|
||||
|
||||
export function useMetadataContext(): MetadataContextValue {
|
||||
export function useMetadataContext() {
|
||||
const context = useContext(MetadataContext)
|
||||
|
||||
if (!context) {
|
||||
|
||||
Reference in New Issue
Block a user