-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16371 from ElectronicBlueberry/remove-persist
Replace Persistedstate Pinia Plugin with User-Scoped Composable
- Loading branch information
Showing
9 changed files
with
222 additions
and
249 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,6 @@ | ||
import { ref } from "vue"; | ||
|
||
// set to always mock in jest.setup.js | ||
export function useUserLocalStorage<T>(_key: string, initialValue: T) { | ||
return ref(initialValue); | ||
} |
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
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,78 +1,71 @@ | ||
import { defineStore } from "pinia"; | ||
import Vue, { computed, ref } from "vue"; | ||
|
||
import { useUserLocalStorage } from "@/composables/userLocalStorage"; | ||
import type { components } from "@/schema"; | ||
import { loadBroadcastsFromServer } from "@/stores/services/broadcasts.service"; | ||
import { mergeObjectListsById } from "@/utils/utils"; | ||
|
||
export type BroadcastNotification = components["schemas"]["BroadcastNotificationResponse"]; | ||
type Expirable = Pick<BroadcastNotification, "expiration_time">; | ||
|
||
export const useBroadcastsStore = defineStore( | ||
"broadcastsStore", | ||
() => { | ||
const broadcasts = ref<BroadcastNotification[]>([]); | ||
export const useBroadcastsStore = defineStore("broadcastsStore", () => { | ||
const broadcasts = ref<BroadcastNotification[]>([]); | ||
|
||
const loadingBroadcasts = ref<boolean>(false); | ||
const dismissedBroadcasts = ref<{ [key: string]: Expirable }>({}); | ||
const loadingBroadcasts = ref<boolean>(false); | ||
const dismissedBroadcasts = useUserLocalStorage<{ [key: string]: Expirable }>("dismissed-broadcasts", {}); | ||
|
||
const activeBroadcasts = computed(() => { | ||
return broadcasts.value.filter((b) => !dismissedBroadcasts.value[b.id]); | ||
}); | ||
const activeBroadcasts = computed(() => { | ||
return broadcasts.value.filter((b) => !dismissedBroadcasts.value[b.id]); | ||
}); | ||
|
||
async function loadBroadcasts() { | ||
loadingBroadcasts.value = true; | ||
await loadBroadcastsFromServer() | ||
.then((data) => { | ||
broadcasts.value = mergeObjectListsById(data, [], "create_time", "desc"); | ||
}) | ||
.finally(() => { | ||
loadingBroadcasts.value = false; | ||
}); | ||
} | ||
async function loadBroadcasts() { | ||
loadingBroadcasts.value = true; | ||
await loadBroadcastsFromServer() | ||
.then((data) => { | ||
broadcasts.value = mergeObjectListsById(data, [], "create_time", "desc"); | ||
}) | ||
.finally(() => { | ||
loadingBroadcasts.value = false; | ||
}); | ||
} | ||
|
||
function updateBroadcasts(broadcastList: BroadcastNotification[]) { | ||
broadcasts.value = mergeObjectListsById(broadcasts.value, broadcastList, "create_time", "desc").filter( | ||
(b) => !hasExpired(b.expiration_time) | ||
); | ||
} | ||
function updateBroadcasts(broadcastList: BroadcastNotification[]) { | ||
broadcasts.value = mergeObjectListsById(broadcasts.value, broadcastList, "create_time", "desc").filter( | ||
(b) => !hasExpired(b.expiration_time) | ||
); | ||
} | ||
|
||
function dismissBroadcast(broadcast: BroadcastNotification) { | ||
Vue.set(dismissedBroadcasts.value, broadcast.id, { expiration_time: broadcast.expiration_time }); | ||
} | ||
function dismissBroadcast(broadcast: BroadcastNotification) { | ||
Vue.set(dismissedBroadcasts.value, broadcast.id, { expiration_time: broadcast.expiration_time }); | ||
} | ||
|
||
function hasExpired(expirationTimeStr?: string) { | ||
if (!expirationTimeStr) { | ||
return false; | ||
} | ||
const expirationTime = new Date(`${expirationTimeStr}Z`); | ||
const now = new Date(); | ||
return now > expirationTime; | ||
function hasExpired(expirationTimeStr?: string) { | ||
if (!expirationTimeStr) { | ||
return false; | ||
} | ||
const expirationTime = new Date(`${expirationTimeStr}Z`); | ||
const now = new Date(); | ||
return now > expirationTime; | ||
} | ||
|
||
function clearExpiredDismissedBroadcasts() { | ||
for (const key in dismissedBroadcasts.value) { | ||
if (hasExpired(dismissedBroadcasts.value[key]?.expiration_time)) { | ||
delete dismissedBroadcasts.value[key]; | ||
} | ||
function clearExpiredDismissedBroadcasts() { | ||
for (const key in dismissedBroadcasts.value) { | ||
if (hasExpired(dismissedBroadcasts.value[key]?.expiration_time)) { | ||
delete dismissedBroadcasts.value[key]; | ||
} | ||
} | ||
} | ||
|
||
clearExpiredDismissedBroadcasts(); | ||
clearExpiredDismissedBroadcasts(); | ||
|
||
return { | ||
broadcasts, | ||
dismissedBroadcasts, | ||
loadingBroadcasts, | ||
activeBroadcasts, | ||
dismissBroadcast, | ||
loadBroadcasts, | ||
updateBroadcasts, | ||
}; | ||
}, | ||
{ | ||
persist: { | ||
paths: ["dismissedBroadcasts"], | ||
}, | ||
} | ||
); | ||
return { | ||
broadcasts, | ||
dismissedBroadcasts, | ||
loadingBroadcasts, | ||
activeBroadcasts, | ||
dismissBroadcast, | ||
loadBroadcasts, | ||
updateBroadcasts, | ||
}; | ||
}); |
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,21 +1,16 @@ | ||
import { defineStore } from "pinia"; | ||
import { ref } from "vue"; | ||
|
||
export const useUserFlagsStore = defineStore( | ||
"userFlagsStore", | ||
() => { | ||
const showSelectionQueryBreakWarning = ref(true); | ||
import { useUserLocalStorage } from "@/composables/userLocalStorage"; | ||
|
||
function ignoreSelectionQueryBreakWarning() { | ||
showSelectionQueryBreakWarning.value = false; | ||
} | ||
export const useUserFlagsStore = defineStore("userFlagsStore", () => { | ||
const showSelectionQueryBreakWarning = useUserLocalStorage("user-flags-store-show-break-warning", true); | ||
|
||
return { | ||
showSelectionQueryBreakWarning, | ||
ignoreSelectionQueryBreakWarning, | ||
}; | ||
}, | ||
{ | ||
persist: true, | ||
function ignoreSelectionQueryBreakWarning() { | ||
showSelectionQueryBreakWarning.value = false; | ||
} | ||
); | ||
|
||
return { | ||
showSelectionQueryBreakWarning, | ||
ignoreSelectionQueryBreakWarning, | ||
}; | ||
}); |
Oops, something went wrong.