-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to notify user when processing is complete
- Loading branch information
Showing
6 changed files
with
220 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
export type Permission = NotificationPermission | PermissionState; | ||
export type RequestPermission = () => Promise<boolean>; | ||
|
||
export function useNotification(): { | ||
permission: Permission; | ||
requested: boolean; | ||
requestPermission: RequestPermission; | ||
supported: boolean; | ||
} { | ||
const [permission, setPermission] = useState<Permission>(Notification.permission); | ||
|
||
useEffect(() => { | ||
let permissionStatus: PermissionStatus; | ||
|
||
const onChange = () => { | ||
if (permissionStatus) { | ||
setPermission(permissionStatus.state); | ||
} | ||
}; | ||
|
||
(async () => { | ||
permissionStatus = await navigator.permissions.query({ name: "notifications" }); | ||
permissionStatus.addEventListener("change", onChange); | ||
})(); | ||
|
||
return () => { | ||
if (permissionStatus) { | ||
permissionStatus.removeEventListener("change", onChange); | ||
} | ||
}; | ||
}, []); | ||
|
||
const [requested, setRequested] = useState(false); | ||
|
||
const requestPermission = useCallback(async () => { | ||
if (!supported) { | ||
return false; | ||
} | ||
|
||
setRequested(true); | ||
|
||
let permission = Notification.permission; | ||
if (permission !== "granted") { | ||
permission = await Notification.requestPermission(); | ||
} | ||
|
||
return permission === "granted"; | ||
}, []); | ||
|
||
return { | ||
permission, | ||
requestPermission, | ||
requested, | ||
supported, | ||
}; | ||
} | ||
|
||
const supported = typeof window !== "undefined" && "Notification" in window; |
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,19 @@ | ||
.SecondaryMessage { | ||
color: var(--color-dim); | ||
} | ||
|
||
.NotifyMessage { | ||
border: 1px solid var(--checkbox-border); | ||
padding: 0.5rem 01rem; | ||
border-radius: 2rem; | ||
font-size: var(--font-size-regular); | ||
cursor: pointer; | ||
} | ||
.NotifyMessage[data-disabled] { | ||
opacity: 0.5; | ||
cursor: not-allowed; | ||
} | ||
|
||
.Checkbox { | ||
border-radius: 0.25rem; | ||
} |
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,55 +1,36 @@ | ||
.loadingScreenWrapper { | ||
.LoadingScreen { | ||
width: 100%; | ||
height: 100%; | ||
position: relative; | ||
display: flex; | ||
width: 24rem; | ||
flex-direction: column; | ||
} | ||
|
||
.hoverboardWrapper { | ||
height: 8rem; | ||
width: 8rem; | ||
cursor: pointer; | ||
} | ||
|
||
.messageWrapper { | ||
font-size: 0.875rem; | ||
align-items: center; | ||
text-align: center; | ||
padding: 1rem; | ||
gap: 1rem; | ||
font-size: 0.875rem; | ||
} | ||
|
||
.messageWrapper a { | ||
.LoadingScreen a { | ||
text-decoration: underline; | ||
} | ||
|
||
.messageWrapper a:hover { | ||
.LoadingScreen a:hover { | ||
color: var(--primary-accent); | ||
} | ||
|
||
.message { | ||
margin-bottom: 1rem; | ||
} | ||
|
||
.secondaryMessage { | ||
color: var(--color-dim); | ||
width: 18rem; | ||
} | ||
|
||
.viewportWrapper { | ||
position: relative; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
padding: 1rem; | ||
margin: 0.5rem; | ||
border-radius: 0.5rem; | ||
.Hoverboard { | ||
height: 8rem; | ||
width: 8rem; | ||
} | ||
|
||
.HighRiskWarning { | ||
width: 40ch; | ||
padding: 1rem 2rem; | ||
border-radius: 1rem; | ||
margin: 3rem 0; | ||
padding: 0.5rem; | ||
border-radius: 0.5rem; | ||
text-align: center; | ||
font-size: var(--font-size-regular); | ||
background-color: var(--background-color-high-risk-setting); | ||
color: var(--color-high-risk-setting); | ||
} | ||
|
||
.Spacer { | ||
flex-grow: 1; | ||
} |
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,86 +1,65 @@ | ||
import dynamic from "next/dynamic"; | ||
import { ReactNode, useCallback, useEffect, useState } from "react"; | ||
import { ConnectedProps, connect } from "react-redux"; | ||
import { ReactNode, useEffect, useState } from "react"; | ||
|
||
import { useHighRiskSettingCount } from "shared/user-data/GraphQL/useHighRiskSettingCount"; | ||
import { RecordingDocumentTitle } from "ui/components/RecordingDocumentTitle"; | ||
import { getAwaitingSourcemaps, getUploading } from "ui/reducers/app"; | ||
import { UIState } from "ui/state"; | ||
import { useAppSelector } from "ui/setup/hooks"; | ||
|
||
import { DefaultViewportWrapper } from "./Viewport"; | ||
import styles from "./LoadingScreen.module.css"; | ||
|
||
const colorOptions: Array<"blue" | "green" | "red"> = ["blue", "green", "red"]; | ||
export default function LoadingScreen({ message }: { message: ReactNode }) { | ||
const isAwaitingSourceMaps = useAppSelector(getAwaitingSourcemaps); | ||
const uploadingInfo = useAppSelector(getUploading); | ||
|
||
const Hoverboard = dynamic(() => import("./Hoverboard"), { | ||
ssr: false, | ||
loading: () => <div />, | ||
}); | ||
const showHighRiskWarning = useHighRiskSettingCount() > 0; | ||
|
||
export function LoadingScreenTemplate({ children }: { children?: ReactNode }) { | ||
const [hoverboardColor, setHoverboardColor] = useState(colorOptions[2]); | ||
|
||
const changeHoverboardColor = useCallback(() => { | ||
const randomIndex = Math.floor(Math.random() * colorOptions.length); | ||
setHoverboardColor(colorOptions[randomIndex]); | ||
}, []); | ||
const [colorIndex, setColorIndex] = useState(0); | ||
const color = colorOptions[colorIndex % colorOptions.length]; | ||
|
||
useEffect(() => { | ||
const timeoutId = setInterval(changeHoverboardColor, 5000); | ||
return () => clearInterval(timeoutId); | ||
}, [changeHoverboardColor]); | ||
const timeoutId = setInterval(() => { | ||
setColorIndex(prevIndex => prevIndex + 1); | ||
}, 5_000); | ||
|
||
return ( | ||
<div className={styles.loadingScreenWrapper}> | ||
<DefaultViewportWrapper> | ||
<div className={styles.loadingScreenWrapper}> | ||
<div className="flex flex-col items-center space-y-2"> | ||
<div className={styles.hoverboardWrapper} onClick={changeHoverboardColor}> | ||
<Hoverboard color={hoverboardColor} /> | ||
</div> | ||
{children} | ||
</div> | ||
</div> | ||
</DefaultViewportWrapper> | ||
</div> | ||
); | ||
} | ||
return () => clearInterval(timeoutId); | ||
}, []); | ||
|
||
function LoadingScreen({ | ||
uploading, | ||
awaitingSourcemaps, | ||
message, | ||
secondaryMessage, | ||
}: PropsFromRedux & { message: string; secondaryMessage?: string }) { | ||
const waitingForMessage = | ||
awaitingSourcemaps || uploading ? ( | ||
<span>Uploading {Math.round(uploading?.amount ? Number(uploading.amount) : 0)}Mb</span> | ||
) : ( | ||
<> | ||
<div className={styles.message} dangerouslySetInnerHTML={{ __html: message }} /> | ||
{secondaryMessage && <div className={styles.secondaryMessage}>{secondaryMessage}</div>} | ||
</> | ||
let content: ReactNode; | ||
if (isAwaitingSourceMaps || uploadingInfo) { | ||
content = ( | ||
<span> | ||
Uploading {Math.round(uploadingInfo?.amount ? Number(uploadingInfo.amount) : 0)}Mb | ||
</span> | ||
); | ||
|
||
const showHighRiskWarning = useHighRiskSettingCount() > 0; | ||
} else { | ||
content = message; | ||
} | ||
|
||
return ( | ||
<LoadingScreenTemplate> | ||
<> | ||
<RecordingDocumentTitle /> | ||
<div className={styles.messageWrapper}>{waitingForMessage}</div> | ||
{showHighRiskWarning && ( | ||
<div className={styles.HighRiskWarning}> | ||
You have advanced settings enabled that may negatively affect performance | ||
<DefaultViewportWrapper className={styles.LoadingScreen}> | ||
<div className={styles.Spacer}></div> | ||
<div className={styles.Hoverboard}> | ||
<Hoverboard color={color} /> | ||
</div> | ||
)} | ||
</LoadingScreenTemplate> | ||
{content} | ||
<div className={styles.Spacer}></div> | ||
{showHighRiskWarning && ( | ||
<div className={styles.HighRiskWarning}> | ||
You have advanced settings enabled that may negatively affect performance | ||
</div> | ||
)} | ||
</DefaultViewportWrapper> | ||
</> | ||
); | ||
} | ||
|
||
const connector = connect((state: UIState) => ({ | ||
uploading: getUploading(state), | ||
awaitingSourcemaps: getAwaitingSourcemaps(state), | ||
})); | ||
type PropsFromRedux = ConnectedProps<typeof connector>; | ||
const colorOptions: Array<"blue" | "green" | "red"> = ["blue", "green", "red"]; | ||
|
||
export default connector(LoadingScreen); | ||
const Hoverboard = dynamic(() => import("./Hoverboard"), { | ||
ssr: false, | ||
loading: () => <div />, | ||
}); |
Oops, something went wrong.