107 changed files with 1776 additions and 1141 deletions
@ -0,0 +1,2 @@ |
|||||
|
dist/build.tar |
||||
|
dist/output |
||||
@ -1,4 +1,9 @@ |
|||||
FROM registry.access.redhat.com/ubi9/nginx-122:1-45 |
FROM nginx:1.27.2-alpine |
||||
|
|
||||
|
RUN rm -r /usr/share/nginx/html \ |
||||
|
&& mkdir /usr/share/nginx/html |
||||
|
|
||||
|
WORKDIR /usr/share/nginx/html |
||||
|
|
||||
ADD dist . |
ADD dist . |
||||
|
|
||||
|
|||||
@ -5,11 +5,12 @@ |
|||||
"description": "Meshtastic web client", |
"description": "Meshtastic web client", |
||||
"license": "GPL-3.0-only", |
"license": "GPL-3.0-only", |
||||
"scripts": { |
"scripts": { |
||||
"dev": "vite --host", |
"build": "rsbuild build", |
||||
"build": "tsc && pnpm check && vite build ", |
|
||||
"check": "biome check .", |
"check": "biome check .", |
||||
"check:fix": "pnpm check --write", |
"check:fix": "pnpm check --write", |
||||
"preview": "vite preview", |
"dev": "rsbuild dev --open", |
||||
|
"format": "biome format --write", |
||||
|
"preview": "rsbuild preview", |
||||
"package": "gzipper c -i html,js,css,png,ico,svg,webmanifest,txt dist dist/output && tar -cvf dist/build.tar -C ./dist/output/ $(ls ./dist/output/)" |
"package": "gzipper c -i html,js,css,png,ico,svg,webmanifest,txt dist dist/output && tar -cvf dist/build.tar -C ./dist/output/ $(ls ./dist/output/)" |
||||
}, |
}, |
||||
"repository": { |
"repository": { |
||||
@ -48,6 +49,7 @@ |
|||||
"cmdk": "^1.0.0", |
"cmdk": "^1.0.0", |
||||
"crypto-random-string": "^5.0.0", |
"crypto-random-string": "^5.0.0", |
||||
"immer": "^10.1.1", |
"immer": "^10.1.1", |
||||
|
"js-cookie": "^3.0.5", |
||||
"lucide-react": "^0.363.0", |
"lucide-react": "^0.363.0", |
||||
"mapbox-gl": "^3.6.0", |
"mapbox-gl": "^3.6.0", |
||||
"maplibre-gl": "4.1.2", |
"maplibre-gl": "4.1.2", |
||||
@ -66,13 +68,15 @@ |
|||||
"devDependencies": { |
"devDependencies": { |
||||
"@biomejs/biome": "^1.8.2", |
"@biomejs/biome": "^1.8.2", |
||||
"@buf/meshtastic_protobufs.bufbuild_es": "1.10.0-20240906232734-3da561588c55.1", |
"@buf/meshtastic_protobufs.bufbuild_es": "1.10.0-20240906232734-3da561588c55.1", |
||||
|
"@rsbuild/core": "^1.0.10", |
||||
|
"@rsbuild/plugin-react": "^1.0.3", |
||||
"@types/chrome": "^0.0.263", |
"@types/chrome": "^0.0.263", |
||||
|
"@types/js-cookie": "^3.0.6", |
||||
"@types/node": "^20.14.9", |
"@types/node": "^20.14.9", |
||||
"@types/react": "^18.3.3", |
"@types/react": "^18.3.3", |
||||
"@types/react-dom": "^18.3.0", |
"@types/react-dom": "^18.3.0", |
||||
"@types/w3c-web-serial": "^1.0.6", |
"@types/w3c-web-serial": "^1.0.6", |
||||
"@types/web-bluetooth": "^0.0.20", |
"@types/web-bluetooth": "^0.0.20", |
||||
"@vitejs/plugin-react": "^4.3.1", |
|
||||
"autoprefixer": "^10.4.19", |
"autoprefixer": "^10.4.19", |
||||
"gzipper": "^7.2.0", |
"gzipper": "^7.2.0", |
||||
"postcss": "^8.4.38", |
"postcss": "^8.4.38", |
||||
@ -80,8 +84,7 @@ |
|||||
"tailwindcss": "^3.4.4", |
"tailwindcss": "^3.4.4", |
||||
"tar": "^6.2.1", |
"tar": "^6.2.1", |
||||
"tslib": "^2.6.3", |
"tslib": "^2.6.3", |
||||
"typescript": "^5.5.2", |
"typescript": "^5.5.2" |
||||
"vite": "^5.3.1", |
}, |
||||
"vite-plugin-environment": "^1.1.3" |
"packageManager": "[email protected]" |
||||
} |
|
||||
} |
} |
||||
|
|||||
File diff suppressed because it is too large
@ -0,0 +1,30 @@ |
|||||
|
import { defineConfig } from "@rsbuild/core"; |
||||
|
import { pluginReact } from "@rsbuild/plugin-react"; |
||||
|
import { execSync } from "node:child_process"; |
||||
|
|
||||
|
let hash = ""; |
||||
|
|
||||
|
try { |
||||
|
hash = execSync("git rev-parse --short HEAD").toString().trim(); |
||||
|
} catch (error) { |
||||
|
hash = "DEV"; |
||||
|
} |
||||
|
|
||||
|
export default defineConfig({ |
||||
|
plugins: [pluginReact()], |
||||
|
source: { |
||||
|
define: { |
||||
|
"process.env.COMMIT_HASH": JSON.stringify(hash), |
||||
|
}, |
||||
|
alias: { |
||||
|
"@app": "./src", |
||||
|
"@pages": "./src/pages", |
||||
|
"@components": "./src/components", |
||||
|
"@core": "./src/core", |
||||
|
"@layouts": "./src/layouts", |
||||
|
}, |
||||
|
}, |
||||
|
html: { |
||||
|
title: "Meshtastic Web", |
||||
|
}, |
||||
|
}); |
||||
@ -0,0 +1,134 @@ |
|||||
|
import { useDevice } from "@app/core/stores/deviceStore"; |
||||
|
import { Button } from "@components/UI/Button"; |
||||
|
import { |
||||
|
Dialog, |
||||
|
DialogContent, |
||||
|
DialogDescription, |
||||
|
DialogFooter, |
||||
|
DialogHeader, |
||||
|
DialogTitle, |
||||
|
} from "@components/UI/Dialog.tsx"; |
||||
|
import { fromByteArray } from "base64-js"; |
||||
|
import { DownloadIcon, PrinterIcon } from "lucide-react"; |
||||
|
import React from "react"; |
||||
|
|
||||
|
export interface PkiBackupDialogProps { |
||||
|
open: boolean; |
||||
|
onOpenChange: (open: boolean) => void; |
||||
|
} |
||||
|
|
||||
|
export const PkiBackupDialog = ({ |
||||
|
open, |
||||
|
onOpenChange, |
||||
|
}: PkiBackupDialogProps) => { |
||||
|
const { config, setDialogOpen } = useDevice(); |
||||
|
const privateKey = config.security?.privateKey; |
||||
|
const publicKey = config.security?.publicKey; |
||||
|
|
||||
|
const decodeKeyData = React.useCallback( |
||||
|
(key: Uint8Array<ArrayBufferLike>) => { |
||||
|
if (!key) return ""; |
||||
|
return fromByteArray(key ?? new Uint8Array(0)); |
||||
|
}, |
||||
|
[], |
||||
|
); |
||||
|
|
||||
|
const closeDialog = React.useCallback(() => { |
||||
|
setDialogOpen("pkiBackup", false); |
||||
|
}, [setDialogOpen]); |
||||
|
|
||||
|
const renderPrintWindow = React.useCallback(() => { |
||||
|
if (!privateKey || !publicKey) return; |
||||
|
|
||||
|
const printWindow = window.open("", "_blank"); |
||||
|
if (printWindow) { |
||||
|
printWindow.document.write(` |
||||
|
<html> |
||||
|
<head> |
||||
|
<title>=== MESHTASTIC KEYS ===</title> |
||||
|
<style> |
||||
|
body { font-family: Arial, sans-serif; padding: 20px; } |
||||
|
h1 { font-size: 18px; } |
||||
|
p { font-size: 14px; word-break: break-all; } |
||||
|
</style> |
||||
|
</head> |
||||
|
<body> |
||||
|
<h1>=== MESHTASTIC KEYS ===</h1> |
||||
|
<br> |
||||
|
<h2>Public Key:</h2> |
||||
|
<p>${decodeKeyData(publicKey)}</p> |
||||
|
<h2>Private Key:</h2> |
||||
|
<p>${decodeKeyData(privateKey)}</p> |
||||
|
<br> |
||||
|
<p>=== END OF KEYS ===</p> |
||||
|
</body> |
||||
|
</html> |
||||
|
`);
|
||||
|
printWindow.document.close(); |
||||
|
printWindow.print(); |
||||
|
closeDialog(); |
||||
|
} |
||||
|
}, [decodeKeyData, privateKey, publicKey, closeDialog]); |
||||
|
|
||||
|
const createDownloadKeyFile = React.useCallback(() => { |
||||
|
if (!privateKey || !publicKey) return; |
||||
|
|
||||
|
const decodedPrivateKey = decodeKeyData(privateKey); |
||||
|
const decodedPublicKey = decodeKeyData(publicKey); |
||||
|
|
||||
|
const formattedContent = [ |
||||
|
"=== MESHTASTIC KEYS ===\n\n", |
||||
|
"Private Key:\n", |
||||
|
decodedPrivateKey, |
||||
|
"\n\nPublic Key:\n", |
||||
|
decodedPublicKey, |
||||
|
"\n\n=== END OF KEYS ===", |
||||
|
].join(""); |
||||
|
|
||||
|
const blob = new Blob([formattedContent], { type: "text/plain" }); |
||||
|
const url = URL.createObjectURL(blob); |
||||
|
|
||||
|
const link = document.createElement("a"); |
||||
|
link.href = url; |
||||
|
link.download = "meshtastic_keys.txt"; |
||||
|
link.style.display = "none"; |
||||
|
document.body.appendChild(link); |
||||
|
link.click(); |
||||
|
document.body.removeChild(link); |
||||
|
closeDialog(); |
||||
|
URL.revokeObjectURL(url); |
||||
|
}, [decodeKeyData, privateKey, publicKey, closeDialog]); |
||||
|
|
||||
|
return ( |
||||
|
<Dialog open={open} onOpenChange={onOpenChange}> |
||||
|
<DialogContent> |
||||
|
<DialogHeader> |
||||
|
<DialogTitle>Backup Keys</DialogTitle> |
||||
|
<DialogDescription> |
||||
|
Its important to backup your public and private keys and store your |
||||
|
backup securely! |
||||
|
</DialogDescription> |
||||
|
<DialogDescription> |
||||
|
<span className="font-bold break-before-auto"> |
||||
|
If you lose your keys, you will need to reset your device. |
||||
|
</span> |
||||
|
</DialogDescription> |
||||
|
</DialogHeader> |
||||
|
<DialogFooter className="mt-6"> |
||||
|
<Button |
||||
|
variant={"default"} |
||||
|
onClick={() => createDownloadKeyFile()} |
||||
|
className="" |
||||
|
> |
||||
|
<DownloadIcon size={20} className="mr-2" /> |
||||
|
Download |
||||
|
</Button> |
||||
|
<Button variant={"default"} onClick={() => renderPrintWindow()}> |
||||
|
<PrinterIcon size={20} className="mr-2" /> |
||||
|
Print |
||||
|
</Button> |
||||
|
</DialogFooter> |
||||
|
</DialogContent> |
||||
|
</Dialog> |
||||
|
); |
||||
|
}; |
||||
@ -0,0 +1,19 @@ |
|||||
|
import { useBackupReminder } from "@app/core/hooks/useKeyBackupReminder"; |
||||
|
import { useDevice } from "@app/core/stores/deviceStore"; |
||||
|
|
||||
|
export const KeyBackupReminder = (): JSX.Element => { |
||||
|
const { setDialogOpen } = useDevice(); |
||||
|
|
||||
|
useBackupReminder({ |
||||
|
reminderInDays: 7, |
||||
|
message: |
||||
|
"We recommend backing up your key data regularly. Would you like to back up now?", |
||||
|
onAccept: () => setDialogOpen("pkiBackup", true), |
||||
|
enabled: true, |
||||
|
cookieOptions: { |
||||
|
secure: true, |
||||
|
sameSite: "strict", |
||||
|
}, |
||||
|
}); |
||||
|
return <></>; |
||||
|
}; |
||||
@ -0,0 +1,171 @@ |
|||||
|
import { Mono } from "@components/generic/Mono.tsx"; |
||||
|
import { H5 } from "@app/components/UI/Typography/H5.tsx"; |
||||
|
import { Subtle } from "@app/components/UI/Typography/Subtle.tsx"; |
||||
|
import { Separator } from "@app/components/UI/Seperator"; |
||||
|
import { TimeAgo } from "@components/generic/Table/tmp/TimeAgo.tsx"; |
||||
|
import { Hashicon } from "@emeraldpay/hashicon-react"; |
||||
|
import { Protobuf } from "@meshtastic/js"; |
||||
|
import type { Protobuf as ProtobufType } from "@meshtastic/js"; |
||||
|
import { |
||||
|
BatteryChargingIcon, |
||||
|
BatteryFullIcon, |
||||
|
BatteryLowIcon, |
||||
|
BatteryMediumIcon, |
||||
|
Dot, |
||||
|
LockIcon, |
||||
|
LockOpenIcon, |
||||
|
MountainSnow, |
||||
|
Star, |
||||
|
} from "lucide-react"; |
||||
|
import { numberToHexUnpadded } from "@noble/curves/abstract/utils"; |
||||
|
|
||||
|
export interface NodeDetailProps { |
||||
|
node: ProtobufType.Mesh.NodeInfo; |
||||
|
} |
||||
|
|
||||
|
export const NodeDetail = ({ node }: NodeDetailProps): JSX.Element => { |
||||
|
const name = node.user?.longName || `!${numberToHexUnpadded(node.num)}`; |
||||
|
const hardwareType = Protobuf.Mesh.HardwareModel[ |
||||
|
node.user?.hwModel ?? 0 |
||||
|
].replaceAll("_", " "); |
||||
|
|
||||
|
return ( |
||||
|
<div className="dark:text-black"> |
||||
|
<div className="flex gap-2"> |
||||
|
<div className="flex flex-col items-center gap-2 min-w-6 pt-1"> |
||||
|
<Hashicon value={node.num.toString()} size={22} /> |
||||
|
|
||||
|
<div> |
||||
|
{node.user?.publicKey && node.user?.publicKey.length > 0 ? ( |
||||
|
<LockIcon |
||||
|
className="text-green-600" |
||||
|
size={12} |
||||
|
strokeWidth={3} |
||||
|
aria-label="Public Key Enabled" |
||||
|
/> |
||||
|
) : ( |
||||
|
<LockOpenIcon |
||||
|
className="text-yellow-500" |
||||
|
size={12} |
||||
|
strokeWidth={3} |
||||
|
aria-label="No Public Key" |
||||
|
/> |
||||
|
)} |
||||
|
</div> |
||||
|
|
||||
|
<Star |
||||
|
fill={node.isFavorite ? "black" : "none"} |
||||
|
size={15} |
||||
|
aria-label={node.isFavorite ? "Favorite" : "Not a Favorite"} |
||||
|
/> |
||||
|
</div> |
||||
|
|
||||
|
<div> |
||||
|
<H5>{name}</H5> |
||||
|
|
||||
|
{hardwareType !== "UNSET" && <Subtle>{hardwareType}</Subtle>} |
||||
|
|
||||
|
{!!node.deviceMetrics?.batteryLevel && ( |
||||
|
<div |
||||
|
className="flex items-center gap-1" |
||||
|
title={`${ |
||||
|
node.deviceMetrics?.voltage?.toPrecision(3) ?? "Unknown" |
||||
|
} volts`}
|
||||
|
> |
||||
|
{node.deviceMetrics?.batteryLevel > 100 ? ( |
||||
|
<BatteryChargingIcon size={22} /> |
||||
|
) : node.deviceMetrics?.batteryLevel > 80 ? ( |
||||
|
<BatteryFullIcon size={22} /> |
||||
|
) : node.deviceMetrics?.batteryLevel > 20 ? ( |
||||
|
<BatteryMediumIcon size={22} /> |
||||
|
) : ( |
||||
|
<BatteryLowIcon size={22} /> |
||||
|
)} |
||||
|
<Subtle aria-label="Battery"> |
||||
|
{node.deviceMetrics?.batteryLevel > 100 |
||||
|
? "Charging" |
||||
|
: node.deviceMetrics?.batteryLevel + "%"} |
||||
|
</Subtle> |
||||
|
</div> |
||||
|
)} |
||||
|
|
||||
|
<div className="flex gap-2 items-center"> |
||||
|
{node.user?.shortName && <div>"{node.user?.shortName}"</div>} |
||||
|
{node.user?.id && <div>{node.user?.id}</div>} |
||||
|
</div> |
||||
|
|
||||
|
<div |
||||
|
className="flex gap-1" |
||||
|
title={new Date(node.lastHeard * 1000).toLocaleString( |
||||
|
navigator.language, |
||||
|
)} |
||||
|
> |
||||
|
<div> |
||||
|
{node.lastHeard > 0 && ( |
||||
|
<div> |
||||
|
Heard <TimeAgo timestamp={node.lastHeard * 1000} /> |
||||
|
</div> |
||||
|
)} |
||||
|
</div> |
||||
|
{node.viaMqtt && ( |
||||
|
<div style={{ color: "#660066" }} className="font-medium"> |
||||
|
MQTT |
||||
|
</div> |
||||
|
)} |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
|
||||
|
<Separator className="my-1" /> |
||||
|
|
||||
|
<div className="flex mt-2 text-sm"> |
||||
|
<div className="flex items-center flex-grow"> |
||||
|
<div className="border-2 border-black rounded px-0.5 mr-1"> |
||||
|
{isNaN(node.hopsAway) ? "?" : node.hopsAway} |
||||
|
</div> |
||||
|
<div>{node.hopsAway === 1 ? "Hop" : "Hops"}</div> |
||||
|
</div> |
||||
|
{node.position?.altitude && ( |
||||
|
<div className="flex items-center flex-grow"> |
||||
|
<MountainSnow |
||||
|
size={15} |
||||
|
className="ml-2 mr-1" |
||||
|
aria-label="Elevation" |
||||
|
/> |
||||
|
<div>{node.position?.altitude} ft</div> |
||||
|
</div> |
||||
|
)} |
||||
|
</div> |
||||
|
|
||||
|
<div className="flex mt-2"> |
||||
|
{!!node.deviceMetrics?.channelUtilization && ( |
||||
|
<div className="flex-grow"> |
||||
|
<div>Channel Util</div> |
||||
|
<Mono> |
||||
|
{node.deviceMetrics?.channelUtilization.toPrecision(3)}% |
||||
|
</Mono> |
||||
|
</div> |
||||
|
)} |
||||
|
{!!node.deviceMetrics?.airUtilTx && ( |
||||
|
<div className="flex-grow"> |
||||
|
<div>Airtime Util</div> |
||||
|
<Mono>{node.deviceMetrics?.airUtilTx.toPrecision(3)}%</Mono> |
||||
|
</div> |
||||
|
)} |
||||
|
</div> |
||||
|
|
||||
|
{node.snr !== 0 && ( |
||||
|
<div className="mt-2"> |
||||
|
<div>SNR</div> |
||||
|
<Mono className="flex items-center text-xs"> |
||||
|
{node.snr}db |
||||
|
<Dot /> |
||||
|
{Math.min(Math.max((node.snr + 10) * 5, 0), 100)}% |
||||
|
<Dot /> |
||||
|
{(node.snr + 10) * 5}raw |
||||
|
</Mono> |
||||
|
</div> |
||||
|
)} |
||||
|
</div> |
||||
|
); |
||||
|
}; |
||||
@ -0,0 +1,14 @@ |
|||||
|
import { cn } from "@app/core/utils/cn.ts"; |
||||
|
|
||||
|
export interface H5Props { |
||||
|
className?: string; |
||||
|
children: React.ReactNode; |
||||
|
} |
||||
|
|
||||
|
export const H5 = ({ className, children }: H5Props): JSX.Element => ( |
||||
|
<h5 |
||||
|
className={cn("scroll-m-20 text-lg font-medium tracking-tight", className)} |
||||
|
> |
||||
|
{children} |
||||
|
</h5> |
||||
|
); |
||||
@ -0,0 +1,29 @@ |
|||||
|
import { useMemo } from 'react'; |
||||
|
|
||||
|
export type BrowserFeature = 'Web Bluetooth' | 'Web Serial' | 'Secure Context'; |
||||
|
|
||||
|
interface BrowserSupport { |
||||
|
supported: BrowserFeature[]; |
||||
|
unsupported: BrowserFeature[]; |
||||
|
} |
||||
|
|
||||
|
export function useBrowserFeatureDetection(): BrowserSupport { |
||||
|
const support = useMemo(() => { |
||||
|
const features: [BrowserFeature, boolean][] = [ |
||||
|
['Web Bluetooth', !!navigator?.bluetooth], |
||||
|
['Web Serial', !!navigator?.serial], |
||||
|
['Secure Context', window.location.protocol === 'https:' || window.location.hostname === 'localhost'] |
||||
|
]; |
||||
|
|
||||
|
return features.reduce<BrowserSupport>( |
||||
|
(acc, [feature, isSupported]) => { |
||||
|
const list = isSupported ? acc.supported : acc.unsupported; |
||||
|
list.push(feature); |
||||
|
return acc; |
||||
|
}, |
||||
|
{ supported: [], unsupported: [] } |
||||
|
); |
||||
|
}, []); |
||||
|
|
||||
|
return support; |
||||
|
} |
||||
@ -0,0 +1,52 @@ |
|||||
|
import Cookies, { type CookieAttributes } from "js-cookie"; |
||||
|
import { useCallback, useState } from "react"; |
||||
|
|
||||
|
interface CookieHookResult<T> { |
||||
|
value: T | undefined; |
||||
|
setCookie: (value: T, options?: CookieAttributes) => void; |
||||
|
removeCookie: () => void; |
||||
|
} |
||||
|
|
||||
|
function useCookie<T extends object>( |
||||
|
cookieName: string, |
||||
|
initialValue?: T, |
||||
|
): CookieHookResult<T> { |
||||
|
const [cookieValue, setCookieValue] = useState<T | undefined>(() => { |
||||
|
try { |
||||
|
const cookie = Cookies.get(cookieName); |
||||
|
return cookie ? (JSON.parse(cookie) as T) : initialValue; |
||||
|
} catch (error) { |
||||
|
console.error(`Error parsing cookie ${cookieName}:`, error); |
||||
|
return initialValue; |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
const setCookie = useCallback( |
||||
|
(value: T, options?: CookieAttributes) => { |
||||
|
try { |
||||
|
Cookies.set(cookieName, JSON.stringify(value), options); |
||||
|
setCookieValue(value); |
||||
|
} catch (error) { |
||||
|
console.error(`Error setting cookie ${cookieName}:`, error); |
||||
|
} |
||||
|
}, |
||||
|
[cookieName], |
||||
|
); |
||||
|
|
||||
|
const removeCookie = useCallback(() => { |
||||
|
try { |
||||
|
Cookies.remove(cookieName); |
||||
|
setCookieValue(undefined); |
||||
|
} catch (error) { |
||||
|
console.error(`Error removing cookie ${cookieName}:`, error); |
||||
|
} |
||||
|
}, [cookieName]); |
||||
|
|
||||
|
return { |
||||
|
value: cookieValue, |
||||
|
setCookie, |
||||
|
removeCookie, |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
export default useCookie; |
||||
@ -0,0 +1,120 @@ |
|||||
|
import { Button } from "@app/components/UI/Button"; |
||||
|
import type { CookieAttributes } from "js-cookie"; |
||||
|
import { useCallback, useEffect, useRef } from "react"; |
||||
|
import useCookie from "./useCookie"; |
||||
|
import { useToast } from "./useToast"; |
||||
|
|
||||
|
interface UseBackupReminderOptions { |
||||
|
reminderInDays?: number; |
||||
|
message: string; |
||||
|
onAccept?: () => void | Promise<void>; |
||||
|
enabled: boolean; |
||||
|
cookieOptions?: CookieAttributes; |
||||
|
} |
||||
|
|
||||
|
interface ReminderState { |
||||
|
suppressed: boolean; |
||||
|
lastShown: string; |
||||
|
} |
||||
|
|
||||
|
const TOAST_APPEAR_DELAY = 10_000 // 10 seconds;
|
||||
|
const TOAST_DURATION = 30_000 // 30 seconds;:
|
||||
|
|
||||
|
// remind user in 1 year to backup keys again, if they accept the reminder;
|
||||
|
const ON_ACCEPT_REMINDER_DAYS = 365 |
||||
|
|
||||
|
function isReminderExpired(lastShown: string): boolean { |
||||
|
const lastShownDate = new Date(lastShown); |
||||
|
const now = new Date(); |
||||
|
const daysSinceLastShown = |
||||
|
(now.getTime() - lastShownDate.getTime()) / (1000 * 60 * 60 * 24); |
||||
|
return daysSinceLastShown >= 7; |
||||
|
} |
||||
|
|
||||
|
export function useBackupReminder({ |
||||
|
reminderInDays = 7, |
||||
|
enabled, |
||||
|
message, |
||||
|
onAccept = () => { }, |
||||
|
cookieOptions, |
||||
|
}: UseBackupReminderOptions) { |
||||
|
const { toast } = useToast(); |
||||
|
const toastShownRef = useRef(false); |
||||
|
const { value: reminderCookie, setCookie } = |
||||
|
useCookie<ReminderState>("key_backup_reminder"); |
||||
|
|
||||
|
const suppressReminder = useCallback( |
||||
|
(days: number) => { |
||||
|
const expiryDate = new Date(); |
||||
|
expiryDate.setDate(expiryDate.getDate() + days); |
||||
|
|
||||
|
setCookie( |
||||
|
{ |
||||
|
suppressed: true, |
||||
|
lastShown: new Date().toISOString(), |
||||
|
}, |
||||
|
{ ...cookieOptions, expires: expiryDate }, |
||||
|
); |
||||
|
}, |
||||
|
[setCookie, cookieOptions], |
||||
|
); |
||||
|
|
||||
|
useEffect(() => { |
||||
|
if (!enabled || toastShownRef.current) return; |
||||
|
|
||||
|
const shouldShowReminder = |
||||
|
!reminderCookie?.suppressed || |
||||
|
isReminderExpired(reminderCookie.lastShown); |
||||
|
if (!shouldShowReminder) return; |
||||
|
|
||||
|
toastShownRef.current = true; |
||||
|
|
||||
|
const { dismiss } = toast( |
||||
|
{ |
||||
|
title: "Backup Reminder", |
||||
|
duration: TOAST_DURATION, |
||||
|
delay: TOAST_APPEAR_DELAY, |
||||
|
description: message, |
||||
|
action: ( |
||||
|
<div className="flex gap-2"> |
||||
|
<Button |
||||
|
type="button" |
||||
|
variant="default" |
||||
|
onClick={() => { |
||||
|
onAccept(); |
||||
|
dismiss(); |
||||
|
suppressReminder(ON_ACCEPT_REMINDER_DAYS); |
||||
|
}} |
||||
|
> |
||||
|
Back up now |
||||
|
</Button> |
||||
|
<Button |
||||
|
type="button" |
||||
|
variant="outline" |
||||
|
onClick={() => { |
||||
|
dismiss(); |
||||
|
suppressReminder(reminderInDays); |
||||
|
}} |
||||
|
> |
||||
|
Remind me in {reminderInDays} days |
||||
|
</Button> |
||||
|
</div> |
||||
|
), |
||||
|
}, |
||||
|
); |
||||
|
|
||||
|
return () => { |
||||
|
if (!toastShownRef.current) { |
||||
|
dismiss(); |
||||
|
} |
||||
|
}; |
||||
|
}, [ |
||||
|
enabled, |
||||
|
message, |
||||
|
onAccept, |
||||
|
reminderInDays, |
||||
|
suppressReminder, |
||||
|
toast, |
||||
|
reminderCookie, |
||||
|
]); |
||||
|
} |
||||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue