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.

feat: add submodules and recent searches to search

Brad Barbin9f120884547b
diff --git a/apps/carbon/app/components/Search/Search/Search.tsx b/apps/carbon/app/components/Search/Search/Search.tsx
index 8bbfe45ec4..127656ab6b 100644
--- a/apps/carbon/app/components/Search/Search/Search.tsx
+++ b/apps/carbon/app/components/Search/Search/Search.tsx
@@ -6,7 +6,6 @@ import {
CommandInput,
CommandItem,
CommandList,
- CommandLoading,
CommandSeparator,
HStack,
Kbd,
@@ -15,8 +14,10 @@ import {
useDebounce,
useDisclosure,
useKeyboardShortcuts,
+ useMount,
} from "@carbon/react";
import { useNavigate } from "@remix-run/react";
+import idb from "localforage";
import { useCallback, useEffect, useState } from "react";
import { AiOutlinePartition } from "react-icons/ai";
import { BiListCheck } from "react-icons/bi";
@@ -27,6 +28,17 @@ import { PiShareNetworkFill } from "react-icons/pi";
import { RxMagnifyingGlass } from "react-icons/rx";
import { useSidebar } from "~/components/Layout/Sidebar/useSidebar";
import { useSupabase } from "~/lib/supabase";
+import { useAccountSidebar } from "~/modules/account";
+import { useAccountingSidebar } from "~/modules/accounting";
+import { useDocumentsSidebar } from "~/modules/documents";
+import { useInventorySidebar } from "~/modules/inventory";
+import { useInvoicingSidebar } from "~/modules/invoicing";
+import { usePartsSidebar } from "~/modules/parts";
+import { usePurchasingSidebar } from "~/modules/purchasing";
+import { useSalesSidebar } from "~/modules/sales";
+import { useSettingsSidebar } from "~/modules/settings";
+import { useUsersSidebar } from "~/modules/users";
+import type { Authenticated, Route } from "~/types";
type SearchResult = {
id: number;
@@ -61,7 +73,23 @@ const SearchModal = ({
const [input, setInput] = useState("");
const [debouncedInput] = useDebounce(input, 500);
- const moduleResults = useSidebar();
+ useEffect(() => {
+ if (isOpen) {
+ setInput("");
+ }
+ }, [isOpen]);
+
+ const staticResults = useGroupedSubmodules();
+
+ const [recentResults, setRecentResults] = useState<Route[]>([]);
+ useMount(async () => {
+ const recentResultsFromStorage = await idb.getItem<Route[]>(
+ "recentSearches"
+ );
+ if (recentResultsFromStorage) {
+ setRecentResults(recentResultsFromStorage);
+ }
+ });
const [searchResults, setSearchResults] = useState<SearchResult[]>([]);
@@ -102,9 +130,19 @@ const SearchModal = ({
setInput(value);
};
- const onSelect = (location: string) => {
- navigate(location);
+ const onSelect = async (route: Route) => {
+ const { to, name } = route;
+ navigate(route.to);
onClose();
+ const newRecentSearches = [
+ { to, name },
+ ...((await idb.getItem<Route[]>("recentSearches"))?.filter(
+ (item) => item.to !== to
+ ) ?? []),
+ ].slice(0, 5);
+
+ setRecentResults(newRecentSearches);
+ idb.setItem("recentSearches", newRecentSearches);
};
return (
@@ -123,28 +161,58 @@ const SearchModal = ({
onValueChange={onInputChange}
/>
<CommandList>
- {loading && <CommandLoading />}
- <CommandEmpty>No results found.</CommandEmpty>
-
- <CommandGroup heading="Modules">
- {moduleResults.map((module) => (
- <CommandItem
- key={module.name}
- onSelect={() => onSelect(module.to)}
- >
- <module.icon className="mr-2 w-4 h-4" />
- <span>{module.name}</span>
- </CommandItem>
- ))}
- </CommandGroup>
- <CommandSeparator />
+ <CommandEmpty>
+ {loading ? "Loading..." : "No results found."}
+ </CommandEmpty>
+ {recentResults.length > 0 && (
+ <>
+ <CommandGroup heading="Recent Searches">
+ {recentResults.map((result) => (
+ <CommandItem
+ key={result.to}
+ onSelect={() => onSelect(result)}
+ // append with : so we're not sharing a value with a static result
+ value={`:${result.to}`}
+ >
+ <RxMagnifyingGlass className="w-4 h-4 mr-2 " />
+ {result.name}
+ </CommandItem>
+ ))}
+ </CommandGroup>
+ <CommandSeparator />
+ </>
+ )}
+ {Object.entries(staticResults).map(([module, submodules]) => (
+ <>
+ <CommandGroup heading={module} key={module}>
+ {submodules.map((submodule) => (
+ <CommandItem
+ key={`${submodule.to}-${submodule.name}`}
+ onSelect={() => onSelect(submodule)}
+ value={`${module} ${submodule.name}`}
+ >
+ {submodule.icon && (
+ <submodule.icon className="w-4 h-4 mr-2 " />
+ )}
+ <span>{submodule.name}</span>
+ </CommandItem>
+ ))}
+ </CommandGroup>
+ <CommandSeparator />
+ </>
+ ))}
{searchResults.length > 0 && (
<CommandGroup heading="Search Results">
{searchResults.map((result) => (
<CommandItem
key={result.id}
value={`${input}${result.id}`}
- onSelect={() => onSelect(result.link)}
+ onSelect={() =>
+ onSelect({
+ to: result.link,
+ name: result.name,
+ })
+ }
>
<ResultIcon entity={result.entity} />
<span>{result.name}</span>
@@ -195,7 +263,7 @@ const SearchButton = () => {
<Button
leftIcon={<RxMagnifyingGlass />}
variant="secondary"
- className=" w-[200px] px-2"
+ className="font-normal w-[200px] px-2"
onClick={searchModal.onOpen}
>
<HStack className="w-full">
@@ -208,4 +276,78 @@ const SearchButton = () => {
);
};
+function useGroupedSubmodules() {
+ const modules = useSidebar();
+ const parts = usePartsSidebar();
+ // const jobs = useJobsSidebar();
+ const inventory = useInventorySidebar();
+ // const scheduling = useSchedulingSidebar();
+ // const timecards = useTimecardsSidebar();
+ const sales = useSalesSidebar();
+ const purchasing = usePurchasingSidebar();
+ const documents = useDocumentsSidebar();
+ // const messages = useMessagesSidebar();
+ const accounting = useAccountingSidebar();
+ const invoicing = useInvoicingSidebar();
+ const users = useUsersSidebar();
+ const settings = useSettingsSidebar();
+ const account = useAccountSidebar();
+
+ const groupedSubmodules: Record<
+ string,
+ {
+ groups: {
+ routes: Authenticated<Route>[];
+ name: string;
+ icon?: any;
+ }[];
+ }
+ > = {
+ parts,
+ inventory,
+ sales,
+ purchasing,
+ accounting,
+ invoicing,
+ users,
+ settings,
+ };
+
+ const ungroupedSubmodules: Record<string, { links: Route[] }> = {
+ documents,
+ account,
+ };
+
+ const shortcuts = modules.reduce<Record<string, Route[]>>((acc, module) => {
+ const moduleName = module.name.toLowerCase();
+
+ if (moduleName in groupedSubmodules) {
+ const groups = groupedSubmodules[moduleName].groups;
+ acc = {
+ ...acc,
+ [module.name]: groups.flatMap((group) =>
+ group.routes.map((route) => ({
+ to: route.to,
+ name: route.name,
+ icon: module.icon,
+ }))
+ ),
+ };
+ } else if (moduleName in ungroupedSubmodules || moduleName === "account") {
+ acc = {
+ ...acc,
+ [module.name]: ungroupedSubmodules[moduleName].links.map((link) => ({
+ to: link.to,
+ name: link.name,
+ icon: module.icon,
+ })),
+ };
+ }
+
+ return acc;
+ }, {});
+
+ return shortcuts;
+}
+
export default SearchButton;