Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Refactor brand store state management #4972

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion static/js/brand-store/components/Navigation/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { useParams, NavLink } from "react-router-dom";

import Logo from "./Logo";

import { publisherState, brandIdState } from "../../state/atoms";
import { publisherState } from "../../state/publisherState";
import { brandIdState } from "../../state/brandStoreState";
import { brandStoresListSelector } from "../../state/selectors";
import { useBrand, usePublisher } from "../../hooks";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {

import Navigation from "../../components/Navigation";

import { publisherState } from "../../state/atoms";
import { publisherState } from "../../state/publisherState";

function AccountDetails(): ReactNode {
const [subscriptionPreferencesChanged, setSubscriptionPreferencesChanged] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { QueryClient, QueryClientProvider } from "react-query";
import { useRecoilValue } from "recoil";
import { store } from "../../../state/store";
import AccountDetails from "../AccountDetails";
import { publisherState } from "../../../state/atoms";
import { publisherState } from "../../../state/publisherState";

const queryClient = new QueryClient();

Expand Down
5 changes: 2 additions & 3 deletions static/js/brand-store/pages/Model/CreatePolicyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import { useSigningKeys } from "../../hooks";
import {
signingKeysListState,
newSigningKeyState,
brandIdState,
} from "../../state/atoms";
import { brandStoreState } from "../../state/selectors";
} from "../../state/signingKeysState";
import { brandIdState, brandStoreState } from "../../state/brandStoreState";

type Props = {
setShowNotification: Dispatch<SetStateAction<boolean>>;
Expand Down
4 changes: 2 additions & 2 deletions static/js/brand-store/pages/Model/Model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
Notification,
} from "@canonical/react-components";

import { modelsListState, brandIdState } from "../../state/atoms";
import { currentModelState, brandStoreState } from "../../state/selectors";
import { modelsListState, currentModelState } from "../../state/modelsState";
import { brandIdState, brandStoreState } from "../../state/brandStoreState";

import ModelNav from "./ModelNav";
import ModelBreadcrumb from "./ModelBreadcrumb";
Expand Down
7 changes: 4 additions & 3 deletions static/js/brand-store/pages/Model/Policies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import {
import {
policiesListFilterState,
policiesListState,
} from "../../state/policiesState";
import {
signingKeysListState,
newSigningKeyState,
brandIdState,
} from "../../state/atoms";
import { brandStoreState } from "../../state/selectors";
} from "../../state/signingKeysState";
import { brandIdState, brandStoreState } from "../../state/brandStoreState";

import { isClosedPanel, setPageTitle } from "../../utils";

Expand Down
4 changes: 2 additions & 2 deletions static/js/brand-store/pages/Model/PoliciesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { MainTable, Button, Modal, Icon } from "@canonical/react-components";
import AppPagination from "../../components/AppPagination";

import { usePolicies, UsePoliciesResponse } from "../../hooks";
import { brandIdState } from "../../state/atoms";
import { filteredPoliciesListState } from "../../state/selectors";
import { brandIdState } from "../../state/brandStoreState";
import { filteredPoliciesListState } from "../../state/policiesState";

import type { Policy } from "../../types/shared";

Expand Down
10 changes: 5 additions & 5 deletions static/js/brand-store/pages/Models/CreateModelForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import randomstring from "randomstring";
import { checkModelNameExists, setPageTitle } from "../../utils";

import {
brandStoresState,
modelsListState,
newModelState,
brandIdState,
} from "../../state/atoms";
import {
filteredModelsListState,
} from "../../state/modelsState";
import {
brandStoresState,
brandIdState,
brandStoreState,
} from "../../state/selectors";
} from "../../state/brandStoreState";

import type { Store, Model } from "../../types/shared";

Expand Down
7 changes: 3 additions & 4 deletions static/js/brand-store/pages/Models/Models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import { UseQueryResult } from "react-query";
import {
modelsListFilterState,
modelsListState,
policiesListState,
newModelState,
brandIdState,
} from "../../state/atoms";
import { brandStoreState } from "../../state/selectors";
} from "../../state/modelsState";
import { policiesListState } from "../../state/policiesState";
import { brandIdState, brandStoreState } from "../../state/brandStoreState";

import Filter from "../../components/Filter";
import ModelsTable from "./ModelsTable";
Expand Down
2 changes: 1 addition & 1 deletion static/js/brand-store/pages/Models/ModelsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import AppPagination from "../../components/AppPagination";

import { maskString } from "../../utils";

import { filteredModelsListState } from "../../state/selectors";
import { filteredModelsListState } from "../../state/modelsState";

import type { Model as ModelType } from "../../types/shared";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import { checkSigningKeyExists, setPageTitle } from "../../utils";

