Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}`),
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<div className="flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1 hover:cursor-grab">
{block.kind === 'source' ? (
<Link
href={`/sources?source_name=${encodeURIComponent(block.name)}`}
className="truncate font-medium text-foreground! hover:text-tertiary! dark:text-dark-foreground!"
target="_blank"
rel="noopener noreferrer"
>
{title}
</Link>
) : (
<span className="truncate font-medium">{title}</span>
)}
{block.kind !== 'default' && (
<Tag className="m-0" variant="filled">
{KIND_LABEL[block.kind]}
</Tag>
)}
{block.version && (
<span className="text-xs text-foreground/50 dark:text-dark-foreground/50">
{`version ${block.version}`}
</span>
)}
{!block.compiled && (
<Tag className="m-0" color="warning">
Not in the receiver&apos;s rules
</Tag>
)}
{warnings.length > 0 && (
<Tooltip
title={
<ul className="m-0 list-disc pl-4">
{warnings.map((warning) => (
<li key={warning}>{warning}</li>
))}
</ul>
}
>
<span
role="img"
aria-label={`${warnings.length} warning${warnings.length > 1 ? 's' : ''}: ${warnings.join(' ')}`}
className="flex text-warning"
>
<IconAlertTriangle className="h-4 w-4" />
</span>
</Tooltip>
)}
<span className="ml-auto text-xs text-foreground/50 dark:text-dark-foreground/50">
{block.kind === 'default'
? 'Takes what no block above matched'
: `${rows} rule row${rows === 1 ? '' : 's'}`}
</span>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -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 (
<SortableRuleList
items={ordered}
label={label}
itemName={titleOf}
disabled={!isAuthorized || isPending}
changed={changed}
onMove={onMove}
renderItem={(block) => <BlockRow block={block} title={titleOf(block)} />}
/>
);
};
Original file line number Diff line number Diff line change
@@ -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<TRuleBlock['kind'], string> = {
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;
};
Original file line number Diff line number Diff line change
@@ -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<unknown>;
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<string[] | null>(null);
const [sentIds, setSentIds] = useState<string[]>([]);
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 (
<section
aria-labelledby={`rule-order-${target}`}
className="flex flex-col gap-3"
>
<div className="flex flex-col gap-1">
<h3 id={`rule-order-${target}`} className="m-0 text-sm font-semibold">
{title}
</h3>
<p className="m-0 text-sm text-foreground/60 dark:text-dark-foreground/60">
{description}
</p>
</div>

{isDirty && (
<NotificationCard
type="info"
description={
etag
? 'This order is not saved yet.'
: 'The engine sent no revision this console can read, so the order cannot be saved.'
}
action={
<div className="flex items-center justify-end gap-2">
<Button
onClick={() => {
setDraft(null);
reset();
}}
disabled={isPending}
>
Discard
</Button>
<Button
type="primary"
onClick={handleSave}
loading={isPending}
disabled={!etag}
>
Save order
</Button>
</div>
}
/>
)}

{error ? (
<IngestWriteRefusal
error={error}
sentIds={sentIds}
nameOf={nameOf}
onReload={handleReload}
isReloading={isReloading}
/>
) : (
data &&
!isDirty && <IngestWriteResult result={data} savedWhat="Order" />
)}

{movable.length === 0 && (
<p className="m-0 text-sm text-foreground/60 dark:text-dark-foreground/60">
No block here can move: only the default runs.
</p>
)}

<CustomScrollbar height={componentHeight}>
<RbacOrderList
label={`${title} order`}
ordered={ordered}
titleOf={titleOf}
isPending={isPending}
changed={isDirty}
onMove={(from, to) => {
reset();
setDraft(moveId(draftIds, from, to));
}}
/>
</CustomScrollbar>
</section>
);
};
Loading
Loading