txt-preview.tsx
Overview
txt-preview.tsx is a React functional component that provides a simple text file previewer. It fetches a text file from a given URL, converts its content from a Blob to a string, and displays the content inside a styled container. The component handles the loading state by showing a spinner while the file is being fetched, and shows an error message if the request fails.
This component is useful in applications where users need to preview the content of text files (e.g., logs, documents, or data exports) without downloading them explicitly.
Component: TxtPreviewer
Description
TxtPreviewer is a React component that downloads and renders the contents of a text file from a specified URL. It manages asynchronous fetching, loading states, and error handling internally.
Props
Prop | Type | Required | Description |
|---|---|---|---|
|
| Yes | The URL from which to fetch the text file. |
|
| No | Additional CSS class names for styling the container. |
Internal State
State Variable | Type | Description |
|---|---|---|
|
| Indicates if the file is currently being fetched. |
|
| Stores the text content of the loaded file. |
Functions / Methods
fetchTxt() : Promise<void>
Description: Asynchronously fetches the text file from the specified
urland updates the component state.Implementation details:
Uses a custom
requestutility to perform a GET request expecting a Blob response.Uses
FileReaderAPI to convert Blob to string.On success, updates
datawith the file content and setsloadingtofalse.On failure, shows an error message using the imported
messagecomponent and logs the error.
Usage: Called automatically inside a
useEffecthook when theurlprop changes.
React Hooks
useEffectwith dependency[url]:Triggers
fetchTxt()whenurlchanges.Resets state if
urlis empty or undefined.
Render Output
A styled container
divwith optional additional class names.While loading, displays a centered loading indicator.
When not loading, displays the fetched text inside a
<pre>element with whitespace preserved and wrapped.Uses
classNamesutility to combine base styles with optionalclassNameprop.
Usage Example
import { TxtPreviewer } from './txt-preview';
const Example = () => {
const fileUrl = 'https://example.com/files/sample.txt';
return (
<div style={{ width: 600, height: 400 }}>
<TxtPreviewer url={fileUrl} className="my-custom-class" />
</div>
);
};
Implementation Details & Algorithms
Fetching the file: The component uses a custom
requestfunction (likely a wrapper aroundfetchoraxios) withresponseType: 'blob'to fetch the text file as a binary Blob. This allows efficient handling of potentially large files.Blob to string conversion: After receiving the Blob, a
FileReaderinstance reads the Blob as text asynchronously. This avoids blocking the main thread.Error handling: Any errors during the fetch are caught via the
onErrorcallback provided torequest, triggering a user-visible error message and console logging.Loading state: Managed using the
loadingstate variable to conditionally render the spinner overlay.Styling: Uses Tailwind CSS utility classes (e.g.,
p-4,bg-background-paper,border) and aclassNamesutility for conditional class concatenation.
Interactions with Other Parts of the System
Imports:
messagefrom@/components/ui/message: Provides user feedback on errors.Spinfrom@/components/ui/spin: Loading spinner component.requestfrom@/utils/request: Network request utility, abstracts HTTP calls.classNamesfromclassnames: Utility for conditional CSS classes.
Usage Context:
Typically used in UI views where text file previewing is needed.
Assumes the
urlprop points to a valid accessible resource returning a text file.Relies on global CSS (or Tailwind config) for class names like
bg-background-paperandborder-border-normal.
Component Diagram
componentDiagram
component TxtPreviewer {
+Props: { url: string, className?: string }
+State: loading:boolean, data:string
+fetchTxt(): void
+useEffect(): void
+Render(): JSX
}
component request
component message
component Spin
TxtPreviewer --> request : fetch text file
TxtPreviewer --> message : show error message
TxtPreviewer --> Spin : show loading spinner
Summary
The TxtPreviewer component provides an elegant and reusable way to fetch and display text file content within a React application. Its asynchronous loading, error handling, and clean UI integration make it a useful building block for document preview or file management features.