diff --git a/apps/dfe-core-ui/e2e/specs/Sources/Ingest/ingestRules.spec.ts b/apps/dfe-core-ui/e2e/specs/Sources/Ingest/ingestRules.spec.ts index 9c97efe2d..13c664848 100644 --- a/apps/dfe-core-ui/e2e/specs/Sources/Ingest/ingestRules.spec.ts +++ b/apps/dfe-core-ui/e2e/specs/Sources/Ingest/ingestRules.spec.ts @@ -184,7 +184,7 @@ test.describe('', { tag: [TAGS.core, TAGS.source] }, () => { page, request, }) => { - await page.goto(`${BASE_URL}/sources/ingest-rules`); + await page.goto(`${BASE_URL}/sources/ingest-procedures`); const sourceRules = page.getByRole('list', { name: 'Source rules order' }); await expect( sourceRules.getByTestId(`rule-block-source.${COMPOUND}`), diff --git a/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/BlockRow.tsx b/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/BlockRow.tsx new file mode 100644 index 000000000..d54a739e7 --- /dev/null +++ b/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/BlockRow.tsx @@ -0,0 +1,72 @@ +import { Tooltip } from '@/core/components/Tooltip'; +import { TRuleBlock } from '@/Sources/hooks/useFetchRuleBlocks/types'; +import { IconAlertTriangle } from '@repo/dfe-icons'; +import { Tag } from 'antd'; +import Link from 'next/link'; +import { KIND_LABEL } from './RuleOrderList.helpers'; + +export const BlockRow = ({ + block, + title, +}: { + block: TRuleBlock; + title: string; +}) => { + const rows = block.rules.length; + const warnings = block.warnings ?? []; + return ( +
+ {block.kind === 'source' ? ( + + {title} + + ) : ( + {title} + )} + {block.kind !== 'default' && ( + + {KIND_LABEL[block.kind]} + + )} + {block.version && ( + + {`version ${block.version}`} + + )} + {!block.compiled && ( + + Not in the receiver's rules + + )} + {warnings.length > 0 && ( + + {warnings.map((warning) => ( +
  • {warning}
  • + ))} + + } + > + 1 ? 's' : ''}: ${warnings.join(' ')}`} + className="flex text-warning" + > + + +
    + )} + + {block.kind === 'default' + ? 'Takes what no block above matched' + : `${rows} rule row${rows === 1 ? '' : 's'}`} + +
    + ); +}; diff --git a/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/RbacOrderList.tsx b/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/RbacOrderList.tsx new file mode 100644 index 000000000..af9f810eb --- /dev/null +++ b/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/RbacOrderList.tsx @@ -0,0 +1,35 @@ +import { RbacProtected } from '@/core/components/RbacProtected'; +import { SortableRuleList } from '@/Sources/components/SortableRuleList'; +import { TRuleBlock } from '@/Sources/hooks/useFetchRuleBlocks/types'; +import { BlockRow } from './BlockRow'; + +export const RbacOrderList = ({ + label, + ordered, + titleOf, + isPending, + changed, + onMove, +}: { + label: string; + ordered: TRuleBlock[]; + titleOf: (block: TRuleBlock) => string; + isPending: boolean; + changed: boolean; + onMove: (from: number, to: number) => void; +}) => { + const { isAuthorized } = RbacProtected.useRbac({ + action: RbacProtected.rbacActions.source_write, + }); + return ( + } + /> + ); +}; diff --git a/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/RuleOrderList.helpers.ts b/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/RuleOrderList.helpers.ts new file mode 100644 index 000000000..59756e86e --- /dev/null +++ b/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/RuleOrderList.helpers.ts @@ -0,0 +1,18 @@ +import { TRuleBlock } from '@/Sources/hooks/useFetchRuleBlocks/types'; + +export const sameOrder = (left: string[], right: string[]) => + left.length === right.length && + left.every((id, index) => id === right[index]); + +export const KIND_LABEL: Record = { + source: 'Source', + profile: 'Compatibility profile', + default: 'Default', +}; + +export const moveId = (ids: string[], from: number, to: number) => { + const next = [...ids]; + const [moved] = next.splice(from, 1); + if (moved !== undefined) next.splice(to, 0, moved); + return next; +}; diff --git a/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/index.tsx b/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/index.tsx new file mode 100644 index 000000000..aaafd7bfd --- /dev/null +++ b/apps/dfe-core-ui/src/Sources/components/IngestProcedures/RuleOrderList/index.tsx @@ -0,0 +1,173 @@ +'use client'; + +import { CustomScrollbar } from '@/core/components/CustomScrollbar'; +import { NotificationCard } from '@/core/components/NotificationCard'; +import { useSetComponentHeight } from '@/core/hooks/useSetComponentHeight'; +import { IngestWriteRefusal } from '@/Sources/components/IngestRules/IngestWriteRefusal'; +import { IngestWriteResult } from '@/Sources/components/IngestRules/IngestWriteResult'; +import { + TRuleBlock, + TRuleTarget, +} from '@/Sources/hooks/useFetchRuleBlocks/types'; +import { useReorderRuleBlocks } from '@/Sources/hooks/useReorderRuleBlocks'; +import { Button } from 'antd'; +import { useMemo, useState } from 'react'; +import { RbacOrderList } from './RbacOrderList'; +import { moveId, sameOrder } from './RuleOrderList.helpers'; + +interface RuleOrderListProps { + target: TRuleTarget; + title: string; + description: string; + blocks: TRuleBlock[]; + etag: string | null; + titleOf: (block: TRuleBlock) => string; + nameOf: (id: string) => string; + onReload: () => Promise; + isReloading: boolean; + componentHeight?: { + initialOffset: number; + }; +} + +export const RuleOrderList = ({ + target, + title, + description, + blocks, + etag, + titleOf, + nameOf, + onReload, + isReloading, + componentHeight: componentHeightProp = { + initialOffset: 0, + }, +}: RuleOrderListProps) => { + const serverIds = useMemo(() => blocks.map((block) => block.id), [blocks]); + const [draft, setDraft] = useState(null); + const [sentIds, setSentIds] = useState([]); + const { mutate, isPending, error, reset, data } = useReorderRuleBlocks({ + etag, + onSuccess: () => setDraft(null), + }); + + // A draft only stands while it names the blocks the engine has. + const draftIds = + draft && + draft.length === serverIds.length && + draft.every((id) => serverIds.includes(id)) + ? draft + : serverIds; + const byId = new Map(blocks.map((block) => [block.id, block])); + const ordered = draftIds + .map((id) => byId.get(id)) + .filter((block): block is TRuleBlock => !!block); + const isDirty = !sameOrder(draftIds, serverIds); + const movable = ordered.filter((block) => !block.pinned); + + const handleReload = () => { + void onReload().then(() => { + setDraft(null); + reset(); + }); + }; + + const handleSave = () => { + const ids = movable.map((block) => block.id); + setSentIds(ids); + mutate({ target, blocks: ids }); + }; + + const dirtyHeightOffset = isDirty ? 50 : 0; + const errorHeightOffset = error ? 100 : 0; + const resultHeightOffset = data && !isDirty ? 62 : 0; + + const { componentHeight } = useSetComponentHeight({ + offset: + componentHeightProp?.initialOffset + + dirtyHeightOffset + + errorHeightOffset + + resultHeightOffset, + }); + return ( +
    +
    +

    + {title} +

    +

    + {description} +

    +
    + + {isDirty && ( + + + + + } + /> + )} + + {error ? ( + + ) : ( + data && + !isDirty && + )} + + {movable.length === 0 && ( +

    + No block here can move: only the default runs. +

    + )} + + + { + reset(); + setDraft(moveId(draftIds, from, to)); + }} + /> + +
    + ); +}; diff --git a/apps/dfe-core-ui/src/Sources/components/IngestProcedures/index.tsx b/apps/dfe-core-ui/src/Sources/components/IngestProcedures/index.tsx new file mode 100644 index 000000000..785f29708 --- /dev/null +++ b/apps/dfe-core-ui/src/Sources/components/IngestProcedures/index.tsx @@ -0,0 +1,132 @@ +import { NotificationCard } from '@/core/components/NotificationCard'; +import { RuleOrderList } from './RuleOrderList'; + +import { GenericErrorCard } from '@/core/components/GenericError'; +import { DeploymentCompatibility } from '@/Sources/components/IngestRules/DeploymentCompatibility'; +import { useFetchCompatProfiles } from '@/Sources/hooks/useFetchCompatProfiles'; +import { useFetchRuleBlocks } from '@/Sources/hooks/useFetchRuleBlocks'; +import { + TRuleBlock, + TRuleTarget, +} from '@/Sources/hooks/useFetchRuleBlocks/types'; +import { IconAlertTriangle } from '@repo/dfe-icons'; +import { Spin } from 'antd'; + +const TARGETS: { target: TRuleTarget; title: string; description: string }[] = [ + { + target: '_source', + title: 'Source rules', + description: + 'Which source a record belongs to. The receiver tries each block from the top, and the first that matches decides.', + }, + { + target: '_org_id', + title: 'Org rules', + description: + 'Which org a record belongs to, tried the same way once its source is known.', + }, +]; + +export const IngestProcedures = () => { + const { data, isLoading, isFetching, error, refetch } = useFetchRuleBlocks(); + const { profiles } = useFetchCompatProfiles(); + + const titleOf = (block: TRuleBlock) => { + if (block.kind === 'default') return 'Default'; + if (block.kind === 'profile') { + return ( + profiles?.find((profile) => profile.id === block.name)?.title ?? + block.name + ); + } + return block.name; + }; + + // Ids are source., compat. or default, and a refusal can name one this page has not read. + const nameOf = (id: string) => { + const [kind, ...rest] = id.split('.'); + const name = rest.join('.'); + if (kind === 'source') return name; + if (kind === 'compat') { + return profiles?.find((profile) => profile.id === name)?.title ?? name; + } + return id === 'default' ? 'Default' : id; + }; + return ( +
    + + {isLoading && ( + <> + Loading rule blocks + + )} + {error && ( + + )} + {data && !data.etag && ( + } + title="This order is read only here" + description="The engine answered without a revision the browser may read, which a save has to send back. The console reaches the engine on another origin that does not expose the ETag header." + /> + )} + +
    + {data ? ( + block.target === TARGETS[0].target, + )} + etag={data.etag} + titleOf={titleOf} + nameOf={nameOf} + onReload={refetch} + isReloading={isFetching} + /> + ) : ( +
    + )} + +
    + + {data ? ( + block.target === TARGETS[1].target, + )} + etag={data.etag} + titleOf={titleOf} + nameOf={nameOf} + onReload={refetch} + isReloading={isFetching} + /> + ) : ( +
    + )} +
    +
    +
    + ); +}; diff --git a/apps/dfe-core-ui/src/Sources/components/IngestRules/RuleOrder.tsx b/apps/dfe-core-ui/src/Sources/components/IngestRules/RuleOrder.tsx index 7ce65f481..fb13e3508 100644 --- a/apps/dfe-core-ui/src/Sources/components/IngestRules/RuleOrder.tsx +++ b/apps/dfe-core-ui/src/Sources/components/IngestRules/RuleOrder.tsx @@ -192,6 +192,7 @@ const RuleOrderList = ({ ordered={ordered} titleOf={titleOf} isPending={isPending} + changed={isDirty} onMove={(from, to) => { reset(); setDraft(moveId(draftIds, from, to)); @@ -247,12 +248,14 @@ const RbacOrderList = ({ ordered, titleOf, isPending, + changed, onMove, }: { label: string; ordered: TRuleBlock[]; titleOf: (block: TRuleBlock) => string; isPending: boolean; + changed: boolean; onMove: (from: number, to: number) => void; }) => { const { isAuthorized } = RbacProtected.useRbac({ @@ -264,6 +267,7 @@ const RbacOrderList = ({ label={label} itemName={titleOf} disabled={!isAuthorized || isPending} + changed={changed} onMove={onMove} renderItem={(block) => } /> diff --git a/apps/dfe-core-ui/src/Sources/components/SortableRuleList/SortableDropdown.tsx b/apps/dfe-core-ui/src/Sources/components/SortableRuleList/SortableDropdown.tsx new file mode 100644 index 000000000..7a2019795 --- /dev/null +++ b/apps/dfe-core-ui/src/Sources/components/SortableRuleList/SortableDropdown.tsx @@ -0,0 +1,76 @@ +'use client'; + +import { IconChevronDown, IconChevronUp } from '@repo/dfe-icons'; +import { Button, Dropdown, Select } from 'antd'; +import { useState } from 'react'; + +export const SortableDropdown = ({ + onChange, + onOpenChange, + options, + buttonRef, + open: openProp, + ruleName, +}: { + onChange: (to: number) => void; + onOpenChange: (open: boolean) => void; + options: { value: number; label: string; disabled: boolean }[]; + buttonRef: React.Ref; + open: boolean; + ruleName: string; +}) => { + const [open, setOpen] = useState(openProp); + return ( + { + onOpenChange(next); + setOpen(next); + }} + popupRender={() => ( +
    event.preventDefault()} + > +