Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

style: activate links optimitically

Brad Barbinc0e9b8f6c6ad
diff --git a/apps/carbon/app/components/Layout/Navigation/ContentSidebar.tsx b/apps/carbon/app/components/Layout/Navigation/ContentSidebar.tsx
index 1f424be2af..2078baebe8 100644
--- a/apps/carbon/app/components/Layout/Navigation/ContentSidebar.tsx
+++ b/apps/carbon/app/components/Layout/Navigation/ContentSidebar.tsx
@@ -1,11 +1,11 @@
import { Button, VStack } from "@carbon/react";
-import { Link, useMatches } from "@remix-run/react";
-import { useUrlParams } from "~/hooks";
+import { Link } from "@remix-run/react";
+import { useOptimisticLocation, useUrlParams } from "~/hooks";
import type { Route } from "~/types";
import { CollapsibleSidebar } from "./CollapsibleSidebar";
const ContentSidebar = ({ links }: { links: Route[] }) => {
- const matches = useMatches();
+ const location = useOptimisticLocation();
const [params] = useUrlParams();
const filter = params.get("q") ?? undefined;
@@ -15,10 +15,8 @@ const ContentSidebar = ({ links }: { links: Route[] }) => {
<VStack>
<VStack spacing={1} className="border-b border-border p-2">
{links.map((route) => {
- const isActive = matches.some(
- (match) =>
- match.pathname.includes(route.to) && route.q === filter
- );
+ const isActive =
+ route.to === location.pathname && route.q === filter;
return (
<Button
key={route.name}
diff --git a/apps/carbon/app/components/Layout/Navigation/DetailSidebar.tsx b/apps/carbon/app/components/Layout/Navigation/DetailSidebar.tsx
index b916c4b3a7..66b00a2d7d 100644
--- a/apps/carbon/app/components/Layout/Navigation/DetailSidebar.tsx
+++ b/apps/carbon/app/components/Layout/Navigation/DetailSidebar.tsx
@@ -9,8 +9,9 @@ import {
useKeyboardShortcuts,
} from "@carbon/react";
import { prettifyKeyboardShortcut } from "@carbon/utils";
-import { Link, useMatches, useNavigate } from "@remix-run/react";
+import { Link, useNavigate } from "@remix-run/react";
import type { IconType } from "react-icons";
+import { useOptimisticLocation } from "~/hooks";
type DetailSidebarProps = {
links: {
@@ -23,8 +24,8 @@ type DetailSidebarProps = {
};
const DetailSidebar = ({ links }: DetailSidebarProps) => {
- const matches = useMatches();
const navigate = useNavigate();
+ const location = useOptimisticLocation();
useKeyboardShortcuts(
links.reduce<Record<string, () => void>>((acc, link) => {
@@ -38,11 +39,7 @@ const DetailSidebar = ({ links }: DetailSidebarProps) => {
return (
<VStack className="overflow-y-auto h-full" spacing={1}>
{links.map((route) => {
- const isActive = matches.some(
- (match) =>
- (match.pathname.includes(route.to) && route.to !== "") ||
- (match.id.includes(".index") && route.to === "")
- );
+ const isActive = route.to === location.pathname;
return (
<Tooltip key={route.name}>
diff --git a/apps/carbon/app/components/Layout/Navigation/GroupedContentSidebar.tsx b/apps/carbon/app/components/Layout/Navigation/GroupedContentSidebar.tsx
index e91b60e984..ffc6ffb9c0 100644
--- a/apps/carbon/app/components/Layout/Navigation/GroupedContentSidebar.tsx
+++ b/apps/carbon/app/components/Layout/Navigation/GroupedContentSidebar.tsx
@@ -1,10 +1,11 @@
import { Button, VStack } from "@carbon/react";
-import { Link, useMatches } from "@remix-run/react";
+import { Link } from "@remix-run/react";
+import { useOptimisticLocation } from "~/hooks";
import type { RouteGroup } from "~/types";
import { CollapsibleSidebar } from "./CollapsibleSidebar";
const GroupedContentSidebar = ({ groups }: { groups: RouteGroup[] }) => {
- const matches = useMatches();
+ const location = useOptimisticLocation();
return (
<CollapsibleSidebar>
@@ -20,9 +21,7 @@ const GroupedContentSidebar = ({ groups }: { groups: RouteGroup[] }) => {
{group.name}
</h4>
{group.routes.map((route) => {
- const isActive = matches.some((match) =>
- match.pathname.includes(route.to)
- );
+ const isActive = route.to === location.pathname;
return (
<Button
key={route.name}
diff --git a/apps/carbon/app/components/Layout/Navigation/IconSidebar.tsx b/apps/carbon/app/components/Layout/Navigation/IconSidebar.tsx
index e7d04ac20b..0d40d7c215 100644
--- a/apps/carbon/app/components/Layout/Navigation/IconSidebar.tsx
+++ b/apps/carbon/app/components/Layout/Navigation/IconSidebar.tsx
@@ -4,6 +4,7 @@ import { noop } from "@tanstack/react-table";
import { forwardRef, type AnchorHTMLAttributes } from "react";
import { BsFillHexagonFill } from "react-icons/bs";
import { z } from "zod";
+import { useOptimisticLocation } from "~/hooks";
import type { Authenticated, NavItem } from "~/types";
import { useModules } from "./useModules";
@@ -13,6 +14,8 @@ export const ModuleHandle = z.object({
const IconSidebar = () => {
const navigationPanel = useDisclosure();
+ const location = useOptimisticLocation();
+ const currentModule = getModule(location.pathname);
const links = useModules();
const matchedModules = useMatches().reduce((acc, match) => {
if (match.handle) {
@@ -46,9 +49,15 @@ const IconSidebar = () => {
</Button>
{links.map((link) => {
- const module = link.to.split("/")[2]; // link.to is "/x/parts" -- this returns "parts"
+ const m = getModule(link.to);
+ const moduleMatches = matchedModules.has(m);
+ const anotherModuleMatches = links.some((l) => {
+ const m = getModule(l.to);
+ return matchedModules.has(m);
+ });
- const isActive = matchedModules.has(module);
+ const isActive =
+ currentModule === m || (moduleMatches && !anotherModuleMatches);
return (
<NavigationIconLink
key={link.name}
@@ -130,3 +139,7 @@ const NavigationIconLink = forwardRef<
NavigationIconLink.displayName = "NavigationIconLink";
export default IconSidebar;
+
+function getModule(link: string) {
+ return link.split("/")?.[2];
+}
diff --git a/apps/carbon/app/hooks/index.ts b/apps/carbon/app/hooks/index.ts
index 9988303e8c..d398ae2a0d 100644
--- a/apps/carbon/app/hooks/index.ts
+++ b/apps/carbon/app/hooks/index.ts
@@ -1,6 +1,7 @@
import { useMovingCellRef } from "./useMovingCellRef";
import { useNanoStore } from "./useNanoStore";
import { useOnboarding } from "./useOnboarding";
+import { useOptimisticLocation } from "./useOptimisticLocation";
import { usePermissions } from "./usePermissions";
import { useRealtime } from "./useRealtime";
import { useRouteData } from "./useRouteData";
@@ -11,6 +12,7 @@ export {
useMovingCellRef,
useNanoStore,
useOnboarding,
+ useOptimisticLocation,
usePermissions,
useRealtime,
useRouteData,
diff --git a/apps/carbon/app/hooks/useOptimisticLocation.ts b/apps/carbon/app/hooks/useOptimisticLocation.ts
new file mode 100644
index 0000000000..84eb784fa9
--- /dev/null
+++ b/apps/carbon/app/hooks/useOptimisticLocation.ts
@@ -0,0 +1,13 @@
+// credit: Matt Aitken at trigger.dev
+import { useLocation, useNavigation } from "@remix-run/react";
+
+export function useOptimisticLocation() {
+ const navigation = useNavigation();
+ const location = useLocation();
+
+ if (navigation.state === "idle" || !navigation.location) {
+ return location;
+ }
+
+ return navigation.location;
+}
diff --git a/apps/carbon/app/modules/invoicing/ui/PurchaseInvoices/PurchaseInvoicesTable.tsx b/apps/carbon/app/modules/invoicing/ui/PurchaseInvoices/PurchaseInvoicesTable.tsx
index 86803342ee..c1ca5792fa 100644
--- a/apps/carbon/app/modules/invoicing/ui/PurchaseInvoices/PurchaseInvoicesTable.tsx
+++ b/apps/carbon/app/modules/invoicing/ui/PurchaseInvoices/PurchaseInvoicesTable.tsx
@@ -35,8 +35,6 @@ const PurchaseInvoicesTable = memo(
`id=in.(${data.map((d) => d.id).join(",")})`
);
- console.log({ data });
-
const permissions = usePermissions();
const navigate = useNavigate();
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx b/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx
index e07f8381dc..bc8f8c74ba 100644
--- a/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx
@@ -28,6 +28,7 @@ import { HiOutlineCube } from "react-icons/hi";
import { IoMdAdd } from "react-icons/io";
import { LuClock } from "react-icons/lu";
import { RxCheck, RxChevronDown } from "react-icons/rx";
+import { useOptimisticLocation } from "~/hooks";
import type { BillOfMaterialNode } from "~/modules/shared";
import { path } from "~/utils/path";
import type {
@@ -233,7 +234,7 @@ const QuotationExplorerItem = (
};
const QuotationExplorer = () => {
- const { pathname, search } = useLocation();
+ const { pathname, search } = useOptimisticLocation();
const params = useParams();
if (!params.id) throw new Error("id not found");