Skip to content

Commit

Permalink
fix: ban global location (#84)
Browse files Browse the repository at this point in the history
* fix: ban global `location`

Signed-off-by: Innei <i@innei.in>

* fix: modal transition and tab layout id

Signed-off-by: Innei <i@innei.in>

* fix

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei authored Jun 22, 2024
1 parent 800706a commit e341b2e
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 13 deletions.
7 changes: 7 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export default defineConfig(
},
rules: {
"unicorn/prefer-module": "off",
"no-restricted-globals": [
"error",
{
name: "location",
message: "Since you don't use the same router instance in electron and browser, you can't use the global location to get the route info. \n\n" + "You can use `useLocaltion` or `getReadonlyRoute` to get the route info.",
},
],
},
},
)
4 changes: 3 additions & 1 deletion src/renderer/src/components/ui/modal/stacked/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export const ModalInternal: Component<{

const animateController = useAnimationControls()
useEffect(() => {
requestAnimationFrame(() => animateController.start(modalMontionConfig.animate))
requestAnimationFrame(() => {
animateController.start(modalMontionConfig.animate)
})
}, [animateController])
const noticeModal = useCallback(() => {
animateController
Expand Down
19 changes: 17 additions & 2 deletions src/renderer/src/components/ui/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ import { cva } from "class-variance-authority"
import { m } from "framer-motion"
import * as React from "react"

const Tabs = TabsPrimitive.Root
const TabsIdContext = React.createContext<string | null>(null)

const Tabs: typeof TabsPrimitive.Root = React.forwardRef((props, ref) => {
const { children, ...rest } = props
const id = React.useId()

return (
<TabsIdContext.Provider value={id}>
<TabsPrimitive.Root {...rest} ref={ref}>
{children}
</TabsPrimitive.Root>
</TabsIdContext.Provider>
)
})

const tabsListVariants = cva("", {
variants: {
Expand Down Expand Up @@ -63,6 +76,8 @@ const TabsTrigger = React.forwardRef<
React.useImperativeHandle(ref, () => triggerRef.current!, [ref])

const [isSelect, setIsSelect] = React.useState(false)
const id = React.useContext(TabsIdContext)
const layoutId = `tab-selected-underline-${id}`
React.useLayoutEffect(() => {
if (!triggerRef.current) return

Expand Down Expand Up @@ -98,7 +113,7 @@ const TabsTrigger = React.forwardRef<
{children}
{isSelect && (
<m.span
layoutId="tab-selected-underline"
layoutId={layoutId}
className="absolute -bottom-1 h-0.5 w-[calc(100%-16px)] rounded bg-theme-accent"
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import { useFeed } from "@renderer/queries/feed"
import { DEEPLINK_SCHEME } from "@shared/constants"
import type { FC } from "react"
import { Helmet } from "react-helmet-async"
import { useParams } from "react-router-dom"
import { useParams, useSearchParams } from "react-router-dom"

export function Component() {
const { id } = useParams()
const [search] = useSearchParams()
const view = Number.parseInt(
new URLSearchParams(location.search).get("view") || "0",
search.get("view") || "0",
)

const feed = useFeed({
Expand Down
7 changes: 3 additions & 4 deletions src/renderer/src/pages/(external)/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { useSignOut } from "@renderer/hooks"
import { LOGIN_CALLBACK_URL, loginHandler } from "@renderer/lib/auth"
import { APP_NAME } from "@renderer/lib/constants"
import { useEffect, useState } from "react"
import { useNavigate } from "react-router-dom"
import { useLocation, useNavigate } from "react-router-dom"

export function Component() {
const { status } = useSession()
const navigate = useNavigate()
const [redirecting, setRedirecting] = useState(false)
const signOut = useSignOut()

const urlParams = new URLSearchParams(window.location.search)
const location = useLocation()
const urlParams = new URLSearchParams(location.search)
const provider = urlParams.get("provider")
useEffect(() => {
if (!window.electron && provider) {
Expand All @@ -36,7 +36,6 @@ export function Component() {
<Logo className="size-20" />
<h1 className="text-3xl font-bold">
Log in to
{" "}
{APP_NAME}
</h1>
{redirecting ? (
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/src/pages/(external)/redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export function Component() {
if (window.electron) {
navigate("/")
} else {
getCallbackUrl().then((url) => window.open(url))
getCallbackUrl().then((url) => {
window.open(url)
})
}
}, [])

Expand Down
4 changes: 3 additions & 1 deletion src/renderer/src/pages/add/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { FeedViewType } from "@renderer/lib/enum"
import { FeedForm } from "@renderer/modules/discover/feed-form"
import { useLayoutEffect } from "react"
import { useSearchParams } from "react-router-dom"

export function Component() {
const urlSearchParams = new URLSearchParams(location.search)
const [urlSearchParams] = useSearchParams()

const paramUrl = urlSearchParams.get("url")
const url = paramUrl ? decodeURIComponent(paramUrl) : undefined
const id = urlSearchParams.get("id") || undefined
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/src/pages/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { useLocation } from "react-router-dom"

export function Component() {
const location = useLocation()
const urlSearchParams = new URLSearchParams(location.search)
const url = urlSearchParams.get("url")

Expand Down
6 changes: 4 additions & 2 deletions src/renderer/src/router.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHashRouter } from "react-router-dom"
import { createBrowserRouter, createHashRouter } from "react-router-dom"

import App from "./App"
import { NotFound } from "./components/ui/not-found"
Expand All @@ -7,7 +7,9 @@ import { buildGlobRoutes } from "./lib/route-builder"
const globTree = import.meta.glob("./pages/**/*.tsx")
const tree = buildGlobRoutes(globTree)

export const router = createHashRouter([
const routerCreator = window.electron ? createHashRouter : createBrowserRouter

export const router = routerCreator([
{
path: "/",
element: <App />,
Expand Down
1 change: 1 addition & 0 deletions src/shared/src/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const openElectronWindow = async (

window.open(urlObject.toString())
} else {
// eslint-disable-next-line no-restricted-globals
window.open(url.replace(DEEPLINK_SCHEME, `${location.origin}/`))
}
}
Expand Down

0 comments on commit e341b2e

Please sign in to comment.