Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BrowserProfileReporter.tsx

34 lines1.6 KB

1import { AuthPreviewOperateScope, type BrowserProfile } from "@t3tools/contracts";
2import { useEffect } from "react";
3​
4import { useClientSettings, useClientSettingsHydrated } from "~/hooks/useSettings";
5import { useServerConfigs } from "~/state/entities";
6import { useEnvironments } from "~/state/environments";
7import { previewEnvironment } from "~/state/preview";
8import { useEnvironmentsWithScope } from "~/state/session";
9import { useAtomCommand } from "~/state/use-atom-command";
10​
11const selectProfiles = (settings: { readonly browserProfiles: ReadonlyArray<BrowserProfile> }) =>
12 settings.browserProfiles;
13const selectDefaultProfileId = (settings: { readonly browserDefaultProfileId: string }) =>
14 settings.browserDefaultProfileId;
15​
16export function BrowserProfileReporter() {
17 const hydrated = useClientSettingsHydrated();
18 const profiles = useClientSettings(selectProfiles);
19 const defaultProfileId = useClientSettings(selectDefaultProfileId);
20 const { environments } = useEnvironments();
21 const serverConfigs = useServerConfigs();
22 const operable = useEnvironmentsWithScope(environments, AuthPreviewOperateScope);
23 const report = useAtomCommand(previewEnvironment.reportProfiles, { reportFailure: false });
24​
25 useEffect(() => {
26 if (!hydrated) return;
27 for (const [environmentId, config] of serverConfigs) {
28 if (!config.environment.capabilities.serverBrowser || !operable.has(environmentId)) continue;
29 void report({ environmentId, input: { profiles, defaultProfileId } });
30 }
31 }, [defaultProfileId, hydrated, operable, profiles, report, serverConfigs]);
32​
33 return null;