diff --git a/apps/main/src/index.ts b/apps/main/src/index.ts index fb7ab51af..6a4aeaa09 100644 --- a/apps/main/src/index.ts +++ b/apps/main/src/index.ts @@ -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 { diff --git a/apps/main/src/window.ts b/apps/main/src/window.ts index 5a0aae4a4..6b7b83f3a 100644 --- a/apps/main/src/window.ts +++ b/apps/main/src/window.ts @@ -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, diff --git a/apps/renderer/src/database/db.ts b/apps/renderer/src/database/db.ts index bbfcf6d03..1940677aa 100644 --- a/apps/renderer/src/database/db.ts +++ b/apps/renderer/src/database/db.ts @@ -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"> diff --git a/apps/renderer/src/modules/auth/LoginModalContent.tsx b/apps/renderer/src/modules/auth/LoginModalContent.tsx index 6f71fd676..b8b4f78c1 100644 --- a/apps/renderer/src/modules/auth/LoginModalContent.tsx +++ b/apps/renderer/src/modules/auth/LoginModalContent.tsx @@ -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("") + const [loadingLockSet, _setLoadingLockSet] = useState("") + + const timerRef = useRef(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 ( diff --git a/apps/renderer/src/pages/(external)/redirect.tsx b/apps/renderer/src/pages/(external)/redirect.tsx index 5d45b0b7f..3aa09f05a 100644 --- a/apps/renderer/src/pages/(external)/redirect.tsx +++ b/apps/renderer/src/pages/(external)/redirect.tsx @@ -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 })}
- diff --git a/apps/renderer/src/providers/extension-expose-provider.tsx b/apps/renderer/src/providers/extension-expose-provider.tsx index 129c6dad3..edce6a294 100644 --- a/apps/renderer/src/providers/extension-expose-provider.tsx +++ b/apps/renderer/src/providers/extension-expose-provider.tsx @@ -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(() => { diff --git a/apps/renderer/src/store/list/store.ts b/apps/renderer/src/store/list/store.ts index d1d22ff96..8ead48f13 100644 --- a/apps/renderer/src/store/list/store.ts +++ b/apps/renderer/src/store/list/store.ts @@ -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() diff --git a/apps/renderer/src/store/utils/clear.ts b/apps/renderer/src/store/utils/clear.ts index a5c44b808..53932874b 100644 --- a/apps/renderer/src/store/utils/clear.ts +++ b/apps/renderer/src/store/utils/clear.ts @@ -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() + } +} diff --git a/packages/shared/src/bridge.ts b/packages/shared/src/bridge.ts index 13db314a3..ac7596a2e 100644 --- a/packages/shared/src/bridge.ts +++ b/packages/shared/src/bridge.ts @@ -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