chatwoot/app/javascript/shared/helpers/documentHelper.js
Aakash Bakhle f6be0d80ef
feat: UI changes for document auto sync [AI-153] (#14258)
# Pull Request Template

## Description

FE code for document sync

Adds:
- UI to show counts (stats) of available web pages, stale and synced
documents and last synced at
- Bulk action and manual ways to sync web documents
- index to stats related columns

## Type of change

Please delete options that are not relevant.

- [x] New feature (non-breaking change which adds functionality)

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration.

https://linear.app/chatwoot/issue/AI-153/fe-document-auto-sync

Documents dashboard:
<img width="2160" height="986" alt="CleanShot 2026-05-11 at 17 57 09@2x"
src="https://github.com/user-attachments/assets/6d934764-964c-4656-b005-1b4f0329e553"
/>

Filters:
<img width="1138" height="564" alt="CleanShot 2026-05-11 at 17 58 13@2x"
src="https://github.com/user-attachments/assets/cee780e6-eb8f-4aed-8cc5-b674244a821b"
/>

Needs update:
<img width="2222" height="966" alt="CleanShot 2026-05-11 at 17 57 53@2x"
src="https://github.com/user-attachments/assets/70c85ddd-7eb1-4328-ba14-7929e67e7b36"
/>

pdfs:
<img width="2180" height="558" alt="CleanShot 2026-05-11 at 17 58 30@2x"
src="https://github.com/user-attachments/assets/975b5c9f-bd1c-4979-9870-8f926d7f6e11"
/>

bulk actions:
<img width="2244" height="992" alt="CleanShot 2026-05-11 at 17 58 57@2x"
src="https://github.com/user-attachments/assets/bdb3c63f-d2de-41dc-a6d5-8821d3303be0"
/>

single url sync:
<img width="2264" height="722" alt="CleanShot 2026-05-11 at 17 59 19@2x"
src="https://github.com/user-attachments/assets/7d7323a5-0fcb-4be9-8635-55e56964999b"
/>



## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged and published in downstream
modules

---------

Co-authored-by: Sivin Varghese <[email protected]>
Co-authored-by: iamsivin <[email protected]>
Co-authored-by: Muhsin Keloth <[email protected]>
Co-authored-by: Sony Mathew <[email protected]>
Co-authored-by: Vishnu Narayanan <[email protected]>
2026-05-11 20:13:29 +05:30

79 lines
2.5 KiB
JavaScript

/**
* Document Helper - utilities for document display and formatting
*/
// Constants for document processing
const PDF_PREFIX = 'PDF:';
const TIMESTAMP_PATTERN = /_\d{14}(?=\.pdf$)/; // Format: _YYYYMMDDHHMMSS before .pdf extension
const URL_DISPLAY_PREFIX_PATTERN = /^https?:\/\/(www\.)?/i;
/**
* Checks if a document is a PDF based on its external link
* @param {string} externalLink - The external link string
* @returns {boolean} True if the document is a PDF
*/
export const isPdfDocument = externalLink => {
if (!externalLink) return false;
return externalLink.startsWith(PDF_PREFIX);
};
/**
* Checks if a link is safe to bind to an href attribute (http/https only).
* Guards against schemes like `javascript:` that would execute on click.
* @param {string} externalLink - The external link string
* @returns {boolean} True if the link uses http or https
*/
export const isSafeHttpLink = externalLink => {
if (!externalLink) return false;
try {
const { protocol } = new URL(externalLink);
return protocol === 'http:' || protocol === 'https:';
} catch (e) {
return false;
}
};
/**
* Formats the display link for documents
* For PDF documents: removes 'PDF:' prefix and timestamp suffix
* For regular URLs: strips http(s):// and www. for a denser list view
*
* @param {string} externalLink - The external link string
* @returns {string} Formatted display link
*/
export const formatDocumentLink = externalLink => {
if (!externalLink) return '';
if (isPdfDocument(externalLink)) {
// Remove 'PDF:' prefix
const fullName = externalLink.substring(PDF_PREFIX.length);
// Remove timestamp suffix if present
return fullName.replace(TIMESTAMP_PATTERN, '');
}
return externalLink.replace(URL_DISPLAY_PREFIX_PATTERN, '');
};
/**
* Returns the path of a URL for compact display in document lists. This avoids
* repeating the domain while preserving enough context to distinguish pages.
* Falls back to the bare hostname for root URLs and formatDocumentLink for
* malformed URLs and PDFs.
*/
export const getDocumentDisplayPath = externalLink => {
if (!externalLink) return '';
if (isPdfDocument(externalLink)) return formatDocumentLink(externalLink);
try {
const { pathname, hostname } = new URL(externalLink);
const path = pathname.replace(/^\/+/, '');
if (!path) return hostname.replace(/^www\./i, '');
try {
return decodeURIComponent(path);
} catch (e) {
return path;
}
} catch (e) {
return formatDocumentLink(externalLink);
}
};