-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,120 @@ | ||
import React from "react"; | ||
import { AnchorButton, ButtonGroup, Tooltip } from "@blueprintjs/core"; | ||
import React, { useState } from "react"; | ||
import { | ||
AnchorButton, | ||
ButtonGroup, | ||
Elevation, | ||
Tooltip, | ||
Popover, | ||
PopoverPosition, | ||
Checkbox, | ||
Card, | ||
Button, | ||
Classes, | ||
} from "@blueprintjs/core"; | ||
import * as globals from "../../globals"; | ||
import styles from "./menubar.css"; | ||
|
||
import { storageGet, storageSet, KEYS } from "../util/localStorage"; | ||
|
||
const LOGIN_PROMPT_OFF = "off"; | ||
|
||
const Auth = React.memo((props) => { | ||
const [isPromptOpen, setIsPromptOpen] = useState(shouldShowPrompt()); | ||
|
||
const { auth, userinfo } = props; | ||
|
||
if (!auth || (auth && !auth.requires_client_login)) return null; | ||
const isAuthenticated = userinfo && userinfo.is_authenticated; | ||
|
||
if (shouldShowAuth()) return null; | ||
|
||
const AuthButton = () => { | ||
return ( | ||
<AnchorButton | ||
type="button" | ||
data-testid="auth-button" | ||
href={isAuthenticated ? auth.logout : auth.login} | ||
> | ||
{isAuthenticated ? "Log Out" : "Log In"} | ||
</AnchorButton> | ||
); | ||
}; | ||
|
||
return ( | ||
<ButtonGroup className={styles.menubarButton}> | ||
<Tooltip | ||
content="Log in to cellxgene" | ||
position="bottom" | ||
hoverOpenDelay={globals.tooltipHoverOpenDelay} | ||
> | ||
<AnchorButton | ||
type="button" | ||
data-testid="auth-button" | ||
disabled={false} | ||
href={!userinfo.is_authenticated ? auth.login : auth.logout} | ||
<div style={{ position: "relative" }}> | ||
<Tooltip | ||
content="Log in to cellxgene" | ||
position="bottom" | ||
hoverOpenDelay={globals.tooltipHoverOpenDelay} | ||
> | ||
<AuthButton /> | ||
</Tooltip> | ||
<Popover | ||
position={PopoverPosition.AUTO_END} | ||
isOpen={isPromptOpen} | ||
content={<PopoverContent setIsPromptOpen={setIsPromptOpen} />} | ||
onInteraction={setIsPromptOpen} | ||
> | ||
{!userinfo.is_authenticated ? "Log In" : "Log Out"} | ||
</AnchorButton> | ||
</Tooltip> | ||
<div style={{ position: "absolute" }} /> | ||
</Popover> | ||
</div> | ||
</ButtonGroup> | ||
); | ||
|
||
function shouldShowAuth() { | ||
return auth && auth.requires_client_login; | ||
} | ||
|
||
function shouldShowPrompt() { | ||
if (storageGet(KEYS.LOGIN_PROMPT) === LOGIN_PROMPT_OFF) return false; | ||
|
||
return shouldShowAuth && !isAuthenticated; | ||
} | ||
}); | ||
|
||
function PopoverContent({ setIsPromptOpen }) { | ||
const [isChecked, setIsChecked] = useState(false); | ||
|
||
function handleOKClick() { | ||
if (isChecked) { | ||
storageSet(KEYS.LOGIN_PROMPT, LOGIN_PROMPT_OFF); | ||
} | ||
|
||
setIsPromptOpen(false); | ||
} | ||
|
||
function handleCheckboxChange() { | ||
setIsChecked(!isChecked); | ||
} | ||
|
||
return ( | ||
<Card style={{ width: "500px" }} elevation={Elevation.TWO}> | ||
<p> | ||
Log in to create your own annotations. Please note, logging in later | ||
will refresh this page and you may lose progress. | ||
</p> | ||
<Checkbox | ||
style={{ width: "230px" }} | ||
checked={isChecked} | ||
onChange={handleCheckboxChange} | ||
> | ||
Do not show me this message again | ||
</Checkbox> | ||
<div | ||
style={{ display: "flex", justifyContent: "flex-end", marginTop: 15 }} | ||
> | ||
<Button | ||
className={Classes.POPOVER_DISMISS} | ||
style={{ marginRight: "5px" }} | ||
> | ||
Cancel | ||
</Button> | ||
<Button onClick={handleOKClick} intent="primary"> | ||
OK | ||
</Button> | ||
</div> | ||
</Card> | ||
); | ||
} | ||
|
||
export default Auth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export const KEYS = { | ||
COOKIE_DECISION: "cxg.cookieDecision", | ||
LOGIN_PROMPT: "cxg.LOGIN_PROMPT", | ||
}; | ||
|
||
export function storageGet(key, defaultValue = null) { | ||
try { | ||
const val = window.localStorage.getItem(key); | ||
if (val === null) return defaultValue; | ||
return val; | ||
} catch (e) { | ||
return defaultValue; | ||
} | ||
} | ||
|
||
export function storageSet(key, value) { | ||
try { | ||
window.localStorage.setItem(key, value); | ||
} catch { | ||
// continue | ||
} | ||
} |