Skip to content

Commit

Permalink
feat: fast account switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
hallettj committed May 24, 2019
1 parent 87a7b8f commit 440a72e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
52 changes: 52 additions & 0 deletions packages/client/src/AccountSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Button, Menu, MenuItem } from "@material-ui/core"
import { ButtonProps } from "@material-ui/core/Button"
import { Link } from "@reach/router"
import * as React from "react"
import * as graphql from "./generated/graphql"

type Props = ButtonProps & { accountId: string }

export default function AccountSwitcher({ accountId, ...rest }: Props) {
const [anchorEl, setAnchorEl] = React.useState<HTMLElement | null>(null)
const { data } = graphql.useGetAllAccountsQuery()
if (!data || !data.accounts) {
return null
}
console.log("anchorEl", anchorEl)
const handleClose = () => setAnchorEl(null)
const { accounts } = data
const current = accounts.find(account => account.id === accountId)
const email = current && current.email
return (
<>
<Button
aria-owns={anchorEl ? "account-switcher-menu" : undefined}
aria-haspopup="true"
onClick={event => setAnchorEl(event.currentTarget)}
{...rest}
>
{email || "Accounts"}
</Button>
<Menu
id="account-switcher-menu"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={handleClose}
>
{accounts.map(account => (
<MenuItem
key={account.id}
component={Link}
to={`/accounts/${account.id}/dashboard`}
onClick={handleClose}
>
{account.email}
</MenuItem>
))}
<MenuItem component={Link} to="/accounts">
Manage Accounts
</MenuItem>
</Menu>
</>
)
}
5 changes: 2 additions & 3 deletions packages/client/src/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import moment from "moment"
import * as React from "react"
import * as graphql from "./generated/graphql"
import useSync from "./hooks/useSync"
import AccountSwitcher from "./AccountSwitcher"
import Avatar from "./Avatar"
import Participant from "./Participant"

Expand Down Expand Up @@ -162,9 +163,7 @@ export default function Dashboard({ accountId }: Props) {
>
Inbox
</Typography>
<Button color="inherit" href="/accounts">
{email}
</Button>
<AccountSwitcher accountId={accountId} color="inherit" />
<IconButton
color="inherit"
onClick={() => sync().catch(setError)}
Expand Down

0 comments on commit 440a72e

Please sign in to comment.