import React, { useState, useEffect } from 'react'; import { useNavigate } from…
- User
import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import './Home.css';
const ResultCard = ({ result, onAddToLibrary, cardNumber }) => { return ( {cardNumber}. <a href={
http://192.168.2.5:3006/files/${result.relativePath}} target="_blank" rel="noopener noreferrer" > {result.Filename.split('\').pop()} <button onClick={() => onAddToLibrary(result)} className="add-button"> Add {result.matches.length > 0 ? ( {result.matches.map((match, index) => ( Page {match.page} <div className="snippet" dangerouslySetInnerHTML={{ __html: match.snippet.replace( new RegExp((${match.keyword}), 'gi'), '$1' ), }} /> ))} ) : ( No matches found )} ); };const Home = () => { const [searchTerm, setSearchTerm] = useState(''); const [excludeWords, setExcludeWords] = useState(''); const [searchResults, setSearchResults] = useState([]); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const [filteredFilenames, setFilteredFilenames] = useState([]); const [fileInput, setFileInput] = useState(''); const [searchType, setSearchType] = useState('issues'); // State for search type const navigate = useNavigate();
// File paths for judgments const filenames = [ { name: 'Compilation of Judgments - Claimant', path: '\\001. Miscellaneous\\51. Section 16 Arbitration Act\\Compilation of Judgments- Claimant.pdf', }, { name: 'Xerox Corporation and Ors vs PK Khansaheb and Ors', path: '\\001. Miscellaneous\\57.Additional documents\\Xerox_Corporation_and_Ors_vs_PK_Khansaheb_and_Ors_DE201819121816353171COM57952.pdf', }, // Add other filenames here... ]; // Direct paths for issues const issueFiles = [ { name: 'Miscellaneous', path: 'file://192.168.13.30/c$/GGN%20Office%20Repository/Administrative%20authority%20cannot%20exercise%20power%20when%20matter%20is%20sub%20judice,%20pending/', }, { name: 'Another Issue', path: 'file://192.168.2.5/c$/GGN%20Office%20Repository/AnotherIssue.pdf', }, // Add more issue documents here... ]; useEffect(() => { setFilteredFilenames(searchType === 'issues' ? issueFiles : filenames); }, [searchType]); const handleSearchTypeChange = (event) => { const value = event.target.value; setSearchType(value); setFileInput(''); // Clear the input when changing types setFilteredFilenames(value === 'issues' ? issueFiles : filenames); }; const handleFileInputChange = (event) => { const value = event.target.value; setFileInput(value); if (value) { setFilteredFilenames( filteredFilenames.filter((file) => file.name.toLowerCase().includes(value.toLowerCase()) ) ); } else { setFilteredFilenames(searchType === 'issues' ? issueFiles : filenames); } }; const handleSubmit = async (event) => { event.preventDefault(); if (!searchTerm.trim()) { setError('Please enter a search term.'); return; } setIsLoading(true); setError(null); try { const response = await fetch( `http://192.168.2.5:3006/search?keyword=${encodeURIComponent(searchTerm)}&exclude=${encodeURIComponent(excludeWords || '')}&type=${encodeURIComponent(searchType)}` ); if (!response.ok) throw new Error('Failed to fetch search results'); const data = await response.json(); setSearchResults(data); } catch (error) { console.error(error); setError('Error fetching search results.'); } finally { setIsLoading(false); } }; const handleAddToLibrary = async (pdf) => { const token = localStorage.getItem('token'); if (!token) { navigate('/login', { state: { message: 'Please log in to add documents to your library', redirectAfterLogin: '/home', }, }); return; } try { const response = await fetch( 'http://192.168.2.5:3006/api/lib/library/add', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}`, }, body: JSON.stringify({ filename: pdf.relativePath, originalFilePath: pdf.Filename, }), } ); if (response.ok) { alert('Document added to library'); } else { const errorData = await response.json(); alert(errorData.message || 'Failed to add document to library'); } } catch (error) { console.error('Error adding to library:', error); alert('Network error. Please try again.'); } }; return ( <div className="app-container"> {/* Sidebar */} <div className="sidebar"> <div className="sidebar-header"> <h2>S&A Law Offices</h2> </div> <div className="sidebar-content"> <div className="dropdown"> <input type="text" placeholder={searchType === 'issues' ? 'Type an issue to search...' : 'Type a filename to search...'} value={fileInput} onChange={handleFileInputChange} className="sidebar-input" /> <div className="search-type"> <label> <input type="radio" value="issues" checked={searchType === 'issues'} onChange={handleSearchTypeChange} /> Issues </label> <label> <input type="radio" value="judgments" checked={searchType === 'judgments'} onChange={handleSearchTypeChange} /> Judgments </label> </div> {/* Dropdown for filtered filenames */} {fileInput && ( <div className="dropdown-content show"> {filteredFilenames.length > 0 ? ( filteredFilenames.map((file, index) => { return ( <a key={index} href={file.path} // Directly using the path from issues target="_blank" rel="noopener noreferrer" className="suggestion-item" > {file.name} </a> ); }) ) : ( <div className="no-suggestions">No matches found</div> )} </div> )} </div> </div> </div> {/* Main Content */} <div className="main-content"> <div className="search-container"> <form onSubmit={handleSubmit} className="search-form"> <input type="text" placeholder="Enter search term" value={searchTerm} onChange={(e) => setSearchTerm(e.target.value)} className="search-input" /> <input type="text" placeholder="Exclude words (optional)" value={excludeWords} onChange={(e) => setExcludeWords(e.target.value)} className="search-input" /> <button type="submit" disabled={isLoading} className="search-button" > {isLoading ? 'Searching...' : 'Search'} </button> </form> {error && <div className="error-message">{error}</div>} <div className="results-section"> {isLoading ? ( <div className="loading">Loading...</div> ) : searchResults.length > 0 ? ( <> <div className="results-count"> Found {searchResults.length} documents </div> <div className="results-list"> {searchResults.map((result, index) => ( <ResultCard key={index} result={result} onAddToLibrary={handleAddToLibrary} cardNumber={index + 1} /> ))} </div> </> ) : ( <div className="no-results">No results found</div> )} </div> </div> </div> </div> );};
export default Home; in this i the link of miscelneous is not opening by clciking ....but when i copy oaste the link i broqwseer it is opening ....means not clickbale in UI
- Assistant
The issue you're experiencing is likely because of the way the URL for the "Miscellaneous" files is structured. In a web application, certain URLs can be affected by browser security settings, and using
file://URLs might not be appropriately handled for links in a React application.Here's how to troubleshoot and fix that issue:
Troubleshooting Steps
-
Check the URL Format: Make sure the URL for the "Miscellaneous" files is correctly formatted.
-
Console Errors: Open your browser's developer tools (usually F12), and check the console for any errors when you click on the link.
-
Safety Settings: Some browsers prevent navigation to file URLs if they're not explicitly allowed by security settings. Try to adjust these settings if possible, but it's usually not a good idea in a development or production environment.
Potential Fixes
1. Changing the URL Structure
If possible, consider changing the URL format from
file://to serve these files through your application if they are hosted on a server. For example, instead of usingfile://192.168.13.30/..., you could set this up to be accessed via HTTP(S).If you have a server where these files are hosted, update the paths to something like this:
const issueFiles = [ { name: 'Miscellaneous', path: 'http://yourserver.com/path/to/Miscellaneous.pdf', // Change this to the correct server path }, { name: 'Another Issue', path: 'http://yourserver.com/path/to/AnotherIssue.pdf', // Change to actual server URL }, // Add more issue documents here... ];2. Handling Click Events
If you must keep those file links in your application and want to ensure they open properly, you might need to adjust the
hrefor use anonClickhandler:Change the
<a>tag to show a modal or a warning if it refuses to open:<a href={file.path} target="_blank" rel="noopener noreferrer" onClick={(e) => { if (file.path.startsWith('file://')) { e.preventDefault(); alert('This file link might not open in the browser. Please copy and paste this link into your file explorer: ' + file.path); } }} className="suggestion-item" > {file.name} </a>Conclusion
In summary, using
file://links within a web app often leads to usability issues, as many browsers restrict access to such links. The best practice would be to serve the files over HTTP/HTTPS. If that's not possible due to certain constraints, you can provide the users with instructions on how to open the link manually or consider the alternative approach of using a click handler. -