feat(campaign-ui): review and link managed attachments
This commit is contained in:
53
webui/src/features/campaigns/utils/attachmentPreview.ts
Normal file
53
webui/src/features/campaigns/utils/attachmentPreview.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
export type AttachmentPreviewFileLike = {
|
||||
id: string;
|
||||
display_path: string;
|
||||
filename: string;
|
||||
linked_to_campaign?: boolean;
|
||||
};
|
||||
|
||||
export type AttachmentPreviewLike<T extends AttachmentPreviewFileLike> = {
|
||||
rules: Array<{matches: T[]}>;
|
||||
linkable_files?: T[];
|
||||
};
|
||||
|
||||
export function attachmentPreviewMatchedFiles<T extends AttachmentPreviewFileLike>(
|
||||
preview: AttachmentPreviewLike<T> | null
|
||||
): T[] {
|
||||
if (!preview) return [];
|
||||
const byId = new Map<string, T>();
|
||||
for (const rule of preview.rules) {
|
||||
for (const file of rule.matches) {
|
||||
const key = file.id || file.display_path;
|
||||
if (!key) continue;
|
||||
const existing = byId.get(key);
|
||||
if (existing) {
|
||||
byId.set(key, {
|
||||
...existing,
|
||||
linked_to_campaign: existing.linked_to_campaign !== false || file.linked_to_campaign !== false
|
||||
});
|
||||
} else {
|
||||
byId.set(key, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return [...byId.values()].sort((left, right) => {
|
||||
const linkOrder = Number(left.linked_to_campaign === false) - Number(right.linked_to_campaign === false);
|
||||
if (linkOrder !== 0) return linkOrder;
|
||||
return (left.display_path || left.filename).localeCompare(right.display_path || right.filename);
|
||||
});
|
||||
}
|
||||
|
||||
export function attachmentPreviewLinkableFiles<T extends AttachmentPreviewFileLike>(
|
||||
preview: AttachmentPreviewLike<T> | null
|
||||
): T[] {
|
||||
if (!preview) return [];
|
||||
const source = preview.linkable_files && preview.linkable_files.length > 0
|
||||
? preview.linkable_files
|
||||
: attachmentPreviewMatchedFiles(preview).filter((file) => file.linked_to_campaign === false);
|
||||
const byId = new Map<string, T>();
|
||||
for (const file of source) {
|
||||
const key = file.id || file.display_path;
|
||||
if (key) byId.set(key, file);
|
||||
}
|
||||
return [...byId.values()];
|
||||
}
|
||||
Reference in New Issue
Block a user