Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 34 additions & 12 deletions packages/web-console/src/scenes/Editor/Menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ import React, { useCallback, useEffect, useState } from "react"
import { useDispatch, useSelector } from "react-redux"
import { CSSTransition } from "react-transition-group"
import styled from "styled-components"
import { Add } from "styled-icons/remix-line"
import { Close as _CloseIcon } from "styled-icons/remix-line"
import {
Add,
Close as _CloseIcon,
Command,
Database2,
Play,
Stop,
} from "styled-icons/remix-line"
import { Menu as _MenuIcon } from "styled-icons/remix-fill"
import { Play } from "styled-icons/remix-line"
import { Stop } from "styled-icons/remix-line"
import { Database2 } from "styled-icons/remix-line"
import { HelpCircle } from "styled-icons/boxicons-regular"
import { Slack } from "styled-icons/boxicons-logos"

Expand All @@ -55,6 +58,7 @@ import { actions, selectors } from "../../../store"
import { color } from "../../../utils"

import QueryPicker from "../QueryPicker"
import { Shortcuts } from "../Shortcuts"
import { useLocalStorage } from "../../../providers/LocalStorageProvider"
import { StoreKey } from "../../../utils/localStorage/types"

Expand Down Expand Up @@ -165,7 +169,8 @@ const MenuLink: React.FunctionComponent<{

const Menu = () => {
const dispatch = useDispatch()
const [popperActive, setPopperActive] = useState<boolean>()
const [queriesPopperActive, setQueriesPopperActive] = useState<boolean>()
const [shortcutsPopperActive, setShortcutsPopperActive] = useState<boolean>()
const escPress = useKeyPress("Escape")
const { savedQueries } = useSelector(selectors.console.getConfig)
const running = useSelector(selectors.query.getRunning)
Expand All @@ -177,14 +182,17 @@ const Menu = () => {
const handleClick = useCallback(() => {
dispatch(actions.query.toggleRunning())
}, [dispatch])
const handleToggle = useCallback((active) => {
const handleQueriesToggle = useCallback((active) => {
if (!exampleQueriesVisited && active) {
updateSettings(StoreKey.EXAMPLE_QUERIES_VISITED, true)
}
setPopperActive(active)
setQueriesPopperActive(active)
}, [])
const handleShortcutsToggle = useCallback((active) => {
setShortcutsPopperActive(active)
}, [])
const handleHidePicker = useCallback(() => {
setPopperActive(false)
setQueriesPopperActive(false)
}, [])
const handleSideMenuButtonClick = useCallback(() => {
dispatch(actions.console.toggleSideMenu())
Expand All @@ -194,7 +202,7 @@ const Menu = () => {
}, [])

useEffect(() => {
setPopperActive(false)
setQueriesPopperActive(false)
}, [escPress])

useEffect(() => {
Expand Down Expand Up @@ -255,8 +263,8 @@ const Menu = () => {

{savedQueries.length > 0 && (
<PopperToggle
active={popperActive}
onToggle={handleToggle}
active={queriesPopperActive}
onToggle={handleQueriesToggle}
trigger={
<QueryPickerButton firstTimeVisitor={!exampleQueriesVisited}>
<Add size="18px" />
Expand All @@ -282,6 +290,20 @@ const Menu = () => {
tooltipText="Go to Web Console help"
/>

<PopperToggle
active={shortcutsPopperActive}
onToggle={handleShortcutsToggle}
trigger={
<MenuButton>
<Command size="18px" />
<span>Shortcuts</span>
</MenuButton>
}
placement="bottom"
>
<Shortcuts />
</PopperToggle>

<DocsearchInput
id="docsearch-input"
placeholder="Search documentation"
Expand Down
159 changes: 159 additions & 0 deletions packages/web-console/src/scenes/Editor/Shortcuts/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import React from "react"
import styled from "styled-components"
import { color } from "../../../utils"
import { Text } from "../../../components"
import { platform } from "../../../utils"

type ShortcutsList = { keys: string[][]; title: string }[]
Comment thread
argshook marked this conversation as resolved.
Resolved

const Wrapper = styled.div`
display: flex;
max-height: 650px;
width: 300px;
max-width: 100vw;
flex-direction: column;
background: ${color("draculaBackgroundDarker")};
box-shadow: ${color("black")} 0px 5px 8px;
border: 1px solid ${color("black")};
border-radius: 4px;
overflow: auto;
`
const List = styled.div`
display: flex;
flex-direction: column;
width: 100%;
`

const ListTitle = styled(Text)`
padding: 0.6rem 1.2rem;
margin-top: 0.6rem;
border-top: 1px solid ${({ theme }) => theme.color.draculaSelection};
background: ${({ theme }) => theme.color.blackAlpha40};
width: 100%;
`

const Item = styled.div`
display: flex;
align-items: center;
padding: 0.6rem 1.2rem;

&:not(:last-child) {
border-bottom: 1px solid ${color("draculaBackground")};
}
`

const ItemKeys = styled.div`
margin-left: auto;
`

const KeyGroup = styled.span`
color: ${color("gray2")};

&:not(:last-child):after {
content: "or";
padding: 0 0.5rem;
}
`

const Key = styled.span`
padding: 0 4px;
background: ${color("gray2")};
border-radius: 2px;
color: ${color("black")};

&:not(:last-child) {
margin-right: 0.25rem;
}
`

const ctrlCmd = platform.isMacintosh || platform.isIOS ? "⌘" : "Ctrl"
const altOption = platform.isMacintosh || platform.isIOS ? "⌥" : "Alt"

const editorList: ShortcutsList = [
{
keys: [[altOption, ctrlCmd, "↑"]],
title: "Add cursor above",
},
{
keys: [[altOption, ctrlCmd, "↓"]],
title: "Add cursor below",
},
{
keys: [["⇧", altOption, "↑"]],
title: "Copy line up",
},
{
keys: [["⇧", altOption, "↓"]],
title: "Copy line down",
},
{
keys: [[altOption, "↑"]],
title: "Move line up",
},
{
keys: [[altOption, "↓"]],
title: "Move line down",
},
{
keys: [["⇧", altOption, "F"]],
title: "Format document",
},
{
keys: [["F1"]],
title: "Command palette",
},
]

const globalList: ShortcutsList = [
{
keys: [["F9"], [ctrlCmd, "Enter"]],
title: "Run query",
},
{
keys: [["F2"]],
title: "Focus results grid",
},
{
keys: [[ctrlCmd, "K"]],
title: "Clear all notifications",
},
]

const ShortcutsGroup = ({
list,
title,
}: {
list: ShortcutsList
title: string
}) => (
<List>
<ListTitle>
<Text color="gray2">{title}</Text>
</ListTitle>
{list.map((shortcutItem, index) => (
<Item key={`shortcut-${index}`}>
<Text color="white">{shortcutItem.title}</Text>
<ItemKeys>
{shortcutItem.keys.map((keyGroup, index) => (
<KeyGroup key={`keyGroup-${index}`}>
{keyGroup.map((key, index) => (
<Key key={`shortcutItem-key-${key}-${index}`}>
<Text color="black" size="xs" weight={600}>
{key}
</Text>
</Key>
))}
</KeyGroup>
))}
</ItemKeys>
</Item>
))}
</List>
)

export const Shortcuts = () => (
<Wrapper>
<ShortcutsGroup list={globalList} title="Global shortcuts" />
<ShortcutsGroup list={editorList} title="SQL editor shortcuts" />
</Wrapper>
)
1 change: 1 addition & 0 deletions packages/web-console/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export * from "./fromFetch"
export * from "./questdb"
export * from "./styled"
export * from "./formatSql"
export * from "./platform"
24 changes: 24 additions & 0 deletions packages/web-console/src/utils/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type Platform = {
isMacintosh: boolean
isWindows: boolean
isIOS: boolean
isLinux: boolean
}

export const platform: Platform = {
isMacintosh: false,
isWindows: false,
isIOS: false,
isLinux: false,
}

if (typeof navigator === "object") {
platform.isMacintosh = navigator.userAgent.indexOf("Macintosh") !== -1
platform.isWindows = navigator.userAgent.indexOf("Windows") >= 0
platform.isIOS =
(navigator.userAgent.indexOf("Macintosh") >= 0 ||
navigator.userAgent.indexOf("iPad") >= 0 ||
navigator.userAgent.indexOf("iPhone") >= 0) &&
!!navigator.maxTouchPoints &&
navigator.maxTouchPoints > 0
}