mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
feat(integrations): unify OAuth returns and the status envelope
The WHOOP and Fitbit OAuth callbacks redirect back with `?whoop=connected|error` / `?fitbit=connected|error`, but the settings page only read that param for Withings, Polar and Oura — a user returning from a WHOOP or Fitbit round-trip landed on a silently unchanged page with no toast. Extend the generic return handler to all four OAuth providers, add the matching connected/failed/error i18n keys across every locale, and invalidate both the per-card key and the consolidated envelope on a successful return. Polar and Oura already write the shared IntegrationKey ledger but each fired its own `/api/<provider>/status` round-trip from the card, so the page issued one consolidated request plus three per-card ones. Fold Polar and Oura into `/api/integrations/status` and let the cards read a passed view-model instead of fetching, retiring the extra round-trips. The per-provider status routes stay for the iOS and test callers.
This commit is contained in:
1 parent
38a2c7127e
commit
9430e89889
15 files changed
+583
-42
No files matched your search
@@ -23,7 +23,13 @@ vi.mock("@tanstack/react-query", () => ({
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
import { OAuthProviderCard } from "../oauth-provider-card";
|
||||
|
||||
function render({ credentials = false }: { credentials?: boolean } = {}) {
|
||||
function render({
|
||||
credentials = false,
|
||||
viewModel,
|
||||
}: {
|
||||
credentials?: boolean;
|
||||
viewModel?: Parameters<typeof OAuthProviderCard>[0]["viewModel"];
|
||||
} = {}) {
|
||||
return renderToStaticMarkup(
|
||||
<I18nProvider initialLocale="en">
|
||||
<OAuthProviderCard
|
||||
@@ -33,6 +39,7 @@ function render({ credentials = false }: { credentials?: boolean } = {}) {
|
||||
icon={Watch}
|
||||
dataHref="/insights/sleep"
|
||||
credentials={credentials}
|
||||
viewModel={viewModel}
|
||||
/>
|
||||
</I18nProvider>,
|
||||
);
|
||||
@@ -74,6 +81,25 @@ describe("OAuthProviderCard — parked + test + data-link parity", () => {
|
||||
expect(html).toContain('href="/insights/sleep"');
|
||||
});
|
||||
|
||||
it("reads off the passed view-model instead of the per-card fetch (04-M2)", () => {
|
||||
// The per-card useQuery mock returns null; the card must render the
|
||||
// connected state from the supplied envelope view-model alone, proving the
|
||||
// /api/<provider>/status round-trip is no longer the source.
|
||||
statusPayload = null;
|
||||
const html = render({
|
||||
viewModel: {
|
||||
connected: true,
|
||||
configured: true,
|
||||
available: true,
|
||||
state: "connected",
|
||||
lastSuccessAt: "2026-06-01T00:00:00.000Z",
|
||||
lastError: null,
|
||||
},
|
||||
});
|
||||
expect(html).toContain('data-testid="polar-data-link"');
|
||||
expect(html).toContain("Test connection");
|
||||
});
|
||||
|
||||
it("does not render the data link or test button when disconnected", () => {
|
||||
statusPayload = {
|
||||
connected: false,
|
||||
|
||||
@@ -36,7 +36,11 @@ import {
|
||||
import { TestConnectionButton } from "@/components/settings/test-connection-button";
|
||||
import { apiFetchRaw, apiGet, apiPost } from "@/lib/api/api-fetch";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { invalidateKeys, measurementDependentKeys } from "@/lib/query-keys";
|
||||
import {
|
||||
invalidateKeys,
|
||||
measurementDependentKeys,
|
||||
queryKeys,
|
||||
} from "@/lib/query-keys";
|
||||
import { IntegrationSetupGuideLink } from "./setup-guide-link";
|
||||
|
||||
export interface OAuthProviderStatus {
|
||||
@@ -85,6 +89,13 @@ export interface OAuthProviderCardProps {
|
||||
* button. The endpoint is the PUT target (e.g. `/api/polar/credentials`). */
|
||||
credentials?: boolean;
|
||||
enabled?: boolean;
|
||||
/**
|
||||
* When provided, the card reads its status from this view-model (sourced off
|
||||
* the consolidated `/api/integrations/status` envelope) instead of firing its
|
||||
* own `/api/<provider>/status` round-trip. v1.17.1 folds Polar/Oura onto the
|
||||
* same envelope WHOOP/Fitbit already use, so the page reads from one source.
|
||||
*/
|
||||
viewModel?: OAuthProviderStatus;
|
||||
}
|
||||
|
||||
export function OAuthProviderCard({
|
||||
@@ -95,6 +106,7 @@ export function OAuthProviderCard({
|
||||
dataHref,
|
||||
credentials = false,
|
||||
enabled = true,
|
||||
viewModel,
|
||||
}: OAuthProviderCardProps) {
|
||||
const { t } = useTranslations();
|
||||
const queryClient = useQueryClient();
|
||||
@@ -107,12 +119,17 @@ export function OAuthProviderCard({
|
||||
null,
|
||||
);
|
||||
|
||||
const { data: status } = useQuery({
|
||||
// Read off the consolidated envelope when a view-model is passed; otherwise
|
||||
// fall back to the per-card status fetch (still used by any caller that has
|
||||
// not been migrated onto the envelope). The fetch is disabled once a
|
||||
// view-model is supplied so the page makes one request, not one-per-card.
|
||||
const { data: fetchedStatus } = useQuery({
|
||||
queryKey: statusQueryKey,
|
||||
queryFn: async () => apiGet<OAuthProviderStatus>(`/api/${provider}/status`),
|
||||
enabled,
|
||||
enabled: enabled && !viewModel,
|
||||
refetchOnWindowFocus: true,
|
||||
});
|
||||
const status = viewModel ?? fetchedStatus;
|
||||
|
||||
async function handleSaveCredentials(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
@@ -134,6 +151,11 @@ export function OAuthProviderCard({
|
||||
setClientId("");
|
||||
setClientSecret("");
|
||||
queryClient.invalidateQueries({ queryKey: statusQueryKey });
|
||||
// The card may read off the consolidated envelope — invalidate it too
|
||||
// so the saved-credentials state repaints regardless of the source.
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.integrationsStatus(),
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
const json = await res.json();
|
||||
@@ -156,6 +178,9 @@ export function OAuthProviderCard({
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: statusQueryKey });
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.integrationsStatus(),
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -6,10 +6,19 @@
|
||||
|
||||
import { CircleDot } from "lucide-react";
|
||||
|
||||
import { OAuthProviderCard } from "@/components/settings/integrations/oauth-provider-card";
|
||||
import {
|
||||
OAuthProviderCard,
|
||||
type OAuthProviderStatus,
|
||||
} from "@/components/settings/integrations/oauth-provider-card";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
export function OuraCard({ enabled = true }: { enabled?: boolean }) {
|
||||
export function OuraCard({
|
||||
enabled = true,
|
||||
viewModel,
|
||||
}: {
|
||||
enabled?: boolean;
|
||||
viewModel?: OAuthProviderStatus;
|
||||
}) {
|
||||
return (
|
||||
<OAuthProviderCard
|
||||
provider="oura"
|
||||
@@ -19,6 +28,7 @@ export function OuraCard({ enabled = true }: { enabled?: boolean }) {
|
||||
dataHref="/insights/sleep"
|
||||
credentials
|
||||
enabled={enabled}
|
||||
viewModel={viewModel}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -6,10 +6,19 @@
|
||||
|
||||
import { Watch } from "lucide-react";
|
||||
|
||||
import { OAuthProviderCard } from "@/components/settings/integrations/oauth-provider-card";
|
||||
import {
|
||||
OAuthProviderCard,
|
||||
type OAuthProviderStatus,
|
||||
} from "@/components/settings/integrations/oauth-provider-card";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
export function PolarCard({ enabled = true }: { enabled?: boolean }) {
|
||||
export function PolarCard({
|
||||
enabled = true,
|
||||
viewModel,
|
||||
}: {
|
||||
enabled?: boolean;
|
||||
viewModel?: OAuthProviderStatus;
|
||||
}) {
|
||||
return (
|
||||
<OAuthProviderCard
|
||||
provider="polar"
|
||||
@@ -19,6 +28,7 @@ export function PolarCard({ enabled = true }: { enabled?: boolean }) {
|
||||
dataHref="/insights/sleep"
|
||||
credentials
|
||||
enabled={enabled}
|
||||
viewModel={viewModel}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -18,7 +18,13 @@ import { queryKeys } from "@/lib/query-keys";
|
||||
// v1.4.19 Phase A5: the redundant in-card status banner is gone — the
|
||||
// IntegrationStatusPill now owns state + last-sync presentation, and
|
||||
// the actionable error message is shown inline above the action row.
|
||||
export type IntegrationKey = "withings" | "whoop" | "fitbit" | "moodlog";
|
||||
export type IntegrationKey =
|
||||
| "withings"
|
||||
| "whoop"
|
||||
| "fitbit"
|
||||
| "moodlog"
|
||||
| "polar"
|
||||
| "oura";
|
||||
export type IntegrationState =
|
||||
| "connected"
|
||||
| "error_transient"
|
||||
@@ -52,6 +58,11 @@ export interface IntegrationStatusViewModel {
|
||||
// moodLog webhook secret + entry count.
|
||||
webhookSecret?: string | null;
|
||||
entryCount?: number;
|
||||
// Polar / Oura OAuth card: usable-credentials + BYO-key flags. `available`
|
||||
// greys out the connect button when no credentials resolve; `hasOwnCredentials`
|
||||
// drives the saved-placeholder UI.
|
||||
available?: boolean;
|
||||
hasOwnCredentials?: boolean;
|
||||
}
|
||||
|
||||
export interface IntegrationStatusEnvelope {
|
||||
|
||||
Reference in new issue
Block a user