refactor: remove unnecessary typeof window checks (#24999)

This commit is contained in:
Danielle Maywood
2026-05-06 22:39:59 +01:00
committed by GitHub
parent f5ad6fb4cb
commit 8ac4b9ab45
8 changed files with 9 additions and 15 deletions
+1
View File
@@ -73,6 +73,7 @@ When investigating or editing TypeScript/React code, always use the TypeScript l
directly. Do not prefix them with `window.` (e.g., write
`location.href`, not `window.location.href`). They are globally
available in every browser context.
- Do not use `typeof window`, `typeof document`, or similar runtime checks for browser globals. Coder is a pure SPA so these globals are always available.
- Always use react-query for data fetching. Do not attempt to manage any
data life cycle manually. Do not ever call an `API` function directly
within a component.
@@ -22,8 +22,7 @@ const getPoolSize = (): number => {
return Math.min(Math.max(1, cores - 1), 3);
};
const hasWorkerSupport = (): boolean =>
typeof window !== "undefined" && typeof Worker !== "undefined";
const hasWorkerSupport = (): boolean => typeof Worker !== "undefined";
export const DiffsWorkerPoolProvider: FC<DiffsWorkerPoolProviderProps> = ({
children,
+1 -1
View File
@@ -345,7 +345,7 @@ export function useConversationEditingState(deps: {
: null;
const [{ editorInitialValue, initialEditorState }, setDraftState] = useState(
() => {
if (typeof window === "undefined" || !draftStorageKey) {
if (!draftStorageKey) {
return { editorInitialValue: "", initialEditorState: undefined };
}
const draft = parseStoredDraft(localStorage.getItem(draftStorageKey));
@@ -95,7 +95,7 @@ export const MobileEnterInsertsNewline: Story = {
},
play: async ({ canvasElement, args }) => {
const originalMatchMedia = window.matchMedia;
window.matchMedia = ((query: string) =>
window.matchMedia = (query: string) =>
({
matches: query === "(max-width: 639px)",
media: query,
@@ -105,7 +105,7 @@ export const MobileEnterInsertsNewline: Story = {
dispatchEvent: () => true,
addListener: () => undefined,
removeListener: () => undefined,
}) as MediaQueryList) as typeof window.matchMedia;
}) as MediaQueryList;
try {
const canvas = within(canvasElement);
@@ -51,7 +51,7 @@ const createMatchMediaController = (initialDesktop: boolean) => {
}
};
const matchMedia = ((query: string): MediaQueryList => {
const matchMedia = (query: string): MediaQueryList => {
const isDesktopQuery = /\(\s*min-width\s*:\s*640px\s*\)/.test(query);
return {
matches: isDesktopQuery ? desktop : false,
@@ -94,7 +94,7 @@ const createMatchMediaController = (initialDesktop: boolean) => {
}
},
};
}) as typeof window.matchMedia;
};
return {
matchMedia,
@@ -106,7 +106,7 @@ export const WorkspaceReadyPage: FC<WorkspaceReadyPageProps> = ({
const favicon = getFaviconByStatus(workspace.latest_build);
const [faviconTheme, setFaviconTheme] = useState<"light" | "dark">("dark");
useEffect(() => {
if (typeof window === "undefined" || !window.matchMedia) {
if (!window.matchMedia) {
return;
}
+1 -1
View File
@@ -145,7 +145,7 @@ export const withWebSocket = (Story: FC, { parameters }: StoryContext) => {
removeEventListener(_type: string, _callback: CallbackFn) {}
close() {}
} as unknown as typeof window.WebSocket;
} as unknown as typeof WebSocket;
return <Story />;
};
-6
View File
@@ -5,9 +5,6 @@
* virtual keyboard to pop up unexpectedly.
*/
export const isMobileViewport = (): boolean => {
if (typeof window === "undefined" || !window.matchMedia) {
return false;
}
return window.matchMedia("(max-width: 639px)").matches;
};
@@ -20,8 +17,5 @@ export const isMobileViewport = (): boolean => {
* mobile branch instead of the desktop flyout branch.
*/
export const isBelowMdViewport = (): boolean => {
if (typeof window === "undefined" || !window.matchMedia) {
return false;
}
return window.matchMedia("(max-width: 767px)").matches;
};