Skip to content
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

Fix anchor in datagrid-based lists #5246

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-worms-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

List item links are no longer rendered outside of cursor. This means you can now copy the item's URL with mouse right click or use middle click to open page in new tab.
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import {
} from "@dashboard/components/Datagrid/hooks/useDatagridChange";
import { TablePaginationWithContext } from "@dashboard/components/TablePagination";
import { AttributeFragment } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import useNavigator from "@dashboard/hooks/useNavigator";
import { ListProps, SortPage } from "@dashboard/types";
import { Item } from "@glideapps/glide-data-grid";
@@ -69,9 +70,7 @@ export const AttributeListDatagrid = ({

if (rowData) {
navigate(attributeUrl(rowData.id), {
state: {
prevLocation: location,
},
state: getPrevLocationState(location),
});
}
},
@@ -120,6 +119,7 @@ export const AttributeListDatagrid = ({
onToggle={handlers.onToggle}
/>
)}
navigatorOpts={{ state: getPrevLocationState(location) }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is repeated across each view with the gridd, can just grid call useLocation inside of its component?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it but I think I would rather have opts like that controlled outside of <Datagrid /> component.

/>

<Box paddingX={6}>
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import {
} from "@dashboard/components/Datagrid/hooks/useDatagridChange";
import { TablePaginationWithContext } from "@dashboard/components/TablePagination";
import { CategoryFragment } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import { PageListProps, SortPage } from "@dashboard/types";
import { Item } from "@glideapps/glide-data-grid";
import { Box } from "@saleor/macaw-ui-next";
@@ -105,11 +106,7 @@ export const CategoryListDatagrid = ({
staticColumns={staticColumns}
/>
)}
navigatorOpts={{
state: {
prevLocation: location,
},
}}
navigatorOpts={{ state: getPrevLocationState(location) }}
/>

<Box paddingX={6}>
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import {
} from "@dashboard/components/Datagrid/hooks/useDatagridChange";
import { TablePaginationWithContext } from "@dashboard/components/TablePagination";
import { commonTooltipMessages } from "@dashboard/components/TooltipTableCellHeader/messages";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import { ListProps, SortPage } from "@dashboard/types";
import { Item } from "@glideapps/glide-data-grid";
import { Box, useTheme } from "@saleor/macaw-ui-next";
@@ -157,6 +158,7 @@ export const CollectionListDatagrid = ({
onToggle={handlers.onToggle}
/>
)}
navigatorOpts={{ state: getPrevLocationState(location) }}
/>

<Box paddingX={6}>
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ import { DashboardCard } from "@dashboard/components/Card";
import { getByName } from "@dashboard/components/Filter/utils";
import { FilterPresetsSelect } from "@dashboard/components/FilterPresetsSelect";
import { ListPageLayout } from "@dashboard/components/Layouts";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import useNavigator from "@dashboard/hooks/useNavigator";
import { sectionNames } from "@dashboard/intl";
import { FilterPageProps, PageListProps, SortPage } from "@dashboard/types";
@@ -143,9 +144,7 @@ const CollectionListPage: React.FC<CollectionListPageProps> = ({
filterDependency={filterDependency}
onRowClick={id => {
navigate(collectionUrl(id), {
state: {
prevLocation: location,
},
state: getPrevLocationState(location),
});
}}
hasRowHover={!isFilterPresetOpen}
9 changes: 1 addition & 8 deletions src/components/Card/Root.tsx
Original file line number Diff line number Diff line change
@@ -5,14 +5,7 @@ export const Root: React.FC<PropsWithBox<{ children: React.ReactNode }>> = ({
children,
...rest
}) => (
<Box
display="flex"
flexDirection="column"
gap={5}
position="relative"
backgroundColor="default1"
{...rest}
>
<Box display="flex" flexDirection="column" gap={5} backgroundColor="default1" {...rest}>
{children}
</Box>
);
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import { TablePaginationWithContext } from "@dashboard/components/TablePaginatio
import { Customer, Customers } from "@dashboard/customers/types";
import { CustomerListUrlSortField } from "@dashboard/customers/urls";
import { PermissionEnum } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import { ListProps, SortPage } from "@dashboard/types";
import { Item } from "@glideapps/glide-data-grid";
import { Box } from "@saleor/macaw-ui-next";
@@ -137,11 +138,7 @@ export const CustomerListDatagrid = ({
onToggle={handlers.onToggle}
/>
)}
navigatorOpts={{
state: {
prevLocation: location,
},
}}
navigatorOpts={{ state: getPrevLocationState(location) }}
/>

<Box paddingX={6}>
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import { useEmptyColumn } from "@dashboard/components/Datagrid/hooks/useEmptyCol
import { TablePaginationWithContext } from "@dashboard/components/TablePagination";
import { DiscountListUrlSortField, discountUrl } from "@dashboard/discounts/discountsUrls";
import { PromotionFragment } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import { ListProps, SortPage } from "@dashboard/types";
import { Item } from "@glideapps/glide-data-grid";
import { Box } from "@saleor/macaw-ui-next";
@@ -121,6 +122,7 @@ export const DiscountListDatagrid = ({
onToggle={handlers.onToggle}
/>
)}
navigatorOpts={{ state: getPrevLocationState(location) }}
/>

<Box paddingX={6}>
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import {
discountUrl,
} from "@dashboard/discounts/discountsUrls";
import { PromotionFragment } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import useNavigator from "@dashboard/hooks/useNavigator";
import { commonMessages } from "@dashboard/intl";
import { FilterPresetsProps, PageListProps, SortPage } from "@dashboard/types";
@@ -50,9 +51,7 @@ const DiscountListPage: React.FC<DiscountListPageProps> = ({
const [isFilterPresetOpen, setFilterPresetOpen] = useState(false);
const handleRowClick = (id: string) => {
navigation(discountUrl(id), {
state: {
prevLocation: location,
},
state: getPrevLocationState(location),
});
};

Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import { TablePaginationWithContext } from "@dashboard/components/TablePaginatio
import { commonTooltipMessages } from "@dashboard/components/TooltipTableCellHeader/messages";
import { SaleListUrlSortField, saleUrl } from "@dashboard/discounts/urls";
import { SaleFragment } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import useLocale from "@dashboard/hooks/useLocale";
import { ChannelProps, ListProps, SortPage } from "@dashboard/types";
import { Item } from "@glideapps/glide-data-grid";
@@ -141,6 +142,7 @@ export const SaleListDatagrid = ({
onToggle={handlers.onToggle}
/>
)}
navigatorOpts={{ state: getPrevLocationState(location) }}
/>

<Box paddingX={6}>
5 changes: 2 additions & 3 deletions src/discounts/components/SaleListPage/SaleListPage.tsx
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import { FilterPresetsSelect } from "@dashboard/components/FilterPresetsSelect";
import { ListPageLayout } from "@dashboard/components/Layouts";
import { saleAddUrl, SaleListUrlSortField, saleUrl } from "@dashboard/discounts/urls";
import { SaleFragment } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import useNavigator from "@dashboard/hooks/useNavigator";
import { commonMessages } from "@dashboard/intl";
import {
@@ -60,9 +61,7 @@ const SaleListPage: React.FC<SaleListPageProps> = ({
const filterDependency = structure.find(getByName("channel"));
const handleRowClick = (id: string) => {
navigation(saleUrl(id), {
state: {
prevLocation: location,
},
state: getPrevLocationState(location),
});
};

Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import { commonTooltipMessages } from "@dashboard/components/TooltipTableCellHea
import { VoucherListUrlSortField, voucherUrl } from "@dashboard/discounts/urls";
import { canBeSorted } from "@dashboard/discounts/views/VoucherList/sort";
import { VoucherFragment } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import useLocale from "@dashboard/hooks/useLocale";
import useNavigator from "@dashboard/hooks/useNavigator";
import { ChannelProps, ListProps, SortPage } from "@dashboard/types";
@@ -80,9 +81,7 @@ export const VoucherListDatagrid = ({

if (rowData) {
navigate(voucherUrl(rowData.id), {
state: {
prevLocation: location,
},
state: getPrevLocationState(location),
});
}
},
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import {
import TablePagination from "@dashboard/components/TablePagination";
import { commonTooltipMessages } from "@dashboard/components/TooltipTableCellHeader/messages";
import { giftCardListUrl, giftCardUrl } from "@dashboard/giftCards/urls";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import useNavigator from "@dashboard/hooks/useNavigator";
import usePaginator from "@dashboard/hooks/usePaginator";
import { Item } from "@glideapps/glide-data-grid";
@@ -171,11 +172,7 @@ export const GiftCardsListDatagrid = () => {
onToggle={handlers.onToggle}
/>
)}
navigatorOpts={{
state: {
prevLocation: location,
},
}}
navigatorOpts={{ state: getPrevLocationState(location) }}
/>

<Box paddingX={6}>
11 changes: 10 additions & 1 deletion src/hooks/useBackLinkWithState.ts
Original file line number Diff line number Diff line change
@@ -2,12 +2,21 @@ import { useEffect, useState } from "react";
import { useLocation } from "react-router";
import urljoin from "url-join";

type LocationData = Pick<Location, "pathname" | "search">;

type LocationWithState = Location & {
state?: {
prevLocation?: Location;
prevLocation?: LocationData;
};
};

export const getPrevLocationState = (location: LocationData) => ({
prevLocation: {
pathname: location.pathname,
search: location.search,
},
});

const getPreviousUrl = (location: LocationWithState) => {
if (!location.state?.prevLocation) {
return null;
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import {
useDatagridChangeState,
} from "@dashboard/components/Datagrid/hooks/useDatagridChange";
import { TablePaginationWithContext } from "@dashboard/components/TablePagination";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import useLocale from "@dashboard/hooks/useLocale";
import { OrderDraft } from "@dashboard/orders/types";
import { OrderDraftListUrlSortField, orderUrl } from "@dashboard/orders/urls";
@@ -125,11 +126,7 @@ export const OrderDraftListDatagrid = ({
onToggle={handlers.onToggle}
/>
)}
navigatorOpts={{
state: {
prevLocation: location,
},
}}
navigatorOpts={{ state: getPrevLocationState(location) }}
/>

<Box paddingX={6}>
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import {
import { useEmptyColumn } from "@dashboard/components/Datagrid/hooks/useEmptyColumn";
import { TablePaginationWithContext } from "@dashboard/components/TablePagination";
import { OrderListQuery } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import { OrderListUrlSortField } from "@dashboard/orders/urls";
import { ListProps, RelayToFlat, SortPage } from "@dashboard/types";
import { Item } from "@glideapps/glide-data-grid";
@@ -129,11 +130,7 @@ export const OrderListDatagrid: React.FC<OrderListDatagridProps> = ({
)}
onRowClick={handleRowClick}
rowAnchor={handleRowAnchor}
navigatorOpts={{
state: {
prevLocation: location,
},
}}
navigatorOpts={{ state: getPrevLocationState(location) }}
/>

<Box paddingX={6}>
3 changes: 2 additions & 1 deletion src/pageTypes/components/PageTypeList/PageTypeList.tsx
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import TableHead from "@dashboard/components/TableHead";
import { TablePaginationWithContext } from "@dashboard/components/TablePagination";
import TableRowLink from "@dashboard/components/TableRowLink";
import { PageTypeFragment } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import { PageTypeListUrlSortField, pageTypeUrl } from "@dashboard/pageTypes/urls";
import { getArrowDirection } from "@dashboard/utils/sort";
import { TableBody, TableCell, TableFooter } from "@material-ui/core";
@@ -86,7 +87,7 @@ const PageTypeList: React.FC<PageTypeListProps> = props => {
pageType
? {
pathname: pageTypeUrl(pageType.id),
state: { prevLocation: location },
state: getPrevLocationState(location),
}
: undefined
}
5 changes: 2 additions & 3 deletions src/pages/components/PageListPage/PageListPage.tsx
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import { BulkDeleteButton } from "@dashboard/components/BulkDeleteButton";
import { DashboardCard } from "@dashboard/components/Card";
import { FilterPresetsSelect } from "@dashboard/components/FilterPresetsSelect";
import { ListPageLayout } from "@dashboard/components/Layouts";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import useNavigator from "@dashboard/hooks/useNavigator";
import { sectionNames } from "@dashboard/intl";
import { Pages } from "@dashboard/pages/types";
@@ -133,9 +134,7 @@ const PageListPage: React.FC<PageListPageProps> = ({
rowAnchor={pageUrl}
onRowClick={id =>
navigate(pageUrl(id), {
state: {
prevLocation: location,
},
state: getPrevLocationState(location),
})
}
/>
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import {
import { useEmptyColumn } from "@dashboard/components/Datagrid/hooks/useEmptyColumn";
import { TablePaginationWithContext } from "@dashboard/components/TablePagination";
import { PermissionGroupFragment } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import useNavigator from "@dashboard/hooks/useNavigator";
import {
permissionGroupDetailsUrl,
@@ -72,9 +73,7 @@ export const PermissionGroupListDatagrid = ({

if (rowData) {
navigate(permissionGroupDetailsUrl(rowData.id), {
state: {
prevLocation: location,
},
state: getPrevLocationState(location),
});
}
},
@@ -118,6 +117,7 @@ export const PermissionGroupListDatagrid = ({
onHeaderClicked={handleHeaderClick}
rowAnchor={handleRowAnchor}
recentlyAddedColumn={recentlyAddedColumn}
navigatorOpts={{ state: getPrevLocationState(location) }}
/>

<Box paddingX={6}>
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import TableHead from "@dashboard/components/TableHead";
import { TablePaginationWithContext } from "@dashboard/components/TablePagination";
import TableRowLink from "@dashboard/components/TableRowLink";
import { ProductTypeFragment } from "@dashboard/graphql";
import { getPrevLocationState } from "@dashboard/hooks/useBackLinkWithState";
import { ProductTypeListUrlSortField, productTypeUrl } from "@dashboard/productTypes/urls";
import { getArrowDirection } from "@dashboard/utils/sort";
import { TableBody, TableCell, TableFooter } from "@material-ui/core";
@@ -122,7 +123,7 @@ const ProductTypeList: React.FC<ProductTypeListProps> = props => {
productType
? {
pathname: productTypeUrl(productType.id),
state: { prevLocation: location },
state: getPrevLocationState(location),
}
: undefined
}
Loading