-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce backend for pull request notifications (DAP-4753) (#23)
- Loading branch information
Showing
35 changed files
with
746 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function generateGuid() { | ||
return Array.from(crypto.getRandomValues(new Uint8Array(16))) | ||
.map((b) => b.toString(16).padStart(2, '0')) | ||
.join('') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 6 additions & 30 deletions
36
libs/engine/src/app/contexts/mutable-web-context/use-app-documents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,15 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { AppId } from '../../services/application/application.entity' | ||
import { Document } from '../../services/document/document.entity' | ||
import { useMutableWeb } from './use-mutable-web' | ||
import { useQueryArray } from '../../hooks/use-query-array' | ||
|
||
export const useAppDocuments = (appId: AppId) => { | ||
const { engine } = useMutableWeb() | ||
const [documents, setDocuments] = useState<Document[]>([]) | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [error, setError] = useState<string | null>(null) | ||
|
||
const loadDocuments = useCallback(async () => { | ||
try { | ||
setIsLoading(true) | ||
const { data, isLoading, error } = useQueryArray<Document>({ | ||
query: () => engine.documentService.getDocumentsByAppId(appId), | ||
deps: [engine, appId], | ||
}) | ||
|
||
const documents = await engine.documentService.getDocumentsByAppId(appId) | ||
setDocuments(documents) | ||
} catch (err) { | ||
console.error(err) | ||
if (err instanceof Error) { | ||
setError(err.message) | ||
} else { | ||
setError('Unknown error') | ||
} | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
}, [engine, appId]) | ||
|
||
useEffect(() => { | ||
loadDocuments() | ||
}, [loadDocuments]) | ||
|
||
return { | ||
documents, | ||
isLoading, | ||
error, | ||
} | ||
return { documents: data, isLoading, error } | ||
} |
37 changes: 6 additions & 31 deletions
37
libs/engine/src/app/contexts/mutable-web-context/use-applications.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,12 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { AppMetadata } from '../../services/application/application.entity' | ||
import { Engine } from '../../../engine' | ||
import { useQueryArray } from '../../hooks/use-query-array' | ||
|
||
export const useApplications = (engine: Engine) => { | ||
const [applications, setApplications] = useState<AppMetadata[]>([]) | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [error, setError] = useState<string | null>(null) | ||
const { data, isLoading, error } = useQueryArray<AppMetadata>({ | ||
query: () => engine.applicationService.getApplications(), | ||
deps: [engine], | ||
}) | ||
|
||
const loadApps = useCallback(async () => { | ||
try { | ||
setIsLoading(true) | ||
|
||
const applications = await engine.applicationService.getApplications() | ||
setApplications(applications) | ||
} catch (err) { | ||
console.error(err) | ||
if (err instanceof Error) { | ||
setError(err.message) | ||
} else { | ||
setError('Unknown error') | ||
} | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
}, [engine]) | ||
|
||
useEffect(() => { | ||
loadApps() | ||
}, [loadApps]) | ||
|
||
return { | ||
applications, | ||
isLoading, | ||
error, | ||
} | ||
return { applications: data, isLoading, error } | ||
} |
44 changes: 6 additions & 38 deletions
44
libs/engine/src/app/contexts/mutable-web-context/use-mutation-apps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,13 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { AppInstanceWithSettings } from '../../services/application/application.entity' | ||
import { Mutation } from '../../services/mutation/mutation.entity' | ||
import { Engine } from '../../../engine' | ||
import { useQueryArray } from '../../hooks/use-query-array' | ||
|
||
export const useMutationApps = (engine: Engine, mutation?: Mutation | null) => { | ||
const [mutationApps, setMutationApps] = useState<AppInstanceWithSettings[]>([]) | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [error, setError] = useState<string | null>(null) | ||
const { data, setData, isLoading, error } = useQueryArray<AppInstanceWithSettings>({ | ||
query: async () => (mutation ? engine.applicationService.getAppsFromMutation(mutation) : []), | ||
deps: [engine, mutation], | ||
}) | ||
|
||
const loadMutationApps = useCallback(async () => { | ||
if (!mutation) { | ||
setMutationApps([]) | ||
return | ||
} | ||
|
||
try { | ||
setIsLoading(true) | ||
|
||
const apps = await engine.applicationService.getAppsFromMutation(mutation) | ||
|
||
setMutationApps(apps) | ||
} catch (err) { | ||
console.error(err) | ||
if (err instanceof Error) { | ||
setError(err.message) | ||
} else { | ||
setError('Unknown error') | ||
} | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
}, [engine, mutation]) | ||
|
||
useEffect(() => { | ||
loadMutationApps() | ||
}, [loadMutationApps]) | ||
|
||
return { | ||
mutationApps, | ||
setMutationApps, | ||
isLoading, | ||
error, | ||
} | ||
return { mutationApps: data, setMutationApps: setData, isLoading, error } | ||
} |
38 changes: 6 additions & 32 deletions
38
libs/engine/src/app/contexts/mutable-web-context/use-mutation-parsers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,13 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { AppMetadata } from '../../services/application/application.entity' | ||
import { ParserConfig } from '../../services/parser-config/parser-config.entity' | ||
import { Engine } from '../../../engine' | ||
import { useQueryArray } from '../../hooks/use-query-array' | ||
|
||
export const useMutationParsers = (engine: Engine, apps: AppMetadata[]) => { | ||
const [parserConfigs, setParserConfigs] = useState<ParserConfig[]>([]) | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [error, setError] = useState<string | null>(null) | ||
const { data, isLoading, error } = useQueryArray<ParserConfig>({ | ||
query: () => engine.parserConfigService.getParserConfigsForApps(apps), | ||
deps: [engine, apps], | ||
}) | ||
|
||
const loadMutationParsers = useCallback(async () => { | ||
try { | ||
setIsLoading(true) | ||
|
||
const parserConfigs = await engine.parserConfigService.getParserConfigsForApps(apps) | ||
|
||
setParserConfigs(parserConfigs) | ||
} catch (err) { | ||
console.error(err) | ||
if (err instanceof Error) { | ||
setError(err.message) | ||
} else { | ||
setError('Unknown error') | ||
} | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
}, [engine, apps]) | ||
|
||
useEffect(() => { | ||
loadMutationParsers() | ||
}, [loadMutationParsers]) | ||
|
||
return { | ||
parserConfigs, | ||
isLoading, | ||
error, | ||
} | ||
return { parserConfigs: data, isLoading, error } | ||
} |
39 changes: 6 additions & 33 deletions
39
libs/engine/src/app/contexts/mutable-web-context/use-mutations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,15 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { useCore } from '@mweb/react' | ||
import { MutationWithSettings } from '../../services/mutation/mutation.entity' | ||
import { Engine } from '../../../engine' | ||
import { useQueryArray } from '../../hooks/use-query-array' | ||
|
||
export const useMutations = (engine: Engine) => { | ||
const { core } = useCore() | ||
const [mutations, setMutations] = useState<MutationWithSettings[]>([]) | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [error, setError] = useState<string | null>(null) | ||
|
||
const loadMutations = useCallback(async () => { | ||
if (!engine) return | ||
const { data, setData, isLoading, error } = useQueryArray<MutationWithSettings>({ | ||
query: () => engine.mutationService.getMutationsWithSettings(core.tree), | ||
deps: [engine, core], | ||
}) | ||
|
||
try { | ||
setIsLoading(true) | ||
|
||
const mutations = await engine.mutationService.getMutationsWithSettings(core.tree) | ||
setMutations(mutations) | ||
} catch (err) { | ||
console.log(err) | ||
if (err instanceof Error) { | ||
setError(err.message) | ||
} else { | ||
setError('Unknown error') | ||
} | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
}, [engine, core]) | ||
|
||
useEffect(() => { | ||
loadMutations() | ||
}, [loadMutations]) | ||
|
||
return { | ||
mutations, | ||
setMutations, | ||
isLoading, | ||
error, | ||
} | ||
return { mutations: data, setMutations: setData, isLoading, error } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { NotificationContext } from './notification-context' | ||
export { NotificationProvider } from './notification-provider' | ||
export { useNotifications } from './use-notifications' | ||
export { useViewNotification } from './use-view-notification' | ||
export { useHideNotification } from './use-hide-notification' | ||
export { useAcceptPullRequest } from './use-accept-pull-request' | ||
export { useRejectPullRequest } from './use-reject-pull-request' |
18 changes: 18 additions & 0 deletions
18
libs/engine/src/app/contexts/notification-context/notification-context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createContext } from 'react' | ||
import { NotificationDto } from '../../services/notification/dtos/notification.dto' | ||
|
||
export type NotificationContextState = { | ||
notifications: NotificationDto[] | ||
setNotifications: React.Dispatch<React.SetStateAction<NotificationDto[]>> | ||
isLoading: boolean | ||
error: string | null | ||
} | ||
|
||
export const contextDefaultValues: NotificationContextState = { | ||
notifications: [], | ||
setNotifications: () => {}, | ||
isLoading: false, | ||
error: null, | ||
} | ||
|
||
export const NotificationContext = createContext<NotificationContextState>(contextDefaultValues) |
41 changes: 41 additions & 0 deletions
41
libs/engine/src/app/contexts/notification-context/notification-provider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { FC, ReactNode, useCallback, useEffect, useRef } from 'react' | ||
import { NotificationContext, NotificationContextState } from './notification-context' | ||
import { Button, Space, notification } from 'antd' | ||
import { useViewport } from '../viewport-context' | ||
import { useQueryArray } from '../../hooks/use-query-array' | ||
import { NotificationDto } from '../../services/notification/dtos/notification.dto' | ||
import { useMutableWeb } from '../mutable-web-context' | ||
|
||
type Props = { | ||
children?: ReactNode | ||
recipientId: string | ||
} | ||
|
||
const NotificationProvider: FC<Props> = ({ children, recipientId }) => { | ||
const { engine } = useMutableWeb() | ||
|
||
const { | ||
data: notifications, | ||
setData: setNotifications, | ||
isLoading, | ||
error, | ||
} = useQueryArray<NotificationDto>({ | ||
query: () => engine.notificationService.getNotificationsByRecipient(recipientId), | ||
deps: [engine, recipientId], | ||
}) | ||
|
||
const state: NotificationContextState = { | ||
notifications, | ||
setNotifications, | ||
isLoading, | ||
error, | ||
} | ||
|
||
return ( | ||
<NotificationContext.Provider value={state}> | ||
<>{children}</> | ||
</NotificationContext.Provider> | ||
) | ||
} | ||
|
||
export { NotificationProvider } |
25 changes: 25 additions & 0 deletions
25
libs/engine/src/app/contexts/notification-context/use-accept-pull-request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { usePromise } from '../../hooks/use-promise' | ||
import { EntityId } from '../../services/base/base.entity' | ||
import { useMutableWeb } from '../mutable-web-context' | ||
import { useNotifications } from './use-notifications' | ||
|
||
export const useAcceptPullRequest = (notificationId: EntityId) => { | ||
const { engine } = useMutableWeb() | ||
const { setNotifications } = useNotifications() | ||
|
||
const { fetch, isLoading, error } = usePromise({ | ||
query: async () => { | ||
const notification = await engine.mutationService.acceptPullRequest(notificationId) | ||
setNotifications((items) => | ||
items.map((item) => (item.id === notification.id ? notification : item)) | ||
) | ||
}, | ||
deps: [engine, notificationId], | ||
}) | ||
|
||
return { | ||
acceptPullRequest: fetch, | ||
isLoading, | ||
error, | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
libs/engine/src/app/contexts/notification-context/use-hide-notification.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { usePromise } from '../../hooks/use-promise' | ||
import { EntityId } from '../../services/base/base.entity' | ||
import { useMutableWeb } from '../mutable-web-context' | ||
import { useNotifications } from './use-notifications' | ||
|
||
export const useHideNotification = (notificationId: EntityId) => { | ||
const { engine } = useMutableWeb() | ||
const { setNotifications } = useNotifications() | ||
|
||
const { fetch, isLoading, error } = usePromise({ | ||
query: async () => { | ||
const notification = await engine.notificationService.hideNotification(notificationId) | ||
setNotifications((items) => | ||
items.map((item) => (item.id === notification.id ? notification : item)) | ||
) | ||
}, | ||
deps: [engine, notificationId], | ||
}) | ||
|
||
return { | ||
hideNotification: fetch, | ||
isLoading, | ||
error, | ||
} | ||
} |
Oops, something went wrong.