Skip to content

Commit 1e59486

Browse files
committed
feat: introduce backend for pull request notifications (DAP-4753) (#23)
1 parent e575a2a commit 1e59486

35 files changed

+746
-192
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function generateGuid() {
2+
return Array.from(crypto.getRandomValues(new Uint8Array(16)))
3+
.map((b) => b.toString(16).padStart(2, '0'))
4+
.join('')
5+
}

libs/engine/src/app/components/context-manager.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { Target } from '../services/target/target.entity'
2929
import { filterAndDiscriminate } from '../common/filter-and-discriminate'
3030
import { Document, DocumentId, DocumentMetadata } from '../services/document/document.entity'
3131
import { ApplicationService } from '../services/application/application.service'
32+
import { DocumentDto } from '../services/document/dtos/document.dto'
3233

3334
interface WidgetProps {
3435
context: TransferableContext
@@ -56,7 +57,7 @@ interface WidgetProps {
5657
ctx: TransferableContext,
5758
dataByAccount: LinkedDataByAccountDto
5859
) => Promise<void>
59-
getDocument: () => Promise<Document | null>
60+
getDocument: () => Promise<DocumentDto | null>
6061
}
6162

6263
interface LayoutManagerProps {
@@ -387,7 +388,7 @@ const InsPointHandler: FC<{
387388
ctx: TransferableContext,
388389
dataByAccount: LinkedDataByAccountDto
389390
) => Promise<void>
390-
onGetDocumentCurry: (appInstanceId: string) => () => Promise<Document | null>
391+
onGetDocumentCurry: (appInstanceId: string) => () => Promise<DocumentDto | null>
391392
}> = ({
392393
insPointName,
393394
element,
@@ -560,7 +561,7 @@ const ControllerHandler: FC<{
560561
ctx: TransferableContext,
561562
dataByAccount: LinkedDataByAccountDto
562563
) => Promise<void>
563-
onGetDocumentCurry: (appInstanceId: string) => () => Promise<Document | null>
564+
onGetDocumentCurry: (appInstanceId: string) => () => Promise<DocumentDto | null>
564565
}> = ({
565566
transferableContext,
566567
controller,
Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,15 @@
1-
import { useCallback, useEffect, useState } from 'react'
21
import { AppId } from '../../services/application/application.entity'
32
import { Document } from '../../services/document/document.entity'
43
import { useMutableWeb } from './use-mutable-web'
4+
import { useQueryArray } from '../../hooks/use-query-array'
55

66
export const useAppDocuments = (appId: AppId) => {
77
const { engine } = useMutableWeb()
8-
const [documents, setDocuments] = useState<Document[]>([])
9-
const [isLoading, setIsLoading] = useState(false)
10-
const [error, setError] = useState<string | null>(null)
118

12-
const loadDocuments = useCallback(async () => {
13-
try {
14-
setIsLoading(true)
9+
const { data, isLoading, error } = useQueryArray<Document>({
10+
query: () => engine.documentService.getDocumentsByAppId(appId),
11+
deps: [engine, appId],
12+
})
1513

16-
const documents = await engine.documentService.getDocumentsByAppId(appId)
17-
setDocuments(documents)
18-
} catch (err) {
19-
console.error(err)
20-
if (err instanceof Error) {
21-
setError(err.message)
22-
} else {
23-
setError('Unknown error')
24-
}
25-
} finally {
26-
setIsLoading(false)
27-
}
28-
}, [engine, appId])
29-
30-
useEffect(() => {
31-
loadDocuments()
32-
}, [loadDocuments])
33-
34-
return {
35-
documents,
36-
isLoading,
37-
error,
38-
}
14+
return { documents: data, isLoading, error }
3915
}
Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,12 @@
1-
import { useCallback, useEffect, useState } from 'react'
21
import { AppMetadata } from '../../services/application/application.entity'
32
import { Engine } from '../../../engine'
3+
import { useQueryArray } from '../../hooks/use-query-array'
44

55
export const useApplications = (engine: Engine) => {
6-
const [applications, setApplications] = useState<AppMetadata[]>([])
7-
const [isLoading, setIsLoading] = useState(false)
8-
const [error, setError] = useState<string | null>(null)
6+
const { data, isLoading, error } = useQueryArray<AppMetadata>({
7+
query: () => engine.applicationService.getApplications(),
8+
deps: [engine],
9+
})
910

10-
const loadApps = useCallback(async () => {
11-
try {
12-
setIsLoading(true)
13-
14-
const applications = await engine.applicationService.getApplications()
15-
setApplications(applications)
16-
} catch (err) {
17-
console.error(err)
18-
if (err instanceof Error) {
19-
setError(err.message)
20-
} else {
21-
setError('Unknown error')
22-
}
23-
} finally {
24-
setIsLoading(false)
25-
}
26-
}, [engine])
27-
28-
useEffect(() => {
29-
loadApps()
30-
}, [loadApps])
31-
32-
return {
33-
applications,
34-
isLoading,
35-
error,
36-
}
11+
return { applications: data, isLoading, error }
3712
}
Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,13 @@
1-
import { useCallback, useEffect, useState } from 'react'
21
import { AppInstanceWithSettings } from '../../services/application/application.entity'
32
import { Mutation } from '../../services/mutation/mutation.entity'
43
import { Engine } from '../../../engine'
4+
import { useQueryArray } from '../../hooks/use-query-array'
55

66
export const useMutationApps = (engine: Engine, mutation?: Mutation | null) => {
7-
const [mutationApps, setMutationApps] = useState<AppInstanceWithSettings[]>([])
8-
const [isLoading, setIsLoading] = useState(false)
9-
const [error, setError] = useState<string | null>(null)
7+
const { data, setData, isLoading, error } = useQueryArray<AppInstanceWithSettings>({
8+
query: async () => (mutation ? engine.applicationService.getAppsFromMutation(mutation) : []),
9+
deps: [engine, mutation],
10+
})
1011

11-
const loadMutationApps = useCallback(async () => {
12-
if (!mutation) {
13-
setMutationApps([])
14-
return
15-
}
16-
17-
try {
18-
setIsLoading(true)
19-
20-
const apps = await engine.applicationService.getAppsFromMutation(mutation)
21-
22-
setMutationApps(apps)
23-
} catch (err) {
24-
console.error(err)
25-
if (err instanceof Error) {
26-
setError(err.message)
27-
} else {
28-
setError('Unknown error')
29-
}
30-
} finally {
31-
setIsLoading(false)
32-
}
33-
}, [engine, mutation])
34-
35-
useEffect(() => {
36-
loadMutationApps()
37-
}, [loadMutationApps])
38-
39-
return {
40-
mutationApps,
41-
setMutationApps,
42-
isLoading,
43-
error,
44-
}
12+
return { mutationApps: data, setMutationApps: setData, isLoading, error }
4513
}
Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,13 @@
1-
import { useCallback, useEffect, useState } from 'react'
21
import { AppMetadata } from '../../services/application/application.entity'
32
import { ParserConfig } from '../../services/parser-config/parser-config.entity'
43
import { Engine } from '../../../engine'
4+
import { useQueryArray } from '../../hooks/use-query-array'
55

66
export const useMutationParsers = (engine: Engine, apps: AppMetadata[]) => {
7-
const [parserConfigs, setParserConfigs] = useState<ParserConfig[]>([])
8-
const [isLoading, setIsLoading] = useState(false)
9-
const [error, setError] = useState<string | null>(null)
7+
const { data, isLoading, error } = useQueryArray<ParserConfig>({
8+
query: () => engine.parserConfigService.getParserConfigsForApps(apps),
9+
deps: [engine, apps],
10+
})
1011

11-
const loadMutationParsers = useCallback(async () => {
12-
try {
13-
setIsLoading(true)
14-
15-
const parserConfigs = await engine.parserConfigService.getParserConfigsForApps(apps)
16-
17-
setParserConfigs(parserConfigs)
18-
} catch (err) {
19-
console.error(err)
20-
if (err instanceof Error) {
21-
setError(err.message)
22-
} else {
23-
setError('Unknown error')
24-
}
25-
} finally {
26-
setIsLoading(false)
27-
}
28-
}, [engine, apps])
29-
30-
useEffect(() => {
31-
loadMutationParsers()
32-
}, [loadMutationParsers])
33-
34-
return {
35-
parserConfigs,
36-
isLoading,
37-
error,
38-
}
12+
return { parserConfigs: data, isLoading, error }
3913
}
Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
1-
import { useCallback, useEffect, useState } from 'react'
21
import { useCore } from '@mweb/react'
32
import { MutationWithSettings } from '../../services/mutation/mutation.entity'
43
import { Engine } from '../../../engine'
4+
import { useQueryArray } from '../../hooks/use-query-array'
55

66
export const useMutations = (engine: Engine) => {
77
const { core } = useCore()
8-
const [mutations, setMutations] = useState<MutationWithSettings[]>([])
9-
const [isLoading, setIsLoading] = useState(false)
10-
const [error, setError] = useState<string | null>(null)
118

12-
const loadMutations = useCallback(async () => {
13-
if (!engine) return
9+
const { data, setData, isLoading, error } = useQueryArray<MutationWithSettings>({
10+
query: () => engine.mutationService.getMutationsWithSettings(core.tree),
11+
deps: [engine, core],
12+
})
1413

15-
try {
16-
setIsLoading(true)
17-
18-
const mutations = await engine.mutationService.getMutationsWithSettings(core.tree)
19-
setMutations(mutations)
20-
} catch (err) {
21-
console.log(err)
22-
if (err instanceof Error) {
23-
setError(err.message)
24-
} else {
25-
setError('Unknown error')
26-
}
27-
} finally {
28-
setIsLoading(false)
29-
}
30-
}, [engine, core])
31-
32-
useEffect(() => {
33-
loadMutations()
34-
}, [loadMutations])
35-
36-
return {
37-
mutations,
38-
setMutations,
39-
isLoading,
40-
error,
41-
}
14+
return { mutations: data, setMutations: setData, isLoading, error }
4215
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export { NotificationContext } from './notification-context'
2+
export { NotificationProvider } from './notification-provider'
3+
export { useNotifications } from './use-notifications'
4+
export { useViewNotification } from './use-view-notification'
5+
export { useHideNotification } from './use-hide-notification'
6+
export { useAcceptPullRequest } from './use-accept-pull-request'
7+
export { useRejectPullRequest } from './use-reject-pull-request'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createContext } from 'react'
2+
import { NotificationDto } from '../../services/notification/dtos/notification.dto'
3+
4+
export type NotificationContextState = {
5+
notifications: NotificationDto[]
6+
setNotifications: React.Dispatch<React.SetStateAction<NotificationDto[]>>
7+
isLoading: boolean
8+
error: string | null
9+
}
10+
11+
export const contextDefaultValues: NotificationContextState = {
12+
notifications: [],
13+
setNotifications: () => {},
14+
isLoading: false,
15+
error: null,
16+
}
17+
18+
export const NotificationContext = createContext<NotificationContextState>(contextDefaultValues)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React, { FC, ReactNode, useCallback, useEffect, useRef } from 'react'
2+
import { NotificationContext, NotificationContextState } from './notification-context'
3+
import { Button, Space, notification } from 'antd'
4+
import { useViewport } from '../viewport-context'
5+
import { useQueryArray } from '../../hooks/use-query-array'
6+
import { NotificationDto } from '../../services/notification/dtos/notification.dto'
7+
import { useMutableWeb } from '../mutable-web-context'
8+
9+
type Props = {
10+
children?: ReactNode
11+
recipientId: string
12+
}
13+
14+
const NotificationProvider: FC<Props> = ({ children, recipientId }) => {
15+
const { engine } = useMutableWeb()
16+
17+
const {
18+
data: notifications,
19+
setData: setNotifications,
20+
isLoading,
21+
error,
22+
} = useQueryArray<NotificationDto>({
23+
query: () => engine.notificationService.getNotificationsByRecipient(recipientId),
24+
deps: [engine, recipientId],
25+
})
26+
27+
const state: NotificationContextState = {
28+
notifications,
29+
setNotifications,
30+
isLoading,
31+
error,
32+
}
33+
34+
return (
35+
<NotificationContext.Provider value={state}>
36+
<>{children}</>
37+
</NotificationContext.Provider>
38+
)
39+
}
40+
41+
export { NotificationProvider }

0 commit comments

Comments
 (0)