Switch prefs to dexie

This commit is contained in:
Philipp Heckel 2022-03-01 22:01:51 -05:00
parent 23d275acec
commit effc1f42eb
5 changed files with 61 additions and 44 deletions

View File

@ -10,7 +10,6 @@ class ConnectionManager {
return; return;
} }
console.log(`[ConnectionManager] Refreshing connections`); console.log(`[ConnectionManager] Refreshing connections`);
console.log(users);
const subscriptionIds = subscriptions.ids(); const subscriptionIds = subscriptions.ids();
const deletedIds = Array.from(this.connections.keys()).filter(id => !subscriptionIds.includes(id)); const deletedIds = Array.from(this.connections.keys()).filter(id => !subscriptionIds.includes(id));

View File

@ -1,5 +1,6 @@
import Subscription from "./Subscription"; import Subscription from "./Subscription";
import Subscriptions from "./Subscriptions"; import Subscriptions from "./Subscriptions";
import db from "./db";
class Repository { class Repository {
loadSubscriptions() { loadSubscriptions() {
@ -41,33 +42,41 @@ class Repository {
localStorage.setItem('subscriptions', serialized); localStorage.setItem('subscriptions', serialized);
} }
loadSelectedSubscriptionId() { async setSelectedSubscriptionId(selectedSubscriptionId) {
console.log(`[Repository] Loading selected subscription ID from localStorage`); console.log(`[Repository] Saving selected subscription ${selectedSubscriptionId}`);
const selectedSubscriptionId = localStorage.getItem('selectedSubscriptionId'); db.prefs.put({key: 'selectedSubscriptionId', value: selectedSubscriptionId});
return (selectedSubscriptionId) ? selectedSubscriptionId : "";
} }
saveSelectedSubscriptionId(selectedSubscriptionId) { async getSelectedSubscriptionId() {
console.log(`[Repository] Saving selected subscription ${selectedSubscriptionId} to localStorage`); console.log(`[Repository] Loading selected subscription ID`);
localStorage.setItem('selectedSubscriptionId', selectedSubscriptionId); const selectedSubscriptionId = await db.prefs.get('selectedSubscriptionId');
return (selectedSubscriptionId) ? selectedSubscriptionId.value : "";
} }
setMinPriority(minPriority) { async setMinPriority(minPriority) {
localStorage.setItem('minPriority', minPriority.toString()); db.prefs.put({key: 'minPriority', value: minPriority.toString()});
} }
getMinPriority() { async getMinPriority() {
const minPriority = localStorage.getItem('minPriority'); const minPriority = await db.prefs.get('minPriority');
return (minPriority) ? Number(minPriority) : 1; return (minPriority) ? Number(minPriority.value) : 1;
} }
setDeleteAfter(deleteAfter) { minPriority() {
localStorage.setItem('deleteAfter', deleteAfter.toString()); return db.prefs.get('minPriority');
} }
getDeleteAfter() { async setDeleteAfter(deleteAfter) {
const deleteAfter = localStorage.getItem('deleteAfter'); db.prefs.put({key:'deleteAfter', value: deleteAfter.toString()});
return (deleteAfter) ? Number(deleteAfter) : 604800; // Default is one week }
async getDeleteAfter() {
const deleteAfter = await db.prefs.get('deleteAfter');
return (deleteAfter) ? Number(deleteAfter.value) : 604800; // Default is one week
}
deleteAfter() {
return db.prefs.get('deleteAfter');
} }
} }

View File

@ -9,7 +9,10 @@ import Dexie from 'dexie';
const db = new Dexie('ntfy'); const db = new Dexie('ntfy');
db.version(1).stores({ db.version(1).stores({
users: '&baseUrl, username', subscriptions: '&id',
notifications: '&id,subscriptionId',
users: '&baseUrl,username',
prefs: '&key'
}); });
export default db; export default db;

View File

@ -92,21 +92,24 @@ const App = () => {
// Define hooks: Note that the order of the hooks is important. The "loading" hooks // Define hooks: Note that the order of the hooks is important. The "loading" hooks
// must be before the "saving" hooks. // must be before the "saving" hooks.
useEffect(() => { useEffect(() => {
// Load subscriptions const load = async () => {
const subscriptions = repository.loadSubscriptions(); // Load subscriptions
const selectedSubscriptionId = repository.loadSelectedSubscriptionId(); const subscriptions = repository.loadSubscriptions();
setSubscriptions(subscriptions); const selectedSubscriptionId = await repository.getSelectedSubscriptionId();
setSubscriptions(subscriptions);
// Set selected subscription // Set selected subscription
const maybeSelectedSubscription = subscriptions.get(selectedSubscriptionId); const maybeSelectedSubscription = subscriptions.get(selectedSubscriptionId);
if (maybeSelectedSubscription) { if (maybeSelectedSubscription) {
setSelectedSubscription(maybeSelectedSubscription); setSelectedSubscription(maybeSelectedSubscription);
} }
// Poll all subscriptions // Poll all subscriptions
subscriptions.forEach((subscriptionId, subscription) => { subscriptions.forEach((subscriptionId, subscription) => {
poll(subscription); poll(subscription);
}); });
};
load();
}, [/* initial render */]); }, [/* initial render */]);
useEffect(() => { useEffect(() => {
const notificationClickFallback = (subscription) => setSelectedSubscription(subscription); const notificationClickFallback = (subscription) => setSelectedSubscription(subscription);
@ -124,7 +127,7 @@ const App = () => {
useEffect(() => repository.saveSubscriptions(subscriptions), [subscriptions]); useEffect(() => repository.saveSubscriptions(subscriptions), [subscriptions]);
useEffect(() => { useEffect(() => {
const subscriptionId = (selectedSubscription) ? selectedSubscription.id : ""; const subscriptionId = (selectedSubscription) ? selectedSubscription.id : "";
repository.saveSelectedSubscriptionId(subscriptionId) repository.setSelectedSubscriptionId(subscriptionId)
}, [selectedSubscription]); }, [selectedSubscription]);
return ( return (

View File

@ -45,7 +45,7 @@ const Preferences = (props) => {
); );
}; };
const Notifications = (props) => { const Notifications = () => {
return ( return (
<Card sx={{p: 3}}> <Card sx={{p: 3}}>
<Typography variant="h5"> <Typography variant="h5">
@ -60,10 +60,12 @@ const Notifications = (props) => {
}; };
const MinPriority = () => { const MinPriority = () => {
const [minPriority, setMinPriority] = useState(repository.getMinPriority()); const minPriority = useLiveQuery(() => repository.getMinPriority());
const handleChange = (ev) => { const handleChange = async (ev) => {
setMinPriority(ev.target.value); await repository.setMinPriority(ev.target.value);
repository.setMinPriority(ev.target.value); }
if (!minPriority) {
return null; // While loading
} }
return ( return (
<Pref title="Minimum priority"> <Pref title="Minimum priority">
@ -81,10 +83,12 @@ const MinPriority = () => {
}; };
const DeleteAfter = () => { const DeleteAfter = () => {
const [deleteAfter, setDeleteAfter] = useState(repository.getDeleteAfter()); const deleteAfter = useLiveQuery(async () => repository.getDeleteAfter());
const handleChange = (ev) => { const handleChange = async (ev) => {
setDeleteAfter(ev.target.value); await repository.setDeleteAfter(ev.target.value);
repository.setDeleteAfter(ev.target.value); }
if (!deleteAfter) {
return null; // While loading
} }
return ( return (
<Pref title="Delete notifications"> <Pref title="Delete notifications">
@ -101,7 +105,6 @@ const DeleteAfter = () => {
) )
}; };
const PrefGroup = (props) => { const PrefGroup = (props) => {
return ( return (
<div style={{ <div style={{
@ -159,7 +162,7 @@ const DefaultServer = (props) => {
); );
}; };
const Users = (props) => { const Users = () => {
const [dialogKey, setDialogKey] = useState(0); const [dialogKey, setDialogKey] = useState(0);
const [dialogOpen, setDialogOpen] = useState(false); const [dialogOpen, setDialogOpen] = useState(false);
const users = useLiveQuery(() => db.users.toArray()); const users = useLiveQuery(() => db.users.toArray());