Skip to content

Commit

Permalink
Add shortcuts at bottom of sidebar (#4705)
Browse files Browse the repository at this point in the history
* Add shortcuts at bottom of sidebar

* Navigator context

* Style sidebar

* Remove menu back to cloud tests

* Add changeset

* Add condtion to back to cloud btn

* Style improvents, show cmd base on os

* Show playground on ctrl + '

* Test sidebar go to cloud and shortcuts

* Update changeset

* Improve naming

* Rename Navigator to NavigatorSearch

* Move context outside AppLayout

* Memoize shortcuts

* Update tests

* Fix typo

* Refactor dev panel
  • Loading branch information
poulch authored Mar 7, 2024
1 parent f845150 commit 5faad9e
Show file tree
Hide file tree
Showing 50 changed files with 750 additions and 331 deletions.
6 changes: 6 additions & 0 deletions .changeset/serious-beans-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"saleor-dashboard": minor
---

Introduce menu items with sortcuts for GraphQL playground and search actions in sidebar.
Move "Go to Saleor Cloud" button at bottom of sidebar
8 changes: 8 additions & 0 deletions locale/defaultMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,10 @@
"context": "window title",
"string": "Grant refund"
},
"Cn6l5R": {
"context": "playground shortcut",
"string": "Playground"
},
"Co2U4u": {
"string": "No plugins found"
},
Expand Down Expand Up @@ -6568,6 +6572,10 @@
"context": "swatch attribute type",
"string": "Swatch"
},
"gx6b6x": {
"context": "search shortcut",
"string": "Search"
},
"gxPjIQ": {
"string": "Are you sure you want to delete {email} from staff members?"
},
Expand Down
49 changes: 5 additions & 44 deletions src/components/AppLayout/AppLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import useAppState from "@dashboard/hooks/useAppState";
import { DevModeQuery } from "@dashboard/orders/queries";
import { getFilterVariables } from "@dashboard/orders/views/OrderList/filters";
import { LinearProgress } from "@material-ui/core";
import { useActionBar } from "@saleor/macaw-ui";
import { Box } from "@saleor/macaw-ui-next";
import React, { useState } from "react";
import { useLocation } from "react-router";
import React from "react";

import { DevModePanel } from "../DevModePanel/DevModePanel";
import { useDevModeContext } from "../DevModePanel/hooks";
import { useDevModeKeyTrigger } from "../DevModePanel/useDevModeKeyTrigger";
import Navigator from "../Navigator";
import NavigatorSearch from "../NavigatorSearch";
import { Sidebar } from "../Sidebar";
import { useStyles } from "./styles";
import { extractQueryParams } from "./util";

