Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9220e41530 | ||
|
|
14ac885550 | ||
|
|
643721fc16 |
No files matched your search
@@ -1,9 +1,17 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import type { MutableRefObject, ReactNode } from "react";
|
import type { MutableRefObject, ReactNode } from "react";
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
import { useCallback, useMemo } from "react";
|
||||||
import { Group, Loader, Menu, Switch, Text } from "@mantine/core";
|
import { Group, Loader, Menu, Switch, Text, Tooltip } from "@mantine/core";
|
||||||
import { IconCopy, IconLayoutKanban, IconRefresh, IconSettings, IconTrash } from "@tabler/icons-react";
|
import {
|
||||||
|
IconAlertTriangle,
|
||||||
|
IconCircleCheck,
|
||||||
|
IconCopy,
|
||||||
|
IconLayoutKanban,
|
||||||
|
IconRefresh,
|
||||||
|
IconSettings,
|
||||||
|
IconTrash,
|
||||||
|
} from "@tabler/icons-react";
|
||||||
import type { QueryClient, QueryKey } from "@tanstack/react-query";
|
import type { QueryClient, QueryKey } from "@tanstack/react-query";
|
||||||
import { useIsFetching, useQueryClient } from "@tanstack/react-query";
|
import { useIsFetching, useQueryClient } from "@tanstack/react-query";
|
||||||
|
|
||||||
@@ -11,10 +19,12 @@ import { clientApi } from "@homarr/api/client";
|
|||||||
import { useSession } from "@homarr/auth/client";
|
import { useSession } from "@homarr/auth/client";
|
||||||
import { useRequiredBoard } from "@homarr/boards/context";
|
import { useRequiredBoard } from "@homarr/boards/context";
|
||||||
import { useEditMode } from "@homarr/boards/edit-mode";
|
import { useEditMode } from "@homarr/boards/edit-mode";
|
||||||
|
import { useTimeAgo } from "@homarr/common";
|
||||||
import { useConfirmModal, useModalAction } from "@homarr/modals";
|
import { useConfirmModal, useModalAction } from "@homarr/modals";
|
||||||
import { useSettings } from "@homarr/settings";
|
import { useSettings } from "@homarr/settings";
|
||||||
import { translateIfNecessary } from "@homarr/translation";
|
import { translateIfNecessary } from "@homarr/translation";
|
||||||
import { useI18n, useScopedI18n } from "@homarr/translation/client";
|
import { useI18n, useScopedI18n } from "@homarr/translation/client";
|
||||||
|
import type { TranslationFunction } from "@homarr/translation";
|
||||||
import type { WidgetContextMenuAction } from "@homarr/widgets";
|
import type { WidgetContextMenuAction } from "@homarr/widgets";
|
||||||
import { reduceWidgetOptionsWithDefaultValues, widgetImports } from "@homarr/widgets";
|
import { reduceWidgetOptionsWithDefaultValues, widgetImports } from "@homarr/widgets";
|
||||||
import { WidgetEditModal } from "@homarr/widgets/modals";
|
import { WidgetEditModal } from "@homarr/widgets/modals";
|
||||||
@@ -232,11 +242,14 @@ export const WidgetContextMenu = ({ item, widgetStateRef, children }: WidgetCont
|
|||||||
onClick={handleRefetch}
|
onClick={handleRefetch}
|
||||||
disabled={isWidgetFetching}
|
disabled={isWidgetFetching}
|
||||||
>
|
>
|
||||||
<Group justify="space-between" wrap="nowrap">
|
<Group justify="space-between" wrap="nowrap" gap="sm">
|
||||||
{tMenu("refresh")}
|
{tMenu("refresh")}
|
||||||
<Text size="xs" c="dimmed">
|
<WidgetQueryStatus
|
||||||
<WidgetCacheAge queryClient={queryClient} queryKey={widgetQueryKey} isFetching={isWidgetFetching} />
|
queryClient={queryClient}
|
||||||
</Text>
|
queryKey={widgetQueryKey}
|
||||||
|
isFetching={isWidgetFetching}
|
||||||
|
t={t}
|
||||||
|
/>
|
||||||
</Group>
|
</Group>
|
||||||
</Menu.Item>
|
</Menu.Item>
|
||||||
<Menu.Item
|
<Menu.Item
|
||||||
@@ -273,34 +286,65 @@ export const WidgetContextMenu = ({ item, widgetStateRef, children }: WidgetCont
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const WidgetCacheAge = ({
|
interface WidgetQueryStatusProps {
|
||||||
queryClient,
|
|
||||||
queryKey,
|
|
||||||
isFetching,
|
|
||||||
}: {
|
|
||||||
queryClient: QueryClient;
|
queryClient: QueryClient;
|
||||||
queryKey: QueryKey;
|
queryKey: QueryKey;
|
||||||
isFetching: boolean;
|
isFetching: boolean;
|
||||||
}) => {
|
t: TranslationFunction;
|
||||||
// 1s tick for "just now" → "1s ago" label transition; isFetching prop handles refetch reactivity
|
}
|
||||||
const [, setTick] = useState(0);
|
|
||||||
useEffect(() => {
|
|
||||||
const id = setInterval(() => setTick((n) => n + 1), 1000);
|
|
||||||
return () => clearInterval(id);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (isFetching) return <Loader size={12} />;
|
const WidgetQueryStatus = ({ queryClient, queryKey, isFetching, t }: WidgetQueryStatusProps) => {
|
||||||
|
const queries = queryClient.getQueryCache().findAll({ queryKey });
|
||||||
|
const timestamps = queries.map((query) => query.state.dataUpdatedAt).filter(Boolean);
|
||||||
|
const latest = timestamps.length > 0 ? Math.max(...timestamps) : 0;
|
||||||
|
const ageLabel = useTimeAgo(new Date(latest));
|
||||||
|
|
||||||
const timestamps = queryClient
|
if (isFetching) {
|
||||||
.getQueryCache()
|
return (
|
||||||
.findAll({ queryKey })
|
<Group gap={4} wrap="nowrap">
|
||||||
.map((q) => q.state.dataUpdatedAt)
|
<Loader size={12} />
|
||||||
.filter(Boolean);
|
<Text size="xs" c="dimmed">
|
||||||
if (timestamps.length === 0) return null;
|
{t("item.menu.status.loading")}
|
||||||
|
</Text>
|
||||||
|
</Group>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const latest = Math.max(...timestamps);
|
if (queries.length === 0) {
|
||||||
const seconds = Math.floor((Date.now() - latest) / 1000);
|
return (
|
||||||
if (seconds < 5) return "just now";
|
<Text size="xs" c="dimmed">
|
||||||
if (seconds < 60) return `${seconds}s ago`;
|
{t("item.menu.status.idle")}
|
||||||
return `${Math.floor(seconds / 60)}m ago`;
|
</Text>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasError = queries.some((q) => q.state.status === "error");
|
||||||
|
|
||||||
|
if (hasError) {
|
||||||
|
const errorQuery = queries.find((q) => q.state.status === "error");
|
||||||
|
const errorMessage =
|
||||||
|
errorQuery?.state.error instanceof Error
|
||||||
|
? errorQuery.state.error.message
|
||||||
|
: String(errorQuery?.state.error ?? "Unknown error");
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Tooltip label={errorMessage} multiline position="left" w={250}>
|
||||||
|
<Group gap={4} wrap="nowrap">
|
||||||
|
<IconAlertTriangle size={12} color="var(--mantine-color-red-6)" />
|
||||||
|
<Text size="xs" c="red.6">
|
||||||
|
{t("item.menu.status.error")} · {ageLabel}
|
||||||
|
</Text>
|
||||||
|
</Group>
|
||||||
|
</Tooltip>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Group gap={4} wrap="nowrap">
|
||||||
|
<IconCircleCheck size={12} color="var(--mantine-color-green-6)" />
|
||||||
|
<Text size="xs" c="dimmed">
|
||||||
|
{t("item.menu.status.success")} · {ageLabel}
|
||||||
|
</Text>
|
||||||
|
</Group>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
@@ -153,9 +153,9 @@ export class OverseerrIntegration
|
|||||||
);
|
);
|
||||||
} else if (pendingResults.length > 0) requests = pendingResults;
|
} else if (pendingResults.length > 0) requests = pendingResults;
|
||||||
else if (allResults.length > 0) requests = allResults;
|
else if (allResults.length > 0) requests = allResults;
|
||||||
else return Promise.all([]);
|
else return [];
|
||||||
|
|
||||||
return await Promise.all(
|
const settled = await Promise.allSettled(
|
||||||
requests.map(async (request): Promise<MediaRequest> => {
|
requests.map(async (request): Promise<MediaRequest> => {
|
||||||
const information = await this.getItemInformationAsync(request.media.tmdbId, request.type);
|
const information = await this.getItemInformationAsync(request.media.tmdbId, request.type);
|
||||||
|
|
||||||
@@ -184,6 +184,10 @@ export class OverseerrIntegration
|
|||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return settled
|
||||||
|
.filter((result): result is PromiseFulfilledResult<MediaRequest> => result.status === "fulfilled")
|
||||||
|
.map((result) => result.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected mapRequestStatus(status: UpstreamMediaRequestStatus): MediaRequestStatus {
|
protected mapRequestStatus(status: UpstreamMediaRequestStatus): MediaRequestStatus {
|
||||||
@@ -301,6 +305,10 @@ export class OverseerrIntegration
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to fetch ${type} information for id ${id}: ${response.status} ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (type === "tv") {
|
if (type === "tv") {
|
||||||
const series = (await response.json()) as TvInformation;
|
const series = (await response.json()) as TvInformation;
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ type IntegrationOfKind<TKind extends IntegrationKind> = Omit<Integration, "kind"
|
|||||||
interface Options<TData, TKind extends IntegrationKind, TInput extends Record<string, unknown>> {
|
interface Options<TData, TKind extends IntegrationKind, TInput extends Record<string, unknown>> {
|
||||||
requestAsync: (integration: IntegrationOfKind<TKind>, input: TInput) => Promise<TData>;
|
requestAsync: (integration: IntegrationOfKind<TKind>, input: TInput) => Promise<TData>;
|
||||||
cacheTtlMs?: number;
|
cacheTtlMs?: number;
|
||||||
|
fallbackToStaleOnError?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createIntegrationRequestHandler = <
|
export const createIntegrationRequestHandler = <
|
||||||
@@ -32,6 +33,7 @@ export const createIntegrationRequestHandler = <
|
|||||||
return options.requestAsync(integration, input.options);
|
return options.requestAsync(integration, input.options);
|
||||||
},
|
},
|
||||||
cacheTtlMs: options.cacheTtlMs,
|
cacheTtlMs: options.cacheTtlMs,
|
||||||
|
fallbackToStaleOnError: options.fallbackToStaleOnError,
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
interface Options<TData, TInput extends Record<string, unknown>> {
|
interface Options<TData, TInput extends Record<string, unknown>> {
|
||||||
requestAsync: (input: TInput) => Promise<TData>;
|
requestAsync: (input: TInput) => Promise<TData>;
|
||||||
cacheTtlMs?: number;
|
cacheTtlMs?: number;
|
||||||
|
fallbackToStaleOnError?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
type CacheEntry<TData> = { data: TData; timestamp: Date; expiresAt: number };
|
type CacheEntry<TData> = { data: TData; timestamp: Date; expiresAt: number };
|
||||||
@@ -35,7 +36,6 @@ export const createRequestHandler = <TData, TInput extends Record<string, unknow
|
|||||||
if (cached && Date.now() < cached.expiresAt) {
|
if (cached && Date.now() < cached.expiresAt) {
|
||||||
return { data: cached.data, timestamp: cached.timestamp };
|
return { data: cached.data, timestamp: cached.timestamp };
|
||||||
}
|
}
|
||||||
if (cached) cache.delete(key);
|
|
||||||
|
|
||||||
const existing = inflight.get(key);
|
const existing = inflight.get(key);
|
||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
@@ -55,6 +55,9 @@ export const createRequestHandler = <TData, TInput extends Record<string, unknow
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
inflight.delete(key);
|
inflight.delete(key);
|
||||||
|
if (options.fallbackToStaleOnError && cached) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -13,4 +13,6 @@ export const mediaRequestListRequestHandler = createIntegrationRequestHandler<
|
|||||||
const integrationInstance = await createIntegrationAsync(integration);
|
const integrationInstance = await createIntegrationAsync(integration);
|
||||||
return await integrationInstance.getRequestsAsync();
|
return await integrationInstance.getRequestsAsync();
|
||||||
},
|
},
|
||||||
|
cacheTtlMs: 60_000,
|
||||||
|
fallbackToStaleOnError: true,
|
||||||
});
|
});
|
||||||
@@ -1598,10 +1598,16 @@
|
|||||||
},
|
},
|
||||||
"menu": {
|
"menu": {
|
||||||
"label": {
|
"label": {
|
||||||
"settings": "Settings",
|
"settings": "",
|
||||||
"refresh": "",
|
"refresh": "",
|
||||||
"options": "",
|
"options": "",
|
||||||
"actions": ""
|
"actions": ""
|
||||||
|
},
|
||||||
|
"status": {
|
||||||
|
"success": "",
|
||||||
|
"error": "",
|
||||||
|
"loading": "",
|
||||||
|
"idle": ""
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"create": {
|
"create": {
|
||||||
|
|||||||
@@ -1622,6 +1622,12 @@
|
|||||||
"refresh": "Refresh",
|
"refresh": "Refresh",
|
||||||
"options": "Options",
|
"options": "Options",
|
||||||
"actions": "Actions"
|
"actions": "Actions"
|
||||||
|
},
|
||||||
|
"status": {
|
||||||
|
"success": "OK",
|
||||||
|
"error": "Error",
|
||||||
|
"loading": "Loading",
|
||||||
|
"idle": "Idle"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"create": {
|
"create": {
|
||||||
|
|||||||
Reference in new issue
Block a user