Skip to content

Commit

Permalink
Merge branch 'main' of github.com:iforge-uos/ignis
Browse files Browse the repository at this point in the history
  • Loading branch information
Sampiiiii committed Apr 15, 2024
2 parents 5f04454 + 641b2de commit 0c2b268
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 78 deletions.
117 changes: 39 additions & 78 deletions apps/forge/src/components/command-menu/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useUser } from "@/lib/utils";
import { RootState } from "@/redux/store";
import { useNavigate } from "@tanstack/react-router";
import {
Expand All @@ -11,13 +12,7 @@ import {
CommandSeparator,
CommandShortcut,
} from "@ui/components/ui/command";
import {
UserRoundSearch,
LayoutDashboard,
LogIn,
LogOut,
Settings,
} from "lucide-react";
import { LayoutDashboard, LogIn, LogOut, Settings, UserRound, UserRoundSearch } from "lucide-react";
import React, { ReactElement } from "react";
import { useSelector } from "react-redux";

Expand All @@ -39,104 +34,70 @@ export default function CommandMenu() {
return () => document.removeEventListener("keydown", down);
}, [isMacOs]);

const SETTINGS_SHORTCUTS: Record<string, [() => any, string, ReactElement]> =
{
// p: [
// () => navigate({ to: "/user/profile" }),
// "Profile",
// <UserRound className="mr-2 h-4 w-4" />,
// ],
s: [
() => navigate({ to: "/user/settings" }),
"Settings",
<Settings className="mr-2 h-4 w-4" />,
],
};
const SETTINGS_SHORTCUTS: Record<string, [() => any, string, ReactElement]> = {
p: [() => navigate({ to: "/user/profile" }), "Profile", <UserRound className="mr-2 h-4 w-4" />],
s: [() => navigate({ to: "/user/settings" }), "Settings", <Settings className="mr-2 h-4 w-4" />],
};

const USER_MANAGEMENT_SHORTCUTS: Record<
string,
[() => any, string, ReactElement]
> = {
d: [
() => navigate({ to: "/signin/dashboard" }),
"Dashboard",
<LayoutDashboard className="mr-2 h-4 w-4" />,
],
u: [
() => navigate({ to: "/users" }),
"Search users",
<UserRoundSearch className="mr-2 h-4 w-4" />,
],
i: [
() => navigate({ to: "/signin/actions" }),
"Sign in",
<LogIn className="mr-2 h-4 w-4" />,
],
o: [
() => navigate({ to: "/signin/actions" }),
"Sign out",
<LogOut className="mr-2 h-4 w-4" />,
],
const USER_MANAGEMENT_SHORTCUTS: Record<string, [() => any, string, ReactElement]> = {
d: [() => navigate({ to: "/signin/dashboard" }), "Dashboard", <LayoutDashboard className="mr-2 h-4 w-4" />],
u: [() => navigate({ to: "/users" }), "Search users", <UserRoundSearch className="mr-2 h-4 w-4" />],
i: [() => navigate({ to: "/signin/actions" }), "Sign in", <LogIn className="mr-2 h-4 w-4" />],
o: [() => navigate({ to: "/signin/actions" }), "Sign out", <LogOut className="mr-2 h-4 w-4" />],
};

const SHORTCUTS = { ...SETTINGS_SHORTCUTS, ...USER_MANAGEMENT_SHORTCUTS };

React.useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (isMacOs ? e.metaKey : e.ctrlKey) {
e.preventDefault();
SHORTCUTS[e.key]?.[0]();
const shortcut = SHORTCUTS[e.key];
if (shortcut !== undefined) {
e.preventDefault();
shortcut[0]();
}
}
};

document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, [navigate, isMacOs]);
}, []);

const groups = new Map<string, typeof SHORTCUTS>();

if (useSelector((state: RootState) => state.auth.is_authenticated)) {
groups.set("Settings", SETTINGS_SHORTCUTS);
}

// if (useSelector((state: RootState) => state.auth.is_authenticated)) {
// groups.set("User Management", USER_MANAGEMENT_SHORTCUTS);
// }
if (useUser()?.roles.some((role) => role.name === "Rep")) {
groups.set("User Management", USER_MANAGEMENT_SHORTCUTS);
}

// XXX this doesn't work for some reason
return (
<Command className="rounded-lg border shadow-md">
<CommandDialog open={open} onOpenChange={setOpen}>
<CommandInput placeholder="Type a command or search..." />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
{Object.entries(groups).flatMap(
([name, shortcuts]: [string, typeof SHORTCUTS], index, array) => {
const group = (
<CommandGroup heading={name}>
{Object.entries(shortcuts).map(
([key, [callback, name, icon]]) => {
return (
<CommandItem onSelect={callback}>
{icon}
<span>{name}</span>
<CommandShortcut>
{metaKey}
{key.toUpperCase()}
</CommandShortcut>
</CommandItem>
);
}
)}
</CommandGroup>
);
// console.log(group);

return index < array.length - 1
? [group, <CommandSeparator />]
: [group];
}
)}
{[...groups].flatMap(([name, shortcuts]: [string, typeof SHORTCUTS], index, array) => {
const group = (
<CommandGroup heading={name}>
{Object.entries(shortcuts).map(([key, [callback, name, icon]]) => {
return (
<CommandItem onSelect={callback}>
{icon}
<span>{name}</span>
<CommandShortcut>
{metaKey}
{key.toUpperCase()}
</CommandShortcut>
</CommandItem>
);
})}
</CommandGroup>
);
return index < array.length - 1 ? [group, <CommandSeparator />] : [group];
})}
</CommandList>
</CommandDialog>
</Command>
Expand Down
2 changes: 2 additions & 0 deletions apps/forge/src/components/signin/actions/UCardInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const UCardInput: FlowStepComponent = ({ onPrimary }) => {
onChange={(value) => handleOtpChange(value)}
onComplete={() => handleOnSubmit()}
ref={firstInputRef}
type="search" // I think this turns off 1password autocomplete which gets in the way of our actual auto-complete
autoComplete="off" // so does this
>
<InputOTPGroup>
<InputOTPSlot index={0} />
Expand Down

0 comments on commit 0c2b268

Please sign in to comment.