interface AppLayoutProps {
children: React.ReactNode;
Expand All @@ -24,45 +18,12 @@ const AppLayout: React.FC<AppLayoutProps> = ({ children }) => {
const classes = useStyles();
const { anchor: appActionAnchor } = useActionBar();
const [appState] = useAppState();
const [isNavigatorVisible, setNavigatorVisibility] = useState(false);

const {
isDevModeVisible,
setDevModeVisibility,
setDevModeContent,
setVariables,
} = useDevModeContext();

const params = extractQueryParams(useLocation().search);

useDevModeKeyTrigger((_err, { shift }) => {
if (shift) {
setDevModeContent(DevModeQuery);
const variables = JSON.stringify(
{
filter: getFilterVariables(params),
},
null,
2,
);
setVariables(variables);
} else {
setDevModeContent("");
setVariables("");
}
setDevModeVisibility(!isDevModeVisible);
});

return (
<>
<DevModePanel
isDevModeVisible={isDevModeVisible}
setDevModeVisibility={setDevModeVisibility}
/>
<Navigator
visible={isNavigatorVisible}
setVisibility={setNavigatorVisibility}
/>
<DevModePanel />
<NavigatorSearch />

<Box display="grid" __gridTemplateColumns="auto 1fr">
{appState.loading && (
<LinearProgress className={classes.appLoader} color="primary" />
Expand Down
19 changes: 6 additions & 13 deletions src/components/DevModePanel/DevModePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,18 @@ import { messages } from "./messages";

const authorizedFetch = createFetch();

interface DevModePanelProps {
isDevModeVisible: boolean;
setDevModeVisibility: (value: boolean) => void;
}
export const DevModePanel: React.FC = () => {
const intl = useIntl();
const { rootStyle } = useDashboardTheme();

const { isDevModeVisible, variables, devModeContent, setDevModeVisibility } =
useDevModeContext();

export const DevModePanel: React.FC<DevModePanelProps> = ({
isDevModeVisible,
setDevModeVisibility,
}) => {
const fetcher = createGraphiQLFetcher({
url: process.env.API_URI,
fetch: authorizedFetch,
});

const intl = useIntl();
const { rootStyle } = useDashboardTheme();

const { devModeContent, variables } = useDevModeContext();

const overwriteCodeMirrorCSSVariables = {
__html: `
.graphiql-container, .CodeMirror-info, .CodeMirror-lint-tooltip, reach-portal{
Expand Down
27 changes: 27 additions & 0 deletions src/components/DevModePanel/DevModeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// @ts-strict-ignore
import { DevModeQuery } from "@dashboard/orders/queries";
import { getFilterVariables } from "@dashboard/orders/views/OrderList/filters";
import React, { useState } from "react";
import { useLocation } from "react-router";

import { extractQueryParams } from "../AppLayout/util";
import { DevModeContext } from "./hooks";
import { useDevModeKeyTrigger } from "./useDevModeKeyTrigger";

export function DevModeProvider({ children }) {
// stringified variables (as key/value) passed along with the query
Expand All @@ -11,6 +16,28 @@ export function DevModeProvider({ children }) {
const [devModeContent, setDevModeContent] = useState("");
const [isDevModeVisible, setDevModeVisibility] = useState(false);

const params = extractQueryParams(useLocation().search);

const triggerHandler = ({ shift }: { shift: boolean }) => {
if (shift) {
setDevModeContent(DevModeQuery);
const variables = JSON.stringify(
{
filter: getFilterVariables(params),
},
null,
2,
);
setVariables(variables);
} else {
setDevModeContent("");
setVariables("");
}
setDevModeVisibility(!isDevModeVisible);
};

useDevModeKeyTrigger(triggerHandler);

return (
<DevModeContext.Provider
value={{
Expand Down
17 changes: 9 additions & 8 deletions src/components/DevModePanel/useDevModeKeyTrigger.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { useEffect } from "react";

type DevModeKeyTriggerCallback = (
err: Error | null,
{ shift }: { shift: boolean },
) => void;
type DevModeKeyTriggerCallback = ({ shift }: { shift: boolean }) => void;

export const useDevModeKeyTrigger = (callback: DevModeKeyTriggerCallback) => {
export const useDevModeKeyTrigger = (
callbackHandler: DevModeKeyTriggerCallback,
) => {
useEffect(() => {
const handler = (event: KeyboardEvent) => {
if (event.shiftKey && event.metaKey && event.code === "Quote") {
callback(null, { shift: true });
callbackHandler({ shift: true });
} else if (event.metaKey && event.code === "Quote") {
callback(null, { shift: false });
callbackHandler({ shift: false });
} else if (event.ctrlKey && event.code === "Quote") {
callbackHandler({ shift: false });
}
};

document.addEventListener("keydown", handler);
return () => document.removeEventListener("keydown", handler);
}, [callback]);
}, [callbackHandler]);
};
135 changes: 0 additions & 135 deletions src/components/Navigator/NavigatorInput.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/Navigator/index.ts

This file was deleted.

Loading

0 comments on commit 5faad9e

Please sign in to comment.