new-document-link.tsx
Overview
new-document-link.tsx is a React functional component that renders a customizable hyperlink (<a> element) intended for linking to document previews or downloads within an application. The component dynamically constructs a URL for the document based on the provided document ID and name, and applies conditional behavior to the link's click event depending on the document type and user preferences.
This component is useful in contexts where documents can be previewed or downloaded, and where certain document formats support inline previewing while others do not. It enhances user experience by optionally preventing navigation for unsupported document types or when explicitly disabled.
Detailed Explanation
Component: NewDocumentLink
Description
A React functional component that creates a styled link to a document resource. It supports automatic URL generation for document previewing based on documentId and documentName. It also conditionally disables link navigation for unsupported preview document types, controlled via the preventDefault prop.
Props Interface: IProps
Prop Name | Type | Required | Default Value | Description |
|---|---|---|---|---|
Yes | The inner content of the anchor tag, typically the link's display text or nested components. | |||
|
| No | Custom URL for the link. If omitted, the component generates the URL automatically. | |
|
| No |
| If |
|
| No |
| Inline CSS color for the link text (ignored if |
|
| Yes | The name of the document, used to extract the file extension. | |
|
| No | Unique identifier for the document, used to build the preview URL. | |
|
| No |
| A prefix query parameter added to the generated URL. |
|
| No | CSS class applied to the anchor tag. Overrides inline color styling if provided. |
Functionality
URL Construction
If the
linkprop is provided, it is used directly as thehrefattribute of the anchor tag.Otherwise, the URL is constructed in the format:
/document/{documentId}?ext={extension}&prefix={prefix}where
{extension}is extracted from thedocumentNameusing the importedgetExtensionutility.{prefix}defaults to'file'if not provided.
Click Behavior Control
The
onClickhandler is set conditionally:If
preventDefaultisfalseOR the document extension is supported for preview (isSupportedPreviewDocumentType(extension)returnstrue), the link behaves normally.Otherwise, the default click action is prevented, effectively disabling navigation.
Styling
If a
classNameis provided, it is applied and the inlinecolorstyle is omitted.If no
classNameis provided, the link text color is set via inline style using thecolorprop.The link text supports breaking long words with
wordBreak: 'break-all'.
Accessibility / Security
The link opens in a new browser tab (
target="_blank").It includes
rel="noreferrer"to prevent the new page from accessing the original page'swindow.openerfor security reasons.
Usage Example
import NewDocumentLink from './new-document-link';
function DocumentListItem() {
return (
<li>
<NewDocumentLink
documentId="12345"
documentName="example.pdf"
preventDefault={true}
color="#007bff"
>
Open Example PDF
</NewDocumentLink>
</li>
);
}
In this example, the component generates a link like /document/12345?ext=pdf&prefix=file, styled with a blue color, and disables navigation if the document type is not supported for preview.
Implementation Details
Utilities Used:
getExtension(documentName: string): string
Extracts the file extension (e.g., "pdf", "docx") from thedocumentName.isSupportedPreviewDocumentType(extension: string): boolean
Determines if the file extension corresponds to a document type that supports inline preview.
Conditional Event Handling:
The component uses a ternary operator to decide whether to assign anonClickhandler that prevents default action. This pattern allows the link to be disabled dynamically based on document type and user preference.Dynamic URL Construction:
The logic of building the URL is simple but central to the component's function. It allows fallback to a default route pattern when no explicit link is passed, improving flexibility.
Interaction with Other Parts of the System
This component depends on utilities from
@/utils/document-util, which encapsulate logic for file extension extraction and preview support detection.It is designed to be used anywhere in the application where document links are needed, such as document lists, search results, or document detail views.
The generated URLs likely correspond to routes handled by a document viewer or download handler elsewhere in the app backend or frontend router.
The component is self-contained and primarily concerned with rendering and user interaction; it does not handle document data fetching or state management directly.
Visual Diagram
componentDiagram
component NewDocumentLink {
+props: IProps
+render(): JSX.Element
}
component "document-util" {
+getExtension(name: string): string
+isSupportedPreviewDocumentType(ext: string): boolean
}
NewDocumentLink --> "document-util" : uses
Summary
new-document-link.tsx is a reusable React component for rendering document links with dynamic URL generation and conditional navigation control based on document type support and user input. It provides styling flexibility and integrates with utility functions to handle file extensions and preview capabilities, making it a key element in the document management or preview features of the application.