mirror of
https://github.com/Afilmory/afilmory
synced 2026-04-25 07:15:36 +00:00
refactor: remove unnecessary window checks across components
- Eliminated checks for `typeof window !== 'undefined'` in various components and utility functions, simplifying the codebase. - Updated logic to directly access `window` properties, assuming the code runs in a browser environment. - Improved readability and maintainability by streamlining conditional checks related to window availability. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -118,11 +118,6 @@ export const usePhotoViewerTransitions = ({
|
|||||||
if (!isOpen || !currentPhoto) return
|
if (!isOpen || !currentPhoto) return
|
||||||
if (entryTransition || isViewerContentVisible) return
|
if (entryTransition || isViewerContentVisible) return
|
||||||
|
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
setIsViewerContentVisible(true)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const triggerEl = resolveTriggerElement()
|
const triggerEl = resolveTriggerElement()
|
||||||
|
|
||||||
if (!triggerEl) {
|
if (!triggerEl) {
|
||||||
@@ -208,12 +203,6 @@ export const usePhotoViewerTransitions = ({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
wasOpenRef.current = false
|
|
||||||
restoreTriggerElementVisibility()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const triggerEl = resolveTriggerElement()
|
const triggerEl = resolveTriggerElement()
|
||||||
|
|
||||||
if (!triggerEl || !triggerEl.isConnected) {
|
if (!triggerEl || !triggerEl.isConnected) {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const THUMBNAIL_PADDING = {
|
|||||||
} as const
|
} as const
|
||||||
|
|
||||||
export const escapeAttributeValue = (value: string) => {
|
export const escapeAttributeValue = (value: string) => {
|
||||||
if (typeof window !== 'undefined' && window.CSS?.escape) {
|
if (window.CSS?.escape) {
|
||||||
return window.CSS.escape(value)
|
return window.CSS.escape(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,14 +23,13 @@ export const escapeAttributeValue = (value: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getRootFontSize = () => {
|
const getRootFontSize = () => {
|
||||||
if (typeof window === 'undefined') return 16
|
|
||||||
const value = window.getComputedStyle(document.documentElement).fontSize
|
const value = window.getComputedStyle(document.documentElement).fontSize
|
||||||
const parsed = Number.parseFloat(value || '16')
|
const parsed = Number.parseFloat(value || '16')
|
||||||
return Number.isNaN(parsed) ? 16 : parsed
|
return Number.isNaN(parsed) ? 16 : parsed
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getBorderRadius = (element: Element | null) => {
|
export const getBorderRadius = (element: Element | null) => {
|
||||||
if (typeof window === 'undefined' || !element) return 0
|
if (!element) return 0
|
||||||
|
|
||||||
const computedStyle = window.getComputedStyle(element)
|
const computedStyle = window.getComputedStyle(element)
|
||||||
const radiusCandidates = [
|
const radiusCandidates = [
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ const defaultInjectConfig = {
|
|||||||
export const injectConfig = merge(defaultInjectConfig, __CONFIG__)
|
export const injectConfig = merge(defaultInjectConfig, __CONFIG__)
|
||||||
|
|
||||||
const getInjectedSiteConfig = (): Partial<SiteConfig> | undefined => {
|
const getInjectedSiteConfig = (): Partial<SiteConfig> | undefined => {
|
||||||
if (typeof window !== 'undefined' && window.__SITE_CONFIG__) {
|
if (window.__SITE_CONFIG__) {
|
||||||
return window.__SITE_CONFIG__
|
return window.__SITE_CONFIG__
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
export const isSafari = /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)
|
export const isSafari = /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)
|
||||||
|
|
||||||
export const isMobileDevice = (() => {
|
export const isMobileDevice = (() => {
|
||||||
if (typeof window === 'undefined') return false
|
|
||||||
return (
|
return (
|
||||||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ||
|
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ||
|
||||||
// 现代检测方式:支持触摸且屏幕较小
|
// 现代检测方式:支持触摸且屏幕较小
|
||||||
|
|||||||
@@ -19,21 +19,18 @@ export function ErrorElement() {
|
|||||||
}, [error])
|
}, [error])
|
||||||
|
|
||||||
const reloadTriggeredRef = useRef(false)
|
const reloadTriggeredRef = useRef(false)
|
||||||
const hasWindow = typeof window !== 'undefined'
|
|
||||||
const shouldAttemptReload =
|
const shouldAttemptReload =
|
||||||
hasWindow &&
|
message.startsWith('Failed to fetch dynamically imported module') && window.sessionStorage.getItem('reload') !== '1'
|
||||||
message.startsWith('Failed to fetch dynamically imported module') &&
|
|
||||||
window.sessionStorage.getItem('reload') !== '1'
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!shouldAttemptReload || reloadTriggeredRef.current || !hasWindow) {
|
if (!shouldAttemptReload || reloadTriggeredRef.current) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
reloadTriggeredRef.current = true
|
reloadTriggeredRef.current = true
|
||||||
window.sessionStorage.setItem('reload', '1')
|
window.sessionStorage.setItem('reload', '1')
|
||||||
window.location.reload()
|
window.location.reload()
|
||||||
}, [hasWindow, shouldAttemptReload])
|
}, [shouldAttemptReload])
|
||||||
|
|
||||||
if (shouldAttemptReload) {
|
if (shouldAttemptReload) {
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export const coreApi = $fetch.create({
|
|||||||
setAccessDenied({
|
setAccessDenied({
|
||||||
active: true,
|
active: true,
|
||||||
status: 403,
|
status: 403,
|
||||||
path: typeof window !== 'undefined' ? window.location.pathname : current?.path,
|
path: window.location.pathname,
|
||||||
scope: current?.scope ?? 'api',
|
scope: current?.scope ?? 'api',
|
||||||
reason: detail,
|
reason: detail,
|
||||||
source: 'api',
|
source: 'api',
|
||||||
|
|||||||
@@ -33,10 +33,8 @@ export const SocialAuthButtons = memo(function SocialAuthButtons({
|
|||||||
if (callbackURL) {
|
if (callbackURL) {
|
||||||
return callbackURL
|
return callbackURL
|
||||||
}
|
}
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
return window.location.href
|
return window.location.href
|
||||||
}
|
|
||||||
return
|
|
||||||
}, [callbackURL])
|
}, [callbackURL])
|
||||||
|
|
||||||
const handleSocialClick = useCallback(
|
const handleSocialClick = useCallback(
|
||||||
|
|||||||
@@ -46,11 +46,6 @@ export function SocialConnectionSettings() {
|
|||||||
|
|
||||||
const handleConnect = useCallback(
|
const handleConnect = useCallback(
|
||||||
async (providerId: string, providerName: string) => {
|
async (providerId: string, providerName: string) => {
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
toast.error('当前环境不支持 OAuth 绑定')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = new URL(window.location.href)
|
const url = new URL(window.location.href)
|
||||||
url.searchParams.set('auth-flow', 'link')
|
url.searchParams.set('auth-flow', 'link')
|
||||||
url.searchParams.set('provider', providerId)
|
url.searchParams.set('provider', providerId)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export function useLogin() {
|
|||||||
const loginMutation = useMutation({
|
const loginMutation = useMutation({
|
||||||
mutationFn: async (data: LoginRequest) => {
|
mutationFn: async (data: LoginRequest) => {
|
||||||
if (data.requireRootDomain) {
|
if (data.requireRootDomain) {
|
||||||
const slug = typeof window !== 'undefined' ? getTenantSlugFromHost(window.location.hostname) : null
|
const slug = getTenantSlugFromHost(window.location.hostname)
|
||||||
if (slug !== 'root') {
|
if (slug !== 'root') {
|
||||||
const rootUrl = buildRootTenantUrl('/root-login')
|
const rootUrl = buildRootTenantUrl('/root-login')
|
||||||
window.location.replace(rootUrl)
|
window.location.replace(rootUrl)
|
||||||
@@ -56,7 +56,7 @@ export function useLogin() {
|
|||||||
const isSuperAdmin = session.user.role === 'superadmin'
|
const isSuperAdmin = session.user.role === 'superadmin'
|
||||||
|
|
||||||
if (tenant && !tenant.isPlaceholder && tenant.slug) {
|
if (tenant && !tenant.isPlaceholder && tenant.slug) {
|
||||||
const currentSlug = typeof window !== 'undefined' ? getTenantSlugFromHost(window.location.hostname) : null
|
const currentSlug = getTenantSlugFromHost(window.location.hostname)
|
||||||
if (!isSuperAdmin && tenant.slug !== currentSlug) {
|
if (!isSuperAdmin && tenant.slug !== currentSlug) {
|
||||||
try {
|
try {
|
||||||
const targetUrl = buildTenantUrl(tenant.slug, '/')
|
const targetUrl = buildTenantUrl(tenant.slug, '/')
|
||||||
|
|||||||
@@ -65,10 +65,6 @@ export function buildTenantUrl(slug: string, path = '/'): string {
|
|||||||
throw new Error('Workspace slug is required to build tenant URL.')
|
throw new Error('Workspace slug is required to build tenant URL.')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
throw new TypeError('Cannot build tenant URL outside the browser environment.')
|
|
||||||
}
|
|
||||||
|
|
||||||
const { protocol, hostname, port } = window.location
|
const { protocol, hostname, port } = window.location
|
||||||
const baseDomain = resolveBaseDomain(hostname)
|
const baseDomain = resolveBaseDomain(hostname)
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,6 @@ const STAGE_ORDER: PhotoSyncProgressStage[] = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
const MAX_SYNC_LOGS = 200
|
const MAX_SYNC_LOGS = 200
|
||||||
const PHOTO_SYNC_RESULT_STORAGE_KEY = 'photo-sync:last-result'
|
|
||||||
|
|
||||||
function createInitialStages(totals: PhotoSyncProgressState['totals']): PhotoSyncProgressState['stages'] {
|
function createInitialStages(totals: PhotoSyncProgressState['totals']): PhotoSyncProgressState['stages'] {
|
||||||
return STAGE_ORDER.reduce<PhotoSyncProgressState['stages']>(
|
return STAGE_ORDER.reduce<PhotoSyncProgressState['stages']>(
|
||||||
@@ -73,32 +72,6 @@ export function PhotoPage() {
|
|||||||
const [resolvingConflictId, setResolvingConflictId] = useState<string | null>(null)
|
const [resolvingConflictId, setResolvingConflictId] = useState<string | null>(null)
|
||||||
const [syncProgress, setSyncProgress] = useState<PhotoSyncProgressState | null>(null)
|
const [syncProgress, setSyncProgress] = useState<PhotoSyncProgressState | null>(null)
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const restoreStoredResult = () => {
|
|
||||||
try {
|
|
||||||
const cached = window.sessionStorage.getItem(PHOTO_SYNC_RESULT_STORAGE_KEY)
|
|
||||||
if (!cached) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const parsed = JSON.parse(cached) as { result?: PhotoSyncResult; lastWasDryRun?: boolean | null }
|
|
||||||
if (parsed?.result) {
|
|
||||||
setResult(parsed.result)
|
|
||||||
setLastWasDryRun(parsed.lastWasDryRun ?? null)
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to restore cached photo sync result', error)
|
|
||||||
window.sessionStorage.removeItem(PHOTO_SYNC_RESULT_STORAGE_KEY)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
restoreStoredResult()
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setActiveTab(normalizedInitialTab)
|
setActiveTab(normalizedInitialTab)
|
||||||
}, [normalizedInitialTab])
|
}, [normalizedInitialTab])
|
||||||
@@ -303,16 +276,7 @@ export function PhotoPage() {
|
|||||||
setResult(data)
|
setResult(data)
|
||||||
setLastWasDryRun(context.dryRun)
|
setLastWasDryRun(context.dryRun)
|
||||||
setSyncProgress(null)
|
setSyncProgress(null)
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
try {
|
|
||||||
window.sessionStorage.setItem(
|
|
||||||
PHOTO_SYNC_RESULT_STORAGE_KEY,
|
|
||||||
JSON.stringify({ result: data, lastWasDryRun: context.dryRun }),
|
|
||||||
)
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to persist photo sync result snapshot', error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void summaryQuery.refetch()
|
void summaryQuery.refetch()
|
||||||
void listQuery.refetch()
|
void listQuery.refetch()
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -173,10 +173,6 @@ export function PhotoSyncConflictsPanel({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const confirmAction = (message: string, onConfirm: () => void | Promise<void>) => {
|
const confirmAction = (message: string, onConfirm: () => void | Promise<void>) => {
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
return onConfirm()
|
|
||||||
}
|
|
||||||
|
|
||||||
Prompt.prompt({
|
Prompt.prompt({
|
||||||
title: '确认操作',
|
title: '确认操作',
|
||||||
description: message,
|
description: message,
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
export const getCurrentHostname = (): string | null => {
|
export const getCurrentHostname = (): string | null => {
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
return window.location.hostname
|
return window.location.hostname
|
||||||
} catch {
|
} catch {
|
||||||
@@ -10,10 +7,6 @@ export const getCurrentHostname = (): string | null => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const buildRegistrationUrl = (): string => {
|
export const buildRegistrationUrl = (): string => {
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
return '/platform/welcome'
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { protocol, host } = window.location
|
const { protocol, host } = window.location
|
||||||
return `${protocol}//${host}/platform/welcome`
|
return `${protocol}//${host}/platform/welcome`
|
||||||
@@ -23,10 +16,6 @@ export const buildRegistrationUrl = (): string => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const buildHomeUrl = (): string => {
|
export const buildHomeUrl = (): string => {
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
return '/'
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { protocol, hostname, port } = window.location
|
const { protocol, hostname, port } = window.location
|
||||||
const normalizedPort = port ? `:${port}` : ''
|
const normalizedPort = port ? `:${port}` : ''
|
||||||
|
|||||||
@@ -12,9 +12,6 @@ export function Component() {
|
|||||||
const [password, setPassword] = useState('')
|
const [password, setPassword] = useState('')
|
||||||
const { login, isLoading, error, clearError } = useLogin()
|
const { login, isLoading, error, clearError } = useLogin()
|
||||||
const tenantSlug = useMemo(() => {
|
const tenantSlug = useMemo(() => {
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
return getTenantSlugFromHost(window.location.hostname)
|
return getTenantSlugFromHost(window.location.hostname)
|
||||||
}, [])
|
}, [])
|
||||||
const showEmailLogin = !tenantSlug
|
const showEmailLogin = !tenantSlug
|
||||||
|
|||||||
@@ -14,9 +14,6 @@ export function Component() {
|
|||||||
const { login, isLoading, error, clearError } = useLogin()
|
const { login, isLoading, error, clearError } = useLogin()
|
||||||
|
|
||||||
const tenantSlug = useMemo(() => {
|
const tenantSlug = useMemo(() => {
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
return getTenantSlugFromHost(window.location.hostname)
|
return getTenantSlugFromHost(window.location.hostname)
|
||||||
}, [])
|
}, [])
|
||||||
const rootLoginHref = useMemo(() => {
|
const rootLoginHref = useMemo(() => {
|
||||||
@@ -33,10 +30,6 @@ export function Component() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const targetUrl = buildRootTenantUrl('/root-login')
|
const targetUrl = buildRootTenantUrl('/root-login')
|
||||||
if (window.location.href === targetUrl) {
|
if (window.location.href === targetUrl) {
|
||||||
return
|
return
|
||||||
@@ -89,9 +82,7 @@ export function Component() {
|
|||||||
variant="primary"
|
variant="primary"
|
||||||
className="w-full"
|
className="w-full"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
window.location.href = rootLoginHref
|
window.location.href = rootLoginHref
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Go to root portal
|
Go to root portal
|
||||||
|
|||||||
@@ -30,9 +30,8 @@ function App({ url }: { url?: string }) {
|
|||||||
setCurrentPath(path)
|
setCurrentPath(path)
|
||||||
setIsSidebarOpen(false) // 导航后关闭侧边栏
|
setIsSidebarOpen(false) // 导航后关闭侧边栏
|
||||||
// 在实际应用中,这里会更新浏览器历史记录
|
// 在实际应用中,这里会更新浏览器历史记录
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
window.history.pushState({}, '', path)
|
window.history.pushState({}, '', path)
|
||||||
}
|
|
||||||
},
|
},
|
||||||
[setCurrentPath, setIsSidebarOpen],
|
[setCurrentPath, setIsSidebarOpen],
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user