import {
signingKeysListState,
newSigningKeyState,
brandIdState,
} from "../../state/atoms";
import {
filteredSigningKeysListState,
brandStoreState,
} from "../../state/selectors";
newSigningKeyState,
} from "../../state/signingKeysState";
import { brandIdState, brandStoreState } from "../../state/brandStoreState";

import type { SigningKey } from "../../types/shared";

Expand Down
7 changes: 3 additions & 4 deletions static/js/brand-store/pages/SigningKeys/SigningKeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import { useSigningKeys, useModels } from "../../hooks";
import {
signingKeysListState,
signingKeysListFilterState,
policiesListState,
newSigningKeyState,
brandIdState,
} from "../../state/atoms";
import { brandStoreState } from "../../state/selectors";
} from "../../state/signingKeysState";
import { policiesListState } from "../../state/policiesState";
import { brandIdState, brandStoreState } from "../../state/brandStoreState";

import Filter from "../../components/Filter";
import SigningKeysTable from "./SigningKeysTable";
Expand Down
7 changes: 5 additions & 2 deletions static/js/brand-store/pages/SigningKeys/SigningKeysTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import DeactivateSigningKeyModal from "./DeactivateSigningKeyModal";

import { sortByDateDescending } from "../../utils";

import { filteredSigningKeysListState } from "../../state/selectors";
import { signingKeysListState, brandIdState } from "../../state/atoms";
import {
filteredSigningKeysListState,
signingKeysListState,
} from "../../state/signingKeysState";
import { brandIdState } from "../../state/brandStoreState";

import type { SigningKey } from "../../types/shared";
import type { ItemType } from "../../components/AppPagination/AppPagination";
Expand Down
2 changes: 1 addition & 1 deletion static/js/brand-store/routes/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {

import { fetchStores } from "../../state/slices/brandStoreSlice";
import { brandStoresListSelector } from "../../state/selectors";
import { brandStoresState } from "../../state/atoms";
import { brandStoresState } from "../../state/brandStoreState";

import AccountDetails from "../../pages/AccountDetails";
import Snaps from "../../pages/Snaps";
Expand Down
82 changes: 0 additions & 82 deletions static/js/brand-store/state/atoms/index.ts

This file was deleted.

25 changes: 25 additions & 0 deletions static/js/brand-store/state/brandStoreState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { atom, selectorFamily } from "recoil";

import type { Store } from "../types/shared";

const brandStoresState = atom({
key: "brandStores",
default: [] as Array<Store>,
});

const brandIdState = atom({
key: "brandId",
default: "",
});

const brandStoreState = selectorFamily({
key: "brandStore",
get:
(storeId) =>
({ get }) => {
const brandStores = get(brandStoresState);
return brandStores.find((store) => store.id === storeId);
},
});

export { brandStoresState, brandIdState, brandStoreState };
81 changes: 81 additions & 0 deletions static/js/brand-store/state/modelsState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { atom, selector, selectorFamily } from "recoil";

import { policiesListState } from "./policiesState";

import { Model } from "../types/shared";

function getFilteredModels(models: Array<Model>, filterQuery?: string | null) {
if (!filterQuery) {
return models;
}

return models.filter((model: Model) => {
if (
(model.name && model.name.includes(filterQuery)) ||
(model["api-key"] && model["api-key"].includes(filterQuery)) ||
(model["created-at"] && model["created-at"].includes(filterQuery)) ||
(model["modified-at"] && model["modified-at"].includes(filterQuery))
) {
return true;
}

return false;
});
}

const modelsListState = atom({
key: "modelsList",
default: [] as Array<Model>,
});

const modelsListFilterState = atom({
key: "modelsListFilter",
default: "" as string,
});

const newModelState = atom({
key: "newModel",
default: {
name: "",
apiKey: "",
},
});

const filteredModelsListState = selector<Array<Model>>({
key: "filteredModelsList",
get: ({ get }) => {
const filter = get(modelsListFilterState);
const models = get(modelsListState);
const policies = get(policiesListState);
const modelsWithPolicies = models.map((model) => {
const policy = policies.find(
(policy) => policy["model-name"] === model.name,
);

return {
...model,
"policy-revision": policy ? policy.revision : undefined,
};
});

return getFilteredModels(modelsWithPolicies, filter);
},
});

const currentModelState = selectorFamily({
key: "currentModel",
get:
(modelId) =>
({ get }) => {
const models = get(modelsListState);
return models.find((model) => model.name === modelId);
},
});

export {
modelsListState,
modelsListFilterState,
newModelState,
filteredModelsListState,
currentModelState,
};
Loading
Loading