fix: clear local data when login other account and store window pos before quit

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-10-10 00:21:06 +08:00
parent b72d4557df
commit c7f74cf746
9 changed files with 96 additions and 14 deletions
+20 -2
View File
@@ -1,4 +1,5 @@
import { electronApp, optimizer } from "@electron-toolkit/utils"
import { callWindowExpose } from "@follow/shared/bridge"
import { APP_PROTOCOL } from "@follow/shared/constants"
import { env } from "@follow/shared/env"
import { app, BrowserWindow, session } from "electron"
@@ -8,9 +9,10 @@ import { isDev, isMacOS } from "./env"
import { initializeAppStage0, initializeAppStage1 } from "./init"
import { updateProxy } from "./lib/proxy"
import { handleUrlRouting } from "./lib/router"
import { store } from "./lib/store"
import { setAuthSessionToken } from "./lib/user"
import { registerUpdater } from "./updater"
import { createMainWindow } from "./window"
import { createMainWindow, getMainWindow, windowStateStoreKey } from "./window"
if (isDev) console.info("[main] env loaded:", env)
@@ -115,13 +117,28 @@ function bootstrap() {
}
})
const handleOpen = (url: string) => {
app.on("before-quit", () => {
// store window pos when before app quit
const window = getMainWindow()
if (!window) return
const bounds = window.getBounds()
store.set(windowStateStoreKey, {
width: bounds.width,
height: bounds.height,
x: bounds.x,
y: bounds.y,
})
})
const handleOpen = async (url: string) => {
const isValid = URL.canParse(url)
if (!isValid) return
const urlObj = new URL(url)
if (urlObj.hostname === "auth" || urlObj.pathname === "//auth") {
const token = urlObj.searchParams.get("token")
const userId = urlObj.searchParams.get("userId")
const apiURL = process.env["VITE_API_URL"] || import.meta.env.VITE_API_URL
if (token && apiURL) {
@@ -135,6 +152,7 @@ function bootstrap() {
domain: new URL(apiURL).hostname,
sameSite: "no_restriction",
})
userId && (await callWindowExpose(mainWindow).clearIfLoginOtherAccount(userId))
mainWindow.reload()
}
} else {
+4 -4
View File
@@ -161,9 +161,9 @@ export function createWindow(
return window
}
export const windowStateStoreKey = "windowState"
export const createMainWindow = () => {
const storeKey = "windowState"
const windowState = store.get(storeKey) as {
const windowState = store.get(windowStateStoreKey) as {
height: number
width: number
x: number
@@ -201,7 +201,7 @@ export const createMainWindow = () => {
const windowStoreKey = Symbol.for("maximized")
if (window[windowStoreKey]) {
const stored = window[windowStoreKey]
store.set(storeKey, {
store.set(windowStateStoreKey, {
width: stored.size[0],
height: stored.size[1],
x: stored.position[0],
@@ -213,7 +213,7 @@ export const createMainWindow = () => {
}
const bounds = window.getBounds()
store.set(storeKey, {
store.set(windowStateStoreKey, {
width: bounds.width,
height: bounds.height,
x: bounds.x,
+1 -1
View File
@@ -30,7 +30,7 @@ export interface LocalDBSchemaMap {
}
// Define a local DB
export class BrowserDB extends Dexie {
class BrowserDB extends Dexie {
public entries: BrowserDBTable<"entries">
public feeds: BrowserDBTable<"feeds">
public subscriptions: BrowserDBTable<"subscriptions">
@@ -1,6 +1,6 @@
import clsx from "clsx"
import { AnimatePresence, m } from "framer-motion"
import { useState } from "react"
import { useEffect, useRef, useState } from "react"
import { useTranslation } from "react-i18next"
import { FollowIcon } from "~/components/icons/follow"
@@ -23,7 +23,26 @@ export const LoginModalContent = (props: LoginModalContentProps) => {
const { t } = useTranslation()
const [loadingLockSet, setLoadingLockSet] = useState<string>("")
const [loadingLockSet, _setLoadingLockSet] = useState<string>("")
const timerRef = useRef<NodeJS.Timeout | null>(null)
const setLoadingLockSet = (id: string) => {
_setLoadingLockSet(id)
if (timerRef.current) {
clearTimeout(timerRef.current)
}
timerRef.current = setTimeout(() => {
_setLoadingLockSet("")
}, 3000)
}
useEffect(() => {
return () => {
if (timerRef.current) {
clearTimeout(timerRef.current)
}
}
}, [])
const disabled = !!loadingLockSet
return (
@@ -14,7 +14,10 @@ export function Component() {
const getCallbackUrl = async () => {
const { data } = await apiClient["auth-app"]["new-session"].$post({})
return `${DEEPLINK_SCHEME}auth?token=${data.sessionToken}`
return {
url: `${DEEPLINK_SCHEME}auth?token=${data.sessionToken}&userId=${data.userId}`,
userId: data.userId,
}
}
const onceRef = useRef(false)
@@ -24,7 +27,7 @@ export function Component() {
if (window.electron) {
navigate("/")
} else {
getCallbackUrl().then((url) => {
getCallbackUrl().then(({ url }) => {
window.open(url, "_top")
// If you are in development, you can use the following code to open the app
@@ -42,13 +45,22 @@ export function Component() {
{t("redirect.instruction", { app_name: APP_NAME })}
</h2>
<div className="center flex flex-col gap-4 sm:flex-row">
<Button variant="text" className="h-14 px-10 text-base" onClick={() => navigate("/")}>
<Button
variant="text"
className="h-14 px-10 text-base"
onClick={() => {
navigate("/")
}}
>
{t("redirect.continueInBrowser")}
</Button>
<Button
className="h-14 !rounded-full px-5 text-lg"
onClick={async () => window.open(await getCallbackUrl(), "_top")}
onClick={async () => {
const { url } = await getCallbackUrl()
window.open(url, "_top")
}}
>
{t("redirect.openApp", { app_name: APP_NAME })}
</Button>
@@ -10,6 +10,7 @@ import { useModalStack } from "~/components/ui/modal"
import { useFollow } from "~/hooks/biz/useFollow"
import { usePresentUserProfileModal } from "~/modules/profile/hooks"
import { useSettingModal } from "~/modules/settings/modal/hooks"
import { clearDataIfLoginOtherAccount } from "~/store/utils/clear"
export const ExtensionExposeProvider = () => {
const { present } = useModalStack()
@@ -28,6 +29,10 @@ export const ExtensionExposeProvider = () => {
getWebUrl() {
return window.location.origin
},
clearIfLoginOtherAccount(newUserId: string) {
clearDataIfLoginOtherAccount(newUserId)
},
})
}, [])
useEffect(() => {
+6
View File
@@ -79,6 +79,12 @@ class ListActionStatic {
this.upsertMany([res.data.list])
return res.data
}
async clear() {
set((state) => {
state.lists = {}
})
}
}
export const listActions = new ListActionStatic()
+20 -1
View File
@@ -1,15 +1,25 @@
import { clearUISettings } from "~/atoms/settings/ui"
import { browserDB } from "~/database"
import { getStorageNS } from "~/lib/ns"
import { entryActions } from "../entry"
import { feedActions } from "../feed"
import { clearImageDimensionsDb } from "../image/db"
import { inboxActions } from "../inbox"
import { listActions } from "../list"
import { subscriptionActions } from "../subscription"
import { feedUnreadActions } from "../unread"
export const clearLocalPersistStoreData = async () => {
// All clear and reset method will aggregate here
;[entryActions, subscriptionActions, feedUnreadActions, feedActions].forEach((actions) => {
;[
entryActions,
subscriptionActions,
feedUnreadActions,
feedActions,
listActions,
inboxActions,
].forEach((actions) => {
actions.clear()
})
@@ -18,3 +28,12 @@ export const clearLocalPersistStoreData = async () => {
await clearImageDimensionsDb()
await browserDB.delete().catch(() => null)
}
const storedUserId = getStorageNS("user_id")
export const clearDataIfLoginOtherAccount = (newUserId: string) => {
const oldUserId = localStorage.getItem(storedUserId)
localStorage.setItem(storedUserId, newUserId)
if (oldUserId !== newUserId) {
return clearLocalPersistStoreData()
}
}
+3
View File
@@ -24,6 +24,9 @@ interface RenderGlobalContext {
follow: (options?: { isList: boolean; id?: string; url?: string }) => void
profile: (id: string, variant?: "drawer" | "dialog") => void
// user data
clearIfLoginOtherAccount: (newUserId: string) => void
/// Utils
toast: typeof toast
// URL