Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove useContextBridge hook #1292

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions src/renderer/Core/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import type { IpcRendererEvent } from "electron";
import { useEffect, useState } from "react";
import { Route, Routes, useNavigate } from "react-router";
import { Extension } from "./Extension";
import {
useContextBridge,
useExcludedSearchResultItems,
useFavorites,
useScrollBar,
useSearchResultItems,
} from "./Hooks";
import { useExcludedSearchResultItems, useFavorites, useScrollBar, useSearchResultItems } from "./Hooks";
import { useI18n } from "./I18n";
import { Search } from "./Search";
import { Settings } from "./Settings";
Expand All @@ -19,8 +13,7 @@ import { ThemeContext } from "./ThemeContext";
import { useAppCssProperties } from "./useAppCssProperties";

export const App = () => {
const { contextBridge } = useContextBridge();
const [theme, setTheme] = useState<Theme>(getTheme(contextBridge));
const [theme, setTheme] = useState<Theme>(getTheme(window.ContextBridge));
const [shouldPreferDarkColors, setShouldPreferDarkColors] = useState<boolean>(
window.matchMedia("(prefers-color-scheme: dark)").matches,
);
Expand All @@ -31,24 +24,24 @@ export const App = () => {
const navigate = useNavigate();
const { appCssProperties } = useAppCssProperties();

useI18n({ contextBridge });
useI18n();
useScrollBar({ document, theme });

useEffect(() => {
const navigateToEventHandler = (_: IpcRendererEvent, { pathname }: { pathname: string }) =>
navigate({ pathname });

const nativeThemeChangedEventHandler = () => {
setTheme(getTheme(contextBridge));
setTheme(getTheme(window.ContextBridge));
setShouldPreferDarkColors(window.matchMedia("(prefers-color-scheme: dark)").matches);
};

contextBridge.ipcRenderer.on("navigateTo", navigateToEventHandler);
contextBridge.ipcRenderer.on("nativeThemeChanged", nativeThemeChangedEventHandler);
window.ContextBridge.ipcRenderer.on("navigateTo", navigateToEventHandler);
window.ContextBridge.ipcRenderer.on("nativeThemeChanged", nativeThemeChangedEventHandler);

return () => {
contextBridge.ipcRenderer.off("navigateTo", navigateToEventHandler);
contextBridge.ipcRenderer.off("nativeThemeChanged", nativeThemeChangedEventHandler);
window.ContextBridge.ipcRenderer.off("navigateTo", navigateToEventHandler);
window.ContextBridge.ipcRenderer.off("nativeThemeChanged", nativeThemeChangedEventHandler);
};
}, []);

Expand Down
10 changes: 4 additions & 6 deletions src/renderer/Core/Components/BasicSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BaseLayout } from "@Core/BaseLayout";
import { Header } from "@Core/Header";
import { useContextBridge } from "@Core/Hooks";
import { getNextSearchResultItemId } from "@Core/Search/Helpers/getNextSearchResultItemId";
import { getPreviousSearchResultItemId } from "@Core/Search/Helpers/getPreviousSearchResultItemId";
import { SearchResultList } from "@Core/Search/SearchResultList";
Expand All @@ -24,7 +23,6 @@ export const BasicSearch = ({
debounceDurationInMs,
showGoBackButton,
}: BasicSearchProps) => {
const { contextBridge } = useContextBridge();
const navigate = useNavigate();
const [searchTerm, setSearchTerm] = useState<string>("");
const [isLoading, setIsLoading] = useState<boolean>(false);
Expand Down Expand Up @@ -64,17 +62,17 @@ export const BasicSearch = ({
const selectedSearchResultItem = getSelectedSearchResultItem();

if (selectedSearchResultItem) {
await contextBridge.invokeAction(selectedSearchResultItem.defaultAction);
await window.ContextBridge.invokeAction(selectedSearchResultItem.defaultAction);
}
};

const clickHandlers: Record<string, (s: SearchResultItem) => void> = {
selectSearchResultItem: (s) => setSelectedItemId(s.id),
invokeSearchResultItem: (s) => contextBridge.invokeAction(s.defaultAction),
invokeSearchResultItem: (s) => window.ContextBridge.invokeAction(s.defaultAction),
};

const handleSearchResultItemClickEvent = (searchResultItem: SearchResultItem) => {
const singleClickBehavior = contextBridge.getSettingValue(
const singleClickBehavior = window.ContextBridge.getSettingValue(
"keyboardAndMouse.singleClickBehavior",
"selectSearchResultItem",
);
Expand All @@ -83,7 +81,7 @@ export const BasicSearch = ({
};

const handleSearchResultItemDoubleClickEvent = (searchResultItem: SearchResultItem) => {
const doubleClickBehavior = contextBridge.getSettingValue(
const doubleClickBehavior = window.ContextBridge.getSettingValue(
"keyboardAndMouse.doubleClickBehavior",
"invokeSearchResultItem",
);
Expand Down
1 change: 0 additions & 1 deletion src/renderer/Core/Hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./useContextBridge";
export * from "./useExcludedSearchResultItems";
export * from "./useExtensionProps";
export * from "./useExtensionSetting";
Expand Down
3 changes: 0 additions & 3 deletions src/renderer/Core/Hooks/useContextBridge.ts

This file was deleted.

14 changes: 7 additions & 7 deletions src/renderer/Core/Hooks/useExcludedSearchResultItems.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { useEffect, useState } from "react";
import { useContextBridge } from "./useContextBridge";

export const useExcludedSearchResultItems = () => {
const { contextBridge } = useContextBridge();

const [excludedSearchResultItemIds, setExcludedSearchResultItemIds] = useState<string[]>(
contextBridge.getExcludedSearchResultItemIds(),
window.ContextBridge.getExcludedSearchResultItemIds(),
);

useEffect(() => {
const excludedSearchResultItemsUpdatedEventHandler = () =>
setExcludedSearchResultItemIds(contextBridge.getExcludedSearchResultItemIds());
setExcludedSearchResultItemIds(window.ContextBridge.getExcludedSearchResultItemIds());

contextBridge.ipcRenderer.on("excludedSearchResultItemsUpdated", excludedSearchResultItemsUpdatedEventHandler);
window.ContextBridge.ipcRenderer.on(
"excludedSearchResultItemsUpdated",
excludedSearchResultItemsUpdatedEventHandler,
);

return () => {
contextBridge.ipcRenderer.off(
window.ContextBridge.ipcRenderer.off(
"excludedSearchResultItemsUpdated",
excludedSearchResultItemsUpdatedEventHandler,
);
Expand Down
4 changes: 1 addition & 3 deletions src/renderer/Core/Hooks/useExtensionProps.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { ExtensionProps } from "@Core/ExtensionProps";
import { useNavigate } from "react-router";
import { useContextBridge } from "./useContextBridge";

export const useExtensionProps = (): ExtensionProps => {
const navigate = useNavigate();
const { contextBridge } = useContextBridge();

return {
contextBridge,
contextBridge: window.ContextBridge,
goBack: () => navigate({ pathname: "/" }),
};
};
9 changes: 3 additions & 6 deletions src/renderer/Core/Hooks/useExtensionSetting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getExtensionSettingKey } from "@common/Core/Extension";
import { useState } from "react";
import { useContextBridge } from "./useContextBridge";

export const useExtensionSetting = <Value>({
extensionId,
Expand All @@ -11,22 +10,20 @@ export const useExtensionSetting = <Value>({
key: string;
isSensitive?: boolean;
}) => {
const { contextBridge } = useContextBridge();

const settingKey = getExtensionSettingKey(extensionId, key);

const [value, setValue] = useState<Value>(
contextBridge.getSettingValue(
window.ContextBridge.getSettingValue(
settingKey,
contextBridge.getExtensionSettingDefaultValue(extensionId, key),
window.ContextBridge.getExtensionSettingDefaultValue(extensionId, key),
isSensitive,
),
);

const updateValue = async (updatedValue: Value) => {
setValue(updatedValue);

await contextBridge.updateSettingValue(settingKey, updatedValue, isSensitive);
await window.ContextBridge.updateSettingValue(settingKey, updatedValue, isSensitive);
};

return { value, updateValue };
Expand Down
11 changes: 4 additions & 7 deletions src/renderer/Core/Hooks/useFavorites.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { useEffect, useState } from "react";
import { useContextBridge } from "./useContextBridge";

export const useFavorites = () => {
const { contextBridge } = useContextBridge();

const [favorites, setFavorites] = useState<string[]>(contextBridge.getFavorites());
const [favorites, setFavorites] = useState<string[]>(window.ContextBridge.getFavorites());

useEffect(() => {
const favoritesUpdatedEventHandler = () => setFavorites(contextBridge.getFavorites());
const favoritesUpdatedEventHandler = () => setFavorites(window.ContextBridge.getFavorites());

contextBridge.ipcRenderer.on("favoritesUpdated", favoritesUpdatedEventHandler);
window.ContextBridge.ipcRenderer.on("favoritesUpdated", favoritesUpdatedEventHandler);

return () => {
contextBridge.ipcRenderer.off("favoritesUpdated", favoritesUpdatedEventHandler);
window.ContextBridge.ipcRenderer.off("favoritesUpdated", favoritesUpdatedEventHandler);
};
}, []);

Expand Down
11 changes: 4 additions & 7 deletions src/renderer/Core/Hooks/useSearchResultItems.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import type { SearchResultItem } from "@common/Core";
import { useEffect, useState } from "react";
import { useContextBridge } from "./useContextBridge";

export const useSearchResultItems = () => {
const { contextBridge } = useContextBridge();

const [searchResultItems, setSearchResultItems] = useState<SearchResultItem[]>(
contextBridge.getSearchResultItems(),
window.ContextBridge.getSearchResultItems(),
);

useEffect(() => {
const searchIndexUpdatedEventHandler = () => setSearchResultItems(contextBridge.getSearchResultItems());
const searchIndexUpdatedEventHandler = () => setSearchResultItems(window.ContextBridge.getSearchResultItems());

contextBridge.ipcRenderer.on("searchIndexUpdated", searchIndexUpdatedEventHandler);
window.ContextBridge.ipcRenderer.on("searchIndexUpdated", searchIndexUpdatedEventHandler);

return () => {
contextBridge.ipcRenderer.off("searchIndexUpdated", searchIndexUpdatedEventHandler);
window.ContextBridge.ipcRenderer.off("searchIndexUpdated", searchIndexUpdatedEventHandler);
};
}, []);

Expand Down
7 changes: 2 additions & 5 deletions src/renderer/Core/Hooks/useSetting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState } from "react";
import { useContextBridge } from "./useContextBridge";

export const useSetting = <Value>({
key,
Expand All @@ -10,13 +9,11 @@ export const useSetting = <Value>({
defaultValue: Value;
isSensitive?: boolean;
}) => {
const { contextBridge } = useContextBridge();

const [value, setValue] = useState<Value>(contextBridge.getSettingValue(key, defaultValue, isSensitive));
const [value, setValue] = useState<Value>(window.ContextBridge.getSettingValue(key, defaultValue, isSensitive));

const updateValue = async (updatedValue: Value) => {
setValue(updatedValue);
await contextBridge.updateSettingValue(key, updatedValue, isSensitive);
await window.ContextBridge.updateSettingValue(key, updatedValue, isSensitive);
};

return { value, updateValue };
Expand Down
7 changes: 3 additions & 4 deletions src/renderer/Core/I18n/useI18n.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import type { ContextBridge } from "@common/Core";
import { use } from "i18next";
import { initReactI18next } from "react-i18next";
import { createResources } from "./createResources";
import { getCoreResources } from "./getCoreResources";
import { getExtensionResources } from "./getExtensionResources";

export const useI18n = ({ contextBridge }: { contextBridge: ContextBridge }) => {
export const useI18n = () => {
return use(initReactI18next).init({
resources: createResources([
...getCoreResources(),
...getExtensionResources(contextBridge.getExtensionResources()),
...getExtensionResources(window.ContextBridge.getExtensionResources()),
]),
lng: contextBridge.getSettingValue("general.language", "en-US"),
lng: window.ContextBridge.getSettingValue("general.language", "en-US"),
fallbackLng: "en-US",
});
};
11 changes: 6 additions & 5 deletions src/renderer/Core/Search/ActionsMenu/ActionsMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { KeyboardShortcut } from "@Core/Components";
import { useContextBridge } from "@Core/Hooks";
import { type SearchResultItem, type SearchResultItemAction } from "@common/Core";
import {
Button,
Expand Down Expand Up @@ -38,15 +37,17 @@ export const ActionsMenu = ({
onOpenChange,
keyboardShortcut,
}: AdditionalActionsProps) => {
const { contextBridge } = useContextBridge();
const { t } = useTranslation();

const toasterId = useId("copiedToClipboardToasterId");
const { dispatchToast } = useToastController(toasterId);

const actions = searchResultItem ? getActions(searchResultItem, favorites) : [];

const showKeyboardShortcuts = contextBridge.getSettingValue<boolean>("appearance.showKeyboardShortcuts", true);
const showKeyboardShortcuts = window.ContextBridge.getSettingValue<boolean>(
"appearance.showKeyboardShortcuts",
true,
);

useEffect(() => {
const copiedToClipboardHandler = () =>
Expand All @@ -57,10 +58,10 @@ export const ActionsMenu = ({
{ intent: "success", position: "bottom" },
);

contextBridge.ipcRenderer.on("copiedToClipboard", copiedToClipboardHandler);
window.ContextBridge.ipcRenderer.on("copiedToClipboard", copiedToClipboardHandler);

return () => {
contextBridge.ipcRenderer.off("copiedToClipboard", copiedToClipboardHandler);
window.ContextBridge.ipcRenderer.off("copiedToClipboard", copiedToClipboardHandler);
};
}, []);

Expand Down
5 changes: 1 addition & 4 deletions src/renderer/Core/Search/ConfirmationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@ import {
DialogTitle,
} from "@fluentui/react-components";
import { t } from "i18next";
import { useContextBridge } from "../Hooks";

type ConfirmationDialogProps = {
action?: SearchResultItemAction;
closeDialog: () => void;
};

export const ConfirmationDialog = ({ action, closeDialog }: ConfirmationDialogProps) => {
const { contextBridge } = useContextBridge();

const invokeAction = async () => {
if (action) {
closeDialog();
await contextBridge.invokeAction(action);
await window.ContextBridge.invokeAction(action);
}
};

Expand Down
Loading
Loading