mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
refactor: remove unnecessary typeof window checks (#24999)
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 />;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user