-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PWA Support - Service Worker + Offline Mode
- Loading branch information
Showing
8 changed files
with
502 additions
and
75 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>TrustyNotes - Offline</title> | ||
<style> | ||
body { | ||
font-family: Inter, system-ui, -apple-system, sans-serif; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
min-height: 100vh; | ||
margin: 0; | ||
padding: 20px; | ||
text-align: center; | ||
background: var(--background, #ffffff); | ||
color: var(--text, #1e293b); | ||
} | ||
.container { | ||
max-width: 500px; | ||
} | ||
.icon { | ||
font-size: 48px; | ||
margin-bottom: 20px; | ||
} | ||
h1 { | ||
margin-bottom: 16px; | ||
} | ||
p { | ||
margin-bottom: 24px; | ||
color: var(--text-secondary, #64748b); | ||
} | ||
button { | ||
padding: 8px 16px; | ||
border-radius: 8px; | ||
border: none; | ||
background: var(--primary-color, #3b82f6); | ||
color: white; | ||
cursor: pointer; | ||
} | ||
@media (prefers-color-scheme: dark) { | ||
body { | ||
--background: #1e1e1e; | ||
--text: #ffffff; | ||
--text-secondary: #a1a1aa; | ||
} | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="icon">📱</div> | ||
<h1>You're Offline</h1> | ||
<p>Don't worry! Your notes are safely stored and will sync when you're back online.</p> | ||
<button onclick="window.location.reload()">Try Again</button> | ||
</div> | ||
</body> | ||
</html> |
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 +1,41 @@ | ||
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"} | ||
{ | ||
"name": "TrustyNotes", | ||
"short_name": "TrustyNotes", | ||
"description": "Secure, encrypted note-taking app", | ||
"start_url": "/", | ||
"display": "standalone", | ||
"background_color": "#ffffff", | ||
"theme_color": "#1e1e1e", | ||
"icons": [ | ||
{ | ||
"src": "/android-chrome-192x192.png", | ||
"sizes": "192x192", | ||
"type": "image/png", | ||
"purpose": "any maskable" | ||
}, | ||
{ | ||
"src": "/android-chrome-512x512.png", | ||
"sizes": "512x512", | ||
"type": "image/png", | ||
"purpose": "any maskable" | ||
} | ||
], | ||
"shortcuts": [ | ||
{ | ||
"name": "New Note", | ||
"short_name": "New", | ||
"description": "Create a new note", | ||
"url": "/?new=true", | ||
"icons": [{ "src": "/icons/new-note.png", "sizes": "192x192" }] | ||
} | ||
], | ||
"screenshots": [ | ||
{ | ||
"src": "/screenshots/app.png", | ||
"sizes": "1280x720", | ||
"type": "image/png", | ||
"platform": "wide", | ||
"label": "TrustyNotes App Interface" | ||
} | ||
] | ||
} |
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,84 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { Button, Paper, Group, Text, Stack } from '@mantine/core'; | ||
import { IconDeviceMobile } from '@tabler/icons-react'; | ||
|
||
interface BeforeInstallPromptEvent extends Event { | ||
prompt: () => Promise<void>; | ||
userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }>; | ||
} | ||
|
||
export function InstallPrompt() { | ||
const [deferredPrompt, setDeferredPrompt] = useState<BeforeInstallPromptEvent | null>(null); | ||
const [showIOSPrompt, setShowIOSPrompt] = useState(false); | ||
|
||
useEffect(() => { | ||
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !/CriOS/.test(navigator.userAgent); | ||
const isStandalone = window.matchMedia('(display-mode: standalone)').matches; | ||
|
||
if (isIOS && !isStandalone) { | ||
setShowIOSPrompt(true); | ||
} | ||
|
||
const handleBeforeInstall = (e: Event) => { | ||
e.preventDefault(); | ||
setDeferredPrompt(e as BeforeInstallPromptEvent); | ||
}; | ||
|
||
window.addEventListener('beforeinstallprompt', handleBeforeInstall); | ||
|
||
return () => { | ||
window.removeEventListener('beforeinstallprompt', handleBeforeInstall); | ||
}; | ||
}, []); | ||
|
||
const handleInstall = async () => { | ||
if (!deferredPrompt) return; | ||
|
||
deferredPrompt.prompt(); | ||
const { outcome } = await deferredPrompt.userChoice; | ||
|
||
if (outcome === 'accepted') { | ||
setDeferredPrompt(null); | ||
} | ||
}; | ||
|
||
if (!deferredPrompt && !showIOSPrompt) return null; | ||
|
||
return ( | ||
<Paper p="md" withBorder> | ||
<Stack gap="xs"> | ||
<Group> | ||
<IconDeviceMobile size={24} /> | ||
<Text fw={500}>Install TrustyNotes</Text> | ||
</Group> | ||
|
||
{showIOSPrompt ? ( | ||
<> | ||
<Text size="sm"> | ||
Install TrustyNotes on your iOS device: tap the share button | ||
and select "Add to Home Screen" | ||
</Text> | ||
<Button | ||
variant="light" | ||
onClick={() => setShowIOSPrompt(false)} | ||
> | ||
Got it | ||
</Button> | ||
</> | ||
) : ( | ||
<> | ||
<Text size="sm"> | ||
Install TrustyNotes for quick access and offline use | ||
</Text> | ||
<Button | ||
onClick={handleInstall} | ||
leftSection={<IconDeviceMobile size={16} />} | ||
> | ||
Install App | ||
</Button> | ||
</> | ||
)} | ||
</Stack> | ||
</Paper> | ||
); | ||
} |
Oops, something went wrong.