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

children

React.ReactNode

Yes

The inner content of the anchor tag, typically the link's display text or nested components.

link

string

No

undefined

Custom URL for the link. If omitted, the component generates the URL automatically.

preventDefault

boolean

No

false

If true, disables navigation by preventing the default action on click, except for supported preview types.

color

string

No

'rgb(15, 79, 170)'

Inline CSS color for the link text (ignored if className is provided).

documentName

string

Yes

The name of the document, used to extract the file extension.

documentId

string

No

undefined

Unique identifier for the document, used to build the preview URL.

prefix

string

No

'file'

A prefix query parameter added to the generated URL.

className

string

No

undefined

CSS class applied to the anchor tag. Overrides inline color styling if provided.

Functionality

  1. URL Construction

    • If the link prop is provided, it is used directly as the href attribute of the anchor tag.

    • Otherwise, the URL is constructed in the format:

      /document/{documentId}?ext={extension}&prefix={prefix}
      

      where

      • {extension} is extracted from the documentName using the imported getExtension utility.

      • {prefix} defaults to 'file' if not provided.

  2. Click Behavior Control

    • The onClick handler is set conditionally:

      • If preventDefault is false OR the document extension is supported for preview (isSupportedPreviewDocumentType(extension) returns true), the link behaves normally.

      • Otherwise, the default click action is prevented, effectively disabling navigation.

  3. Styling

    • If a className is provided, it is applied and the inline color style is omitted.

    • If no className is provided, the link text color is set via inline style using the color prop.

    • The link text supports breaking long words with wordBreak: 'break-all'.

  4. 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's window.opener for 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


Interaction with Other Parts of the System


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.