mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-27 14:31:17 -07:00
Chore: full react hooks eslint compliance (#7040)
This commit is contained in:
@@ -2,13 +2,13 @@ import useSWR from "swr";
|
||||
|
||||
import { formatProxyUrl } from "./api-helpers";
|
||||
|
||||
export default function useWidgetAPI(widget, ...options) {
|
||||
const config = {};
|
||||
if (options && options[1]?.refreshInterval) {
|
||||
config.refreshInterval = options[1].refreshInterval;
|
||||
export default function useWidgetAPI(widget, endpoint, queryParams, swrConfig = {}) {
|
||||
const config = { ...swrConfig };
|
||||
if (queryParams?.refreshInterval) {
|
||||
config.refreshInterval = queryParams.refreshInterval;
|
||||
}
|
||||
let url = formatProxyUrl(widget, ...options);
|
||||
if (options[0] === "") {
|
||||
let url = formatProxyUrl(widget, endpoint, queryParams);
|
||||
if (endpoint === "") {
|
||||
url = null;
|
||||
}
|
||||
const { data, error, mutate } = useSWR(url, config);
|
||||
|
||||
@@ -28,6 +28,18 @@ describe("utils/proxy/use-widget-api", () => {
|
||||
expect(result.mutate).toBe("m");
|
||||
});
|
||||
|
||||
it("passes additional SWR configuration separately from the proxy query", () => {
|
||||
useSWR.mockReturnValue({ data: undefined, error: undefined, mutate: vi.fn() });
|
||||
|
||||
const widget = { service_group: "g", service_name: "s", index: 0 };
|
||||
const onSuccess = vi.fn();
|
||||
useWidgetAPI(widget, "status", { refreshInterval: 123 }, { onSuccess });
|
||||
|
||||
const [url, config] = useSWR.mock.calls[0];
|
||||
expect(url).not.toContain("onSuccess");
|
||||
expect(config).toEqual({ refreshInterval: 123, onSuccess });
|
||||
});
|
||||
|
||||
it("returns data.error as the top-level error", () => {
|
||||
const dataError = { message: "nope" };
|
||||
useSWR.mockReturnValue({ data: { error: dataError }, error: undefined, mutate: vi.fn() });
|
||||
|
||||
Reference in New Issue
Block a user