export default Home; in this in judgemnets i wanted this link to open file://192.168.13.30/c/GGN/GGN%20Office%20Repository/Administrative%20authority%20cannot%20exercise%20power%20when%20matter%20is%20sub%20judice,%20pending/ this link to open ......when name of isse which is Administrative authority cannot exercise power when matter is subjudice,pending this is selected ....so plz do this
To achieve the desired functionality where clicking specific filenames in the "Judgments" section and specific issues in the "Issues" section will open different links, you can modify your ResultCard component and the search results handling in the Home component.
Here's how you can implement it:
Modify the ResultCard Component:
In the ResultCard, you can add conditional logic to handle the clicks differently based on the file type (judgments or issues).
Update the File Paths:
Define the specific URLs for "Judgments" and "Issues" that should open when clicked.
Here’s how you can modify the ResultCard component and include the necessary checks in the Home component regarding resultMatches.
Dynamic Link Handling: In the openFile function in the ResultCard component, I've created two constant objects: judgmentFiles and issueFiles that map specific filenames to the corresponding file:// URLs.
Conditional Opening of URLs: When a user clicks on the filename, the openFile function checks whether the clicked filename belongs to the judgments or issues category and opens the corresponding link in a new tab using window.open.
Styling for Clickable Link: The filename in the ResultCard is styled to look like a link, enhancing user experience.
Result
With these modifications, clicking on the specific filenames or issues will open the correct file links in new tabs, as required. Adjust or expand the judgmentFiles and issueFiles objects based on additional documents you have.
Certainly! Below is the complete updated code for both the Home component and the ResultCard component, including the necessary modifications to handle the specified file paths for judgments and issues.
Link Handling: Added functional logic to ResultCard for handling specific paths when specific filenames are clicked.
Conditional Rendering: Updated how results are rendered in conjunction with the search type.
Integration: Ensured that searchType is passed to ResultCard so it can determine which file path to link to.
This code assumes you have a certain structure and styling already defined in your application (like CSS classes). Make sure to adjust paths, method implementations, or surrounding components as needed for your own project setup.
To ensure that your app opens local file paths correctly using the file:// protocol instead of redirecting to a web URL, you need to handle the link behavior correctly in your React app. In a web application context, browsers treat links starting with file:// differently and may not directly allow navigation due to security restrictions.
Steps to Ensure Correct Behavior
Prevent Default Link Behavior: For files that are intended to be opened locally, you need to ensure that any normal click behavior on the link is overridden or handled specifically to use window.open.
Check the Link Logic: Update the link-opening logic in your ResultCard component to make sure it differentiates between the web URLs (which can be structured like your localhost example) and local file paths.
Updated ResultCard Code
Here's how you can adjust the existing ResultCard component so that clicking on certain filenames correctly opens the respective file paths:
javascript
import React from 'react';
const ResultCard = ({ result, onAddToLibrary, cardNumber, searchType }) => {
const openFile = (filename) => {
// Define mapping for judgment files
const judgmentFiles = {
'01.NHAI Rayalseema Expressway Pvt.Ltd..pdf': 'file://192.168.13.30/c$/GGN%20Office%20Repository/Actual%20proof%20of%20loss%20not%20required%20-%20MoRTH%20SDB%20calculation/01.%20NHAI%20v.%20Rayalseema%20Expressway%20Pvt.%20Ltd..pdf',
// Add more mappings as necessary
};
// Define mapping for issue files
const issueFiles = {
'Administrative authority cannot exercise power when matter is subjudice,pending': 'file://192.168.13.30/c$/GGN%20Office%20Repository/Administrative%20authority%20cannot%20exercise%20power%20when%20matter%20is%20sub%20judice,%20pending/',
// Add more mappings as necessary
};
// Check the type and open the corresponding URL
if (searchType === 'judgments' && judgmentFiles[filename]) {
window.open(judgmentFiles[filename], '_blank');
} else if (searchType === 'issues' && issueFiles[filename]) {
window.open(issueFiles[filename], '_blank');
} else {
// If filename is not found in mappings, optionally handle that
console.warn('No URL mapping found for:', filename);
}
};
return (
<div className="result-card">
<div className="result-header">
<h3>
<span className="result-number">{cardNumber}.</span>
<span
onClick={() => openFile(result.Filename.split('\\').pop())}
style={{ cursor: 'pointer', color: 'blue', textDecoration: 'underline' }}
>
{result.Filename.split('\\').pop()}
</span>
</h3>
<button onClick={() => onAddToLibrary(result)} className="add-button">
Add
</button>
</div>
{result.matches.length > 0 ? (
<div className="matches">
{result.matches.map((match, index) => (
<div key={index} className="match-item">
<div>Page {match.page}</div>
<div
className="snippet"
dangerouslySetInnerHTML={{
__html: match.snippet.replace(
new RegExp(`(${match.keyword})`, 'gi'),
'<mark>$1</mark>'
),
}}
/>
</div>
))}
</div>
) : (
<p className="no-matches">No matches found</p>
)}
</div>
);
};
export default ResultCard;
Key Changes Made
Mapped File Paths: The openFile function checks the filename and opens the corresponding local path using file:// if it's a judgment or issue, based on the search type.
Handled Clicks Properly: Each filename is wrapped in a <span> that listens for clicks, preventing default anchor behavior, and window.open is used to programmatically open the specified file.
Additional Notes
Security Restrictions: Keep in mind that browsers have security features that may prevent file:// URLs from being opened by web pages for security reasons. Ensure your application context supports this if it's going to be used in a production or real-world environment.
Configuration for Local Development: When developing locally, ensure your environment allows access to these local files. Otherwise, you may need to adjust folder permissions or configurations depending on the operating system and the server settings being used.
Testing: Test this in a controlled environment where you know the local paths are correct and accessible.
By implementing these changes, you should be able to open the intended local files correctly upon clicking the filenames in your results.
If clicking the filename in your React application is still navigating to an unwanted link (like http://localhost:3001/...) instead of opening a local file, there are a few aspects to ensure correctness:
Ensure You Are Overriding Default Link Behavior
We need to ensure that clicking on the link for the filename opens the local file path instead of the default behavior of a link, which will redirect to a web URL. Make sure that no <a> tags are present directly, and instead use a clickable <span> as described earlier.
Updated ResultCard Code
Here’s a revised version of the ResultCard component that makes sure you clearly handle the clicks without default behavior:
javascript
import React from 'react';
const ResultCard = ({ result, onAddToLibrary, cardNumber, searchType }) => {
const openFile = (filename) => {
const judgmentFiles = {
'01.NHAI Rayalseema Expressway Pvt.Ltd..pdf': 'file://192.168.13.30/c$/GGN%20Office%20Repository/Actual%20proof%20of%20loss%20not%20required%20-%20MoRTH%20SDB%20calculation/01.%20NHAI%20v.%20Rayalseema%20Expressway%20Pvt.%20Ltd..pdf',
// Add more mappings as necessary
};
const issueFiles = {
'Administrative authority cannot exercise power when matter is subjudice,pending': 'file://192.168.13.30/c$/GGN%20Office%20Repository/Administrative%20authority%20cannot%20exercise%20power%20when%20matter%20is%20sub%20judice,%20pending/',
// Add more mappings as necessary
};
if (searchType === 'judgments' && judgmentFiles[filename]) {
window.open(judgmentFiles[filename], '_blank'); // Opens the local file
} else if (searchType === 'issues' && issueFiles[filename]) {
window.open(issueFiles[filename], '_blank'); // Opens the local file
} else {
// Fallback if no mapping is found
console.warn('No URL mapping found for:', filename);
}
};
return (
<div className="result-card">
<div className="result-header">
<h3>
<span className="result-number">{cardNumber}.</span>
<span
onClick={() => openFile(result.Filename.split('\\').pop())} // Extract the filename correctly
style={{ cursor: 'pointer', color: 'blue', textDecoration: 'underline' }}
>
{result.Filename.split('\\').pop()} // Show the filename correctly
</span>
</h3>
<button onClick={() => onAddToLibrary(result)} className="add-button">
Add
</button>
</div>
{result.matches.length > 0 ? (
<div className="matches">
{result.matches.map((match, index) => (
<div key={index} className="match-item">
<div>Page {match.page}</div>
<div
className="snippet"
dangerouslySetInnerHTML={{
__html: match.snippet.replace(
new RegExp(`(${match.keyword})`, 'gi'),
'<mark>$1</mark>'
),
}}
/>
</div>
))}
</div>
) : (
<p className="no-matches">No matches found</p>
)}
</div>
);
};
export default ResultCard;
Key Changes and Checks to Make
Clickable Span: Ensure that you use a <span> instead of an <a> tag for the filename. This approach prevents the default behavior of linking to a web URL.
Correct Mapping: Ensure the mappings in the judgmentFiles and issueFiles objects are correct and correspond to the filenames correctly extracted. Double-check if the filename being extracted via result.Filename.split('\\').pop() matches the keys in those mapping objects.
Testing in Different Environments: If you’re developing locally through a server (like a React development server), remember that some browsers might block file:// URLs due to security policies. If you find that opening file:// URLs still doesn't behave as expected, test the application in an environment that allows for local file access.
Browser Security Settings: Some modern browsers may block access to local files as a security precaution or require flags to accept them. Be aware of this if you're experiencing issues that seem like code bugs.
Console Logging: Use console logging to verify that the correct filename and URL path is being constructed when you click the filename. This will help you troubleshoot if the mappings are incorrectly defined.
Ensure Integrated Components
Be sure you integrate this code with the Home component properly and that the ResultCard receives the correct props.
Example of Using ResultCard in Home Component
Make sure to render it properly within your Home component:
javascript
<div className="results-list">
{searchResults.map((result, index) => (
<ResultCard
key={index}
result={result}
onAddToLibrary={handleAddToLibrary}
cardNumber={index + 1}
searchType={searchType} // Pass the search type to determine which file link to open
/>
))}
</div>
With these adjustments and checks, the application should open the specified local files correctly instead of directing to undesired URLs.