Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore(site): refactor deployment values service to react-query#9669

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
BrunoQuaresma merged 5 commits intomainfrombq/refactor-deployment-values-service
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes fromall 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
2 changes: 1 addition & 1 deletionsite/src/api/api.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1006,7 +1006,7 @@ export type DeploymentConfig = {
readonly options: DeploymentOption[];
};

export constgetDeploymentValues = async (): Promise<DeploymentConfig> => {
export constgetDeploymentConfig = async (): Promise<DeploymentConfig> => {
const response = await axios.get(`/api/v2/deployment/config`);
return response.data;
};
Expand Down
15 changes: 15 additions & 0 deletionssite/src/api/queries/deployment.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
import*asAPIfrom"api/api";

exportconstdeploymentConfig=()=>{
return{
queryKey:["deployment","config"],
Copy link
Member

@ParkreinerParkreinerSep 14, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Could the query keys be declaredas const? That would not only make sure that they're kept immutable, but also give more precise type information when accessing elements of the key at specific indices

Copy link
Member

@ParkreinerParkreinerSep 14, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't know if this would cause the compiler to flag anything, though. I'd mainly be worried about type mismatches betweenreadonly arrays with normal arrays

Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think they could be but I don't see to much value on doing that honestly, what use case you are thinking of?

Copy link
Member

@ParkreinerParkreinerSep 14, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yeah, I'm looking at the code again, and I think it has limited value here, just because all the values are strings. I think I generally default to this approach, but it's most useful when you have a key array that mixes different types.

constkey1=["workspaces",23,{running:true}];constkey2=["workspaces",23,{running:true}]asconst;// Inferred as type (string | number | { running: boolean }), because array is arbitrary-lengthconstuserId1=key1[1];// Inferred as type number, because key2 is a fixed-length, immutable tupleconstuserId2=key2[1];

It's more friendly to thenoUncheckedIndexedAccess compiler setting, too

Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I see, but I don't see we using something similar like this - accessing the keys directly - so I think we could wait until it becomes an issue or provide more value. Wdyt? But if you feel strong about that, we can do it for all the query keys, I just would do in a diff PR (since it is going to change non related queries as well).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

No, I'm okay with keeping things as-is. There have been cases where I've passed the query key into the function to construct which API endpoint and which parameters to use, and theas const declarations would help with that, but if there hasn't been a need for that pattern, then I agree with you – it's going to have limited use

queryFn:API.getDeploymentConfig,
};
};

exportconstdeploymentDAUs=()=>{
return{
queryKey:["deployment","daus"],
queryFn:()=>API.getDeploymentDAUs(),
};
};
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,20 +3,16 @@ import { Margins } from "components/Margins/Margins";
import { Stack } from "components/Stack/Stack";
import { Sidebar } from "./Sidebar";
import { createContext, Suspense, useContext, FC } from "react";
import { useMachine } from "@xstate/react";
import { Loader } from "components/Loader/Loader";
import { DAUsResponse } from "api/typesGenerated";
import { deploymentConfigMachine } from "xServices/deploymentConfig/deploymentConfigMachine";
import { RequirePermission } from "components/RequirePermission/RequirePermission";
import { usePermissions } from "hooks/usePermissions";
import { Outlet } from "react-router-dom";
import { DeploymentConfig } from "api/api";
import { useQuery } from "@tanstack/react-query";
import { deploymentConfig } from "api/queries/deployment";

type DeploySettingsContextValue = {
deploymentValues: DeploymentConfig;
getDeploymentValuesError: unknown;
deploymentDAUs?: DAUsResponse;
getDeploymentDAUsError: unknown;
};

const DeploySettingsContext = createContext<
Expand All@@ -34,14 +30,8 @@ export const useDeploySettings = (): DeploySettingsContextValue => {
};

export const DeploySettingsLayout: FC = () => {
const[state] =useMachine(deploymentConfigMachine);
Copy link
Member

@ParkreinerParkreinerSep 14, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Still need to learn XState, but I'm guessing that this line was meant to expose the machine as a read-only value?

Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

it is to instantiate a machine

Copy link
Member

@ParkreinerParkreinerSep 14, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Okay, so it creates the new machine instance, but it's bound as local state for that given component, right? it's not quite a Redux setup where there's only ever one store, and the components selectively subscribe to it?

Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes, it is bound to the component, in this case to the DashboardLayout

constdeploymentConfigQuery =useQuery(deploymentConfig());
const styles = useStyles();
const {
deploymentValues,
deploymentDAUs,
getDeploymentValuesError,
getDeploymentDAUsError,
} = state.context;
const permissions = usePermissions();

return (
Expand All@@ -50,13 +40,10 @@ export const DeploySettingsLayout: FC = () => {
<Stack className={styles.wrapper} direction="row" spacing={6}>
<Sidebar />
<main className={styles.content}>
{deploymentValues ? (
{deploymentConfigQuery.data ? (
<DeploySettingsContext.Provider
value={{
deploymentValues,
getDeploymentValuesError,
deploymentDAUs,
getDeploymentDAUsError,
deploymentValues: deploymentConfigQuery.data,
}}
>
<Suspense fallback={<Loader />}>
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,10 +3,12 @@ import { FC } from "react";
import { Helmet } from "react-helmet-async";
import { pageTitle } from "utils/page";
import { GeneralSettingsPageView } from "./GeneralSettingsPageView";
import { useQuery } from "@tanstack/react-query";
import { deploymentDAUs } from "api/queries/deployment";

const GeneralSettingsPage: FC = () => {
const { deploymentValues, deploymentDAUs, getDeploymentDAUsError} =
useDeploySettings();
const { deploymentValues} = useDeploySettings();
const deploymentDAUsQuery = useQuery(deploymentDAUs());

return (
<>
Expand All@@ -15,8 +17,8 @@ const GeneralSettingsPage: FC = () => {
</Helmet>
<GeneralSettingsPageView
deploymentOptions={deploymentValues.options}
deploymentDAUs={deploymentDAUs}
getDeploymentDAUsError={getDeploymentDAUsError}
deploymentDAUs={deploymentDAUsQuery.data}
deploymentDAUsError={deploymentDAUsQuery.error}
/>
</>
);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,7 +59,7 @@ export const NoDAUs: Story = {
export const DAUError: Story = {
args: {
deploymentDAUs: undefined,
getDeploymentDAUsError: mockApiError({
deploymentDAUsError: mockApiError({
message: "Error fetching DAUs.",
}),
},
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,12 +13,12 @@ import { DeploymentOption } from "api/api";
export type GeneralSettingsPageViewProps = {
deploymentOptions: DeploymentOption[];
deploymentDAUs?: DAUsResponse;
getDeploymentDAUsError: unknown;
deploymentDAUsError: unknown;
};
export const GeneralSettingsPageView = ({
deploymentOptions,
deploymentDAUs,
getDeploymentDAUsError,
deploymentDAUsError,
}: GeneralSettingsPageViewProps): JSX.Element => {
return (
<>
Expand All@@ -28,8 +28,8 @@ export const GeneralSettingsPageView = ({
docsHref={docs("/admin/configure")}
/>
<Stack spacing={4}>
{Boolean(getDeploymentDAUsError) && (
<ErrorAlert error={getDeploymentDAUsError} />
{Boolean(deploymentDAUsError) && (
<ErrorAlert error={deploymentDAUsError} />
)}
{deploymentDAUs && (
<Box height={200} sx={{ mb: 3 }}>
Expand Down
12 changes: 5 additions & 7 deletionssite/src/pages/UsersPage/UsersPage.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,10 +18,10 @@ import { UsersPageView } from "./UsersPageView";
import { useStatusFilterMenu } from "./UsersFilter";
import { useFilter } from "components/Filter/filter";
import { useDashboard } from "components/Dashboard/DashboardProvider";
import { deploymentConfigMachine } from "xServices/deploymentConfig/deploymentConfigMachine";
import { useQuery } from "@tanstack/react-query";
import { getAuthMethods } from "api/api";
import { roles } from "api/queries/roles";
import { deploymentConfig } from "api/queries/deployment";

export const Language = {
suspendDialogTitle: "Suspend user",
Expand DownExpand Up@@ -62,14 +62,12 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
paginationRef,
count,
} = usersState.context;

const { updateUsers: canEditUsers, viewDeploymentValues } = usePermissions();
const rolesQuery = useQuery({ ...roles(), enabled: canEditUsers });

// Ideally this only runs if 'canViewDeployment' is true.
// TODO: Prevent api call if the user does not have the perms.
const [state] = useMachine(deploymentConfigMachine);
const { deploymentValues } = state.context;
const { data: deploymentValues } = useQuery({
...deploymentConfig(),
enabled: viewDeploymentValues,
});
// Indicates if oidc roles are synced from the oidc idp.
// Assign 'false' if unknown.
const oidcRoleSyncEnabled =
Expand Down
2 changes: 1 addition & 1 deletionsite/src/pages/WorkspacePage/WorkspacePage.test.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,7 +38,7 @@ const renderWorkspacePage = async () => {
jest.spyOn(api, "getTemplate").mockResolvedValueOnce(MockTemplate);
jest.spyOn(api, "getTemplateVersionRichParameters").mockResolvedValueOnce([]);
jest
.spyOn(api, "getDeploymentValues")
.spyOn(api, "getDeploymentConfig")
.mockResolvedValueOnce(MockDeploymentConfig);
jest
.spyOn(api, "watchWorkspaceAgentLogs")
Expand Down
View file
Open in desktop

This file was deleted.

2 changes: 1 addition & 1 deletionsite/src/xServices/workspace/workspaceXService.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -781,7 +781,7 @@ async function loadInitialWorkspaceData({
(permissions as Permissions)?.viewDeploymentValues,
);
const deploymentValues = canViewDeploymentValues
? (await API.getDeploymentValues())?.config
? (await API.getDeploymentConfig())?.config
: undefined;
return {
workspace,
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp