Skip to content

Commit e69c4fa

Browse files
author
Piotr
committed
notification bar
1 parent 429981c commit e69c4fa

File tree

6 files changed

+139
-41
lines changed

6 files changed

+139
-41
lines changed

src/App.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import TopBar from "./components/TopBar.vue"
3434
import Close from './components/icon/Close.vue';
3535
import { useContextMenuStore } from './stores/contextMenu';
3636
import { globalEventBus } from './event_bus';
37+
import { useNotificationBarStore } from './stores/notificationBar';
3738
3839
const store = useMainStore()
3940
const storeFilter = useFilterStore()
@@ -557,6 +558,10 @@ const initWs = async (): Promise<boolean> => {
557558
558559
store.initSettings = init.json!
559560
561+
if (init.json) {
562+
useNotificationBarStore().processNotification(init.json?.updateVersion)
563+
}
564+
560565
let passValid = await client.sendGet("check-pass?password=" + store.getPassword())
561566
if (store.initSettings.authRequired && passValid.status !== 200) {
562567
store.modalShow = "auth"
@@ -606,10 +611,10 @@ const updateSampleLine = () => {
606611
<SettingsDrawer v-if="store.settingsDrawer" @close="store.settingsDrawer = false" :layout="(store.layout as Layout)"
607612
@edit="columnEdited" @remove="columnRemoved" @move="reorderColumns" @settings-update="settingsUpdate"
608613
@update-sample-line="updateSampleLine" :sampleLine="sampleLine" />
609-
<UpdateBar v-if="store.notificationBar" />
614+
<UpdateBar v-if="useNotificationBarStore().display" />
610615
<DemoBar v-if="store.demoMode" @start="store.demoStatus = 'started'" @stop="store.demoStatus = 'stopped'"
611616
@mode="changeDemoMode" @add="addDemoData(100)" />
612-
<div :class="{ 'demo': store.demoMode, 'update': store.notificationBar }">
617+
<div :class="{ 'demo': store.demoMode, 'update': useNotificationBarStore().display }">
613618
<div class="top-bar">
614619
<div class="left">
615620
<div class="logo">
@@ -636,7 +641,7 @@ const updateSampleLine = () => {
636641
<Close />
637642
</button>
638643
<span class="search-error" v-if="store.searchbarValid.length > 0">Invalid search query: {{ store.searchbarValid
639-
}}.
644+
}}.
640645
<br />
641646
<a href="https://logdy.dev/docs" target="_blank">Visit docs</a>
642647
</span>

src/components/UpdateBar.vue

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
<script setup lang="ts">
2-
import { useMainStore } from '../store';
2+
import { useNotificationBarStore } from '../stores/notificationBar';
33
4-
const hide = () => {
5-
6-
}
7-
8-
const remind = () => {
9-
10-
}
4+
const store = useNotificationBarStore()
115
126
</script>
137

148
<template>
15-
<div class="update-bar" v-if="useMainStore().initSettings && useMainStore().initSettings?.updateVersion">
16-
<span>🎉 New Logdy v{{ useMainStore().initSettings?.updateVersion.current_version }} available! {{
17-
useMainStore().initSettings?.updateVersion.excerpt }} <a
18-
:href="useMainStore().initSettings?.updateVersion.blog_link"
19-
v-if="useMainStore().initSettings?.updateVersion.blog_link" target="_blank">Read
9+
<div class="update-bar" v-if="store.updateResponse">
10+
<span>🎉 New Logdy v{{ store.updateResponse.current_version }} available! {{
11+
store.updateResponse.excerpt }} <a :href="store.updateResponse.blog_link"
12+
v-if="store.updateResponse.blog_link" target="_blank">Read
2013
more</a></span>
21-
<button @click="hide">Hide</button>
22-
<button @click="remind">Remind later</button>
14+
<button @click="store.hide">Hide</button>
15+
<button @click="store.remindLater">Remind later</button>
2316
</div>
2417
</template>
2518

@@ -55,7 +48,7 @@ const remind = () => {
5548
button {
5649
padding: 3px 8px;
5750
font-size: 10px;
58-
border-radius: 7px;
51+
border-radius: 4px;
5952
margin-right: 4px;
6053
}
6154
}

src/storage.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ export class Storage<T extends { id?: string }> {
9595
localStorage.setItem(this.id(id), JSON.stringify(item))
9696
}
9797

98+
upsert(id: string, item: T) {
99+
if (typeof item !== 'object') {
100+
throw new Error("upsert is only available for objects")
101+
}
102+
103+
let doc = this.getOne(id)
104+
105+
if (doc) {
106+
this.update(id, { ...doc, ...item })
107+
} else {
108+
this.add(item, id)
109+
}
110+
}
111+
98112
remove(id: string) {
99113
let _id = this.id(id)
100114
localStorage.removeItem(_id)

src/storage_common.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Storage } from "./storage";
2+
3+
interface CommonStorage {
4+
id?: string
5+
updateBarVersionSeen?: string // which version the user acknowledged
6+
updateBarShowAfter?: number //a UNIX timestamp after which the banner can be displayed next time
7+
}
8+
9+
class CommonStorageImpl {
10+
11+
private storage: Storage<CommonStorage>;
12+
13+
constructor() {
14+
this.storage = new Storage<CommonStorage>('common')
15+
}
16+
17+
private ID = "main_id"
18+
19+
get(): CommonStorage {
20+
let st = this.storage.getOne(this.ID)
21+
22+
if (!st) {
23+
this.storage.upsert(this.ID, {})
24+
} else {
25+
return st
26+
}
27+
28+
return {}
29+
}
30+
31+
save(item: CommonStorage) {
32+
this.storage.update(this.ID, item)
33+
}
34+
}
35+
36+
export const storageCommon = new CommonStorageImpl()

src/store.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ export interface InitSettings {
2626
// this will hold a Layout JSON to load from the backend
2727
configStr: string;
2828

29-
updateVersion: {
30-
checked: boolean;
31-
local_version: string;
32-
current_version: string;
33-
current_version_published: string;
34-
download_link: string;
35-
blog_link: string;
36-
excerpt: string;
37-
}
29+
updateVersion: UpdateResponse
30+
}
31+
32+
export interface UpdateResponse {
33+
checked: boolean;
34+
local_version: string;
35+
current_version: string;
36+
current_version_published: string;
37+
download_link: string;
38+
blog_link: string;
39+
excerpt: string;
3840
}
3941

4042
type ReceiveStatus = "paused" | "following" | "following_cursor"
@@ -448,18 +450,6 @@ export const useMainStore = defineStore("main", () => {
448450
}
449451
}
450452

451-
const notificationBar = computed(() => {
452-
453-
return false // for until its finished
454-
455-
// if (!initSettings.value) {
456-
// return false
457-
// }
458-
459-
// return initSettings.value?.updateVersion.checked &&
460-
// initSettings.value.updateVersion.current_version != initSettings.value.updateVersion.local_version
461-
})
462-
463453
watch(searchbar, (newVal) => {
464454
if (!isRecordJson.value) {
465455
return
@@ -492,7 +482,6 @@ export const useMainStore = defineStore("main", () => {
492482
status,
493483
statusStr,
494484

495-
notificationBar,
496485
getUpdate,
497486

498487
receiveStatus,

src/stores/notificationBar.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { defineStore } from "pinia";
2+
import { ref } from "vue";
3+
import { UpdateResponse, useMainStore } from "../store";
4+
import { storageCommon } from "../storage_common";
5+
6+
const DAY = 1000 * 3600 * 24
7+
8+
export const useNotificationBarStore = defineStore("notification_bar", () => {
9+
10+
const display = ref<Boolean>(false)
11+
const updateResponse = ref<UpdateResponse>()
12+
13+
const processNotification = (resp: UpdateResponse) => {
14+
updateResponse.value = resp
15+
16+
let s = useMainStore().initSettings
17+
18+
if (!s?.updateVersion.checked) {
19+
return
20+
}
21+
22+
if (s.updateVersion.current_version == s.updateVersion.local_version) {
23+
return
24+
}
25+
26+
let stored = storageCommon.get()
27+
if (stored.updateBarVersionSeen == s.updateVersion.current_version) {
28+
return
29+
}
30+
31+
if (stored.updateBarShowAfter && stored.updateBarShowAfter > new Date().getTime()) {
32+
return
33+
}
34+
35+
display.value = true
36+
}
37+
38+
const hide = () => {
39+
let st = storageCommon.get()
40+
st.updateBarVersionSeen = updateResponse.value?.current_version
41+
st.updateBarShowAfter = undefined
42+
storageCommon.save(st)
43+
44+
display.value = false
45+
}
46+
const remindLater = () => {
47+
display.value = false
48+
let st = storageCommon.get()
49+
st.updateBarShowAfter = new Date().getTime() + (3 * DAY)
50+
storageCommon.save(st)
51+
52+
}
53+
54+
return {
55+
display,
56+
processNotification,
57+
hide,
58+
remindLater,
59+
updateResponse
60+
};
61+
});

0 commit comments

Comments
 (0)