* ci Signed-off-by: Innei <i@innei.in> * feat: hotupdate renderer impl * fix: skip dev Signed-off-by: Innei <i@innei.in> * update logci Signed-off-by: Innei <i@innei.in> * update logci Signed-off-by: Innei <i@innei.in> * feat: tracker Signed-off-by: Innei <tukon479@gmail.com> * log Signed-off-by: Innei <tukon479@gmail.com> * fix: hash Signed-off-by: Innei <tukon479@gmail.com> * fix: build Signed-off-by: Innei <tukon479@gmail.com> * fix: deps Signed-off-by: Innei <tukon479@gmail.com> * Update nightly.yml * update Signed-off-by: Innei <tukon479@gmail.com> * update Signed-off-by: Innei <tukon479@gmail.com> * update Signed-off-by: Innei <tukon479@gmail.com> * update minimunm Signed-off-by: Innei <tukon479@gmail.com> * calc main hash Signed-off-by: Innei <tukon479@gmail.com> * add main Hash logic Signed-off-by: Innei <tukon479@gmail.com> * remove Signed-off-by: Innei <tukon479@gmail.com> * fix Signed-off-by: Innei <tukon479@gmail.com> * log Signed-off-by: Innei <tukon479@gmail.com> * update Signed-off-by: Innei <tukon479@gmail.com> * update Signed-off-by: Innei <tukon479@gmail.com> * test Signed-off-by: Innei <tukon479@gmail.com> * update Signed-off-by: Innei <tukon479@gmail.com> * update Signed-off-by: Innei <tukon479@gmail.com> * fix: update Signed-off-by: Innei <tukon479@gmail.com> * debug Signed-off-by: Innei <tukon479@gmail.com> * update Signed-off-by: Innei <tukon479@gmail.com> * fix Signed-off-by: Innei <tukon479@gmail.com> * update Signed-off-by: Innei <tukon479@gmail.com> * fix: only one Signed-off-by: Innei <tukon479@gmail.com> * try fix cookie Signed-off-by: Innei <tukon479@gmail.com> * update Signed-off-by: Innei <tukon479@gmail.com> * fix cookie Signed-off-by: Innei <tukon479@gmail.com> * fix: domain Signed-off-by: Innei <tukon479@gmail.com> * fix Signed-off-by: Innei <tukon479@gmail.com> * fix: remove test code Signed-off-by: Innei <tukon479@gmail.com> * fix Signed-off-by: Innei <tukon479@gmail.com> --------- Signed-off-by: Innei <i@innei.in> Signed-off-by: Innei <tukon479@gmail.com>
117 lines
3.0 KiB
TypeScript
117 lines
3.0 KiB
TypeScript
import type { BrowserWindow } from "electron"
|
|
import { useEffect, useLayoutEffect, useRef } from "react"
|
|
import type { toast } from "sonner"
|
|
|
|
import type { GeneralSettings, UISettings } from "./interface/settings"
|
|
|
|
const PREFIX = "__follow"
|
|
|
|
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
declare const dialog: {
|
|
ask: (options: {
|
|
title: string
|
|
message: string
|
|
onConfirm?: () => void
|
|
onCancel?: () => void
|
|
confirmText?: string
|
|
cancelText?: string
|
|
}) => Promise<boolean>
|
|
}
|
|
interface RenderGlobalContext {
|
|
/// Access Settings
|
|
showSetting: (path?: string) => void
|
|
getGeneralSettings: () => GeneralSettings
|
|
getUISettings: () => UISettings
|
|
|
|
/**
|
|
* @description only work in electron app
|
|
*/
|
|
onWindowClose: () => void
|
|
/**
|
|
* @description only work in electron app
|
|
*/
|
|
onWindowShow: () => void
|
|
|
|
/// Actions
|
|
follow: (options?: { isList: boolean; id?: string; url?: string }) => void
|
|
profile: (id: string, variant?: "drawer" | "dialog") => void
|
|
quickAdd: () => void
|
|
rsshubRoute: (route: string) => void
|
|
// Navigate
|
|
goToDiscover: () => void
|
|
|
|
// user data
|
|
clearIfLoginOtherAccount: (newUserId: string) => void
|
|
|
|
/// Utils
|
|
toast: typeof toast
|
|
|
|
readyToUpdate: () => void
|
|
dialog: typeof dialog
|
|
// URL
|
|
getWebUrl: () => string
|
|
getApiUrl: () => string
|
|
|
|
// View
|
|
zenMode: () => void
|
|
}
|
|
|
|
export const registerGlobalContext = (context: Partial<RenderGlobalContext>) => {
|
|
globalThis[PREFIX] = {
|
|
...globalThis[PREFIX],
|
|
...context,
|
|
}
|
|
}
|
|
|
|
export const useRegisterGlobalContext = <K extends keyof RenderGlobalContext>(
|
|
key: K,
|
|
fn: RenderGlobalContext[K],
|
|
) => {
|
|
const eventCallbackRef = useRef(fn)
|
|
useLayoutEffect(() => {
|
|
eventCallbackRef.current = fn
|
|
}, [fn])
|
|
|
|
useEffect(() => {
|
|
registerGlobalContext({ [key]: eventCallbackRef.current })
|
|
}, [key])
|
|
}
|
|
|
|
function createProxy<T extends RenderGlobalContext>(window: BrowserWindow, path: string[] = []): T {
|
|
return new Proxy((() => {}) as any, {
|
|
get(_, prop: string) {
|
|
const newPath = [...path, prop]
|
|
|
|
return createProxy(window, newPath)
|
|
},
|
|
async apply(_, __, args: any[]) {
|
|
const methodPath = path.join(".")
|
|
|
|
try {
|
|
return await window.webContents.executeJavaScript(
|
|
`globalThis.${PREFIX}?.${methodPath}?.(${args.map((arg) => JSON.stringify(arg)).join(",")})`,
|
|
)
|
|
} catch (err) {
|
|
console.error(`Failed to executeJavaScript: ${methodPath}`, err)
|
|
}
|
|
},
|
|
})
|
|
}
|
|
type AddPromise<T> = T extends (...args: infer A) => Promise<infer R>
|
|
? (...args: A) => Promise<R>
|
|
: T extends (...args: infer A) => infer R
|
|
? (...args: A) => Promise<Awaited<R>>
|
|
: unknown
|
|
|
|
type Fn<T> = {
|
|
[K in keyof T]: AddPromise<T[K]> &
|
|
(T[K] extends object ? { [P in keyof T[K]]: AddPromise<T[K][P]> } : never)
|
|
}
|
|
export function callWindowExpose<T extends RenderGlobalContext>(window: BrowserWindow) {
|
|
return createProxy(window) as Fn<T>
|
|
}
|
|
|
|
export function callWindowExposeRenderer() {
|
|
return globalThis[PREFIX] as Fn<RenderGlobalContext>
|
|
}
|