-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Refactor] Establish Pattern and Remove Routing Dependencies #1108
Comments
From Data Catalog implementation try, we could find the routing problems that need to be handled. Base finding: react-router-dom component doesn't work with Next instance, and Next has its own Link component.
|
Update: This has been kindof been worked on when during breakout components by having the Link Component pass down as a prop. But there are some hacks in there that need to be clean up and we should relook at how we are doing this to see if it is the most optimal and clean... Going to think about what we should do with this ticket... |
**Related Ticket:** Contributes to #1108 This aims to ensure catalog filters work correctly in Next.js. ### Description of Changes Removes the usage of React Router's `useNavigate` hook in the library to make it more agnostic of routing frameworks, allowing it to work better with Next.js instances. Changes included: - Added a custom `commit` function to `useQsStateCreator`. It appears this was the only place where the `useNavigate` function was effectively in use. I tried using the default `commit` function from the [qs-state-hook](https://github.com/danielfdsilva/qs-state-hook) library, but it doesn't work with Next.js - Formatted the touched files ### Validation / Testing Using `veda-config`: 1. Visit the data catalog page 2. Click on filters, and URL params are updated as usual 3. Reload the page with filters selected; the filter panel is populated as expected 4. Repeat the steps on the Exploration and Stories pages Using `next-veda-ui` instance: 1. Link the source of this PR to the Next.js instance using the instructions in `veda-ui/docs/development/REGISTRY.md` 2. Replace the file `app/(datasets)/data-catalog/catalog.tsx` (this avoids build errors caused by type issues) ```js 'use client'; import React from 'react'; import { CatalogView, useFiltersWithQS } from '@lib'; import { usePathname } from 'next/navigation'; import Link from 'next/link'; export default function Catalog({ datasets }: { datasets: any }) { const pathname = usePathname(); const controlVars = useFiltersWithQS(); return ( <CatalogView datasets={datasets} onFilterChanges={() => controlVars} pathname={pathname} linkProperties={{ LinkElement: Link, pathAttributeKeyName: 'href', }} /> ); } ``` 3. Access `http://localhost:3000/data-catalog`. Filter changes are reflected in the URL, and reloading populates the filter panel as expected This is ready for review.
@vgeorge some ideas we discussed beforehand were possibly creating a context provider similar to what was created for environment variables. Gjore's PR here. This would reduce the copious amounts of prop drilling we currently do with My two cents on what I've seen best patterns wise but again we probably want to take time and effort into consideration. cc @dzole0311 |
…pendency from the Catalog (#1276) **Related Ticket:** Contributes to #1108 ### Description of Changes - Remove `react-router` dependency from the Catalog components - Introduce custom `usePathname` hook - Remove prop drilling of pathname from the Catalog ### Notes & Questions About Changes The `pathname` prop is passed down for two things: 1. Generating a link to the dataset at https://github.com/NASA-IMPACT/veda-ui/blob/820730fd7c1e25e68997b200d31bc4ee27525f7e/app/scripts/components/common/catalog/catalog-card.tsx#L126 2. Controlling scroll in the filters control at https://github.com/NASA-IMPACT/veda-ui/blob/820730fd7c1e25e68997b200d31bc4ee27525f7e/app/scripts/components/common/catalog/filters-control.tsx#L57 I suspect that the usage on item 1 was intented to cover the use case of an instance running on a subpath (e.g. `https://myinstance.org/veda`), why is hard to replicate locally but this change shouldn't affect that. ### Validation / Testing Using `veda-config`: 1. Visit the data catalog page 1. Clicking on cards should open the proper URL (same as before) 1. Visit Explorer page, clicking on cards selected them, instead of visiting an URL (same as before) Using `next-veda-ui` instance: 1. Link the source of this PR to the Next.js instance 1. Visit the catalog page 1. Clicking on cards should open the proper URL (same as before) 1. Visit Explorer page, clicking on cards selected them, instead of visiting an URL (same as before) This is ready for review.
I will have limited availability in the next few weeks and wanted to provide a status update in case someone else has time to tackle the remaining work. In #1270 and #1276, we reduced the dependency on React Router. I believe the next steps are:
import React from 'react';
// try loading dependencies during build time
let NextLink: any;
try {
NextLink = require('next/link').default;
} catch (e) {
NextLink = null;
}
let ReactRouterLink: any;
try {
ReactRouterLink = require('react-router-dom').Link;
} catch (e) {
ReactRouterLink = null;
}
interface CustomLinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
to?: string;
href?: string;
children: React.ReactNode;
}
export const CustomLink = ({ to, href, children, ...props }: CustomLinkProps) => {
const path = to || href;
if (NextLink) {
return (
<NextLink href={path} {...props}>
{children}
</NextLink>
);
}
if (ReactRouterLink) {
return (
<ReactRouterLink to={path} {...props}>
{children}
</ReactRouterLink>
);
}
// Fallback to a regular anchor tag if no routing library is available
return (
<a href={path} {...props}>
{children}
</a>
);
}; |
@vgeorge wrong "Sandra" tagged 😄 |
Thanks for the input & thoughts @vgeorge 🙇♀ . I'm thinking if we are going to tackle the issue with routing coupled up with the components we should probably fully tackle it since this ticket was meant for that. I'm thinking the proposed solution above might still be some sort of bandage as it still worries about "how to navigate" in our veda-ui components when it'd be ideal to totally move away from that. But I agree with starting with the card component to iterate on removing routing entirely from our library. Another thing is, we are using Next in our template instance but ideally we dont want the veda-ui library to care or know - if someone wanted to use our library with Gatsby, they should be able too (I think gatsby works with react-router-dom though but has its own preferred routing mechanism). Thinking that creating some type custom routing controller component that handles based on framework isn't too scalable and is hence more bandagey?? I'll experiment with a first pass at removing routing in one of our components to see how that goes and we could bounce off ideas from that PR :) PR experimentation here: https://github.com/NASA-IMPACT/veda-ui/pull/1310/files |
This will be done in iterations as we work to remove components in veda-ui from being tightly integrated to routing/nav. Follow up tickets have been created here...
|
**Related Ticket:** #1108 **Related PR:** developmentseed/next-veda-ui#25 **v5.11.3-alpha.0** published from this branch ### Description of Changes As part of the greater holistic approach - I think its best to move away from having our library components tightly integrated w/ routing. This is the first iteration to remove **linkProperties**. This PR only worries about removing **linkProps** the **DataCatalog Component**. DataCatalog view no longer has to pass in linkProperties or have to directly worry about routing, we can just pass in a callback now that decides what to do during some action. This is an iterative approach, i've created tickets to remove routing from the other core components. But we can't remove routing directly from the Card component itself easily as GHG uses the card component directly [here](https://github.com/US-GHG-Center/veda-config-ghg/blob/dfe611f27f910bc6428a2b13fe50678710511e68/custom-pages/news-and-events/component.tsx#L5). So that should be its own separate ticket BUT... we are to be redoing the card component for the new instances - where its probably best to rewrite it. As currently its not in an ideal place to scale... so i'll create a placeholder ticket for card component for now but we can probably tackle removing routing when rewriting the card component. cc @vgeorge @hanbyul-here @dzole0311 Follow-up tickets created: * #1325 * #1326 * #1327 ### Notes & Questions About Changes _{Add additonal notes and outstanding questions here related to changes in this pull request}_ ### Validation / Testing - [ ] Validate DataCatalog cards are linking correctly - [ ] Validate selecting cards in DatasetSelectorModal is working as it should
@sandrahoang686 while I agree we shouldn't be handling every framework use case, I don't think relying only on event handlers like |
We need to decouple react-router and navigation dependencies within our veda-ui components. This is because routing is not controlled at the instance level and react-router does not work for nextJs.
Components shouldn't manage any of the routing and should just return the selected filters in which the passed in callback from the instance side should handle the routing.
The text was updated successfully, but these errors were encountered: