Skip to content

Commit

Permalink
adding persistence using application card
Browse files Browse the repository at this point in the history
  • Loading branch information
brayn003 committed Oct 1, 2024
1 parent bb11bf5 commit 8626c26
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
24 changes: 20 additions & 4 deletions app/client/src/pages/Applications/ApplicationCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { getCurrentUser } from "actions/authActions";
import Card, { ContextMenuTrigger } from "components/common/Card";
import { generateEditedByText } from "./helpers";
import { noop } from "lodash";
import { getLatestGitBranchFromLocal } from "utils/storage";

interface ApplicationCardProps {
application: ApplicationPayload;
Expand Down Expand Up @@ -116,7 +117,22 @@ export function ApplicationCard(props: ApplicationCardProps) {
const dispatch = useDispatch();

const applicationId = props.application?.id;
const baseApplicationId = props.application?.baseId;
const showGitBadge = props.application?.gitApplicationMetadata?.branchName;
const [params, setParams] = useState({});

useEffect(() => {
(async () => {
const storedLatestBranch =
await getLatestGitBranchFromLocal(baseApplicationId);

if (storedLatestBranch) {
setParams({ branch: storedLatestBranch });
} else if (showGitBadge) {
setParams({ branch: showGitBadge });
}
})();
}, [baseApplicationId, showGitBadge]);

useEffect(() => {
let colorCode;
Expand Down Expand Up @@ -276,11 +292,11 @@ export function ApplicationCard(props: ApplicationCardProps) {
// should show correct branch of application when edit mode
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const params: any = {};
// const params: any = {};

if (showGitBadge) {
params.branch = showGitBadge;
}
// if (showGitBadge) {
// params.branch = showGitBadge;
// }

const handleMenuOnClose = (open: boolean) => {
if (!open && !isDeleting) {
Expand Down
3 changes: 3 additions & 0 deletions app/client/src/sagas/GitSyncSagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ import type { JSCollectionDataState } from "ee/reducers/entityReducers/jsActions
import { toast } from "@appsmith/ads";
import { gitExtendedSagas } from "ee/sagas/GitExtendedSagas";
import type { ApplicationPayload } from "entities/Application";
import { setLatestGitBranchInLocal } from "utils/storage";

export function* handleRepoLimitReachedError(response?: ApiResponse) {
const { responseMeta } = response || {};
Expand Down Expand Up @@ -377,6 +378,7 @@ function* switchBranch(action: ReduxAction<string>) {
try {
const branch = action.payload;
const applicationId: string = yield select(getCurrentApplicationId);
const baseApplicationId: string = yield select(getCurrentBaseApplicationId);

response = yield GitSyncAPI.checkoutBranch(applicationId, branch);
const isValidResponse: boolean = yield validateResponse(
Expand Down Expand Up @@ -404,6 +406,7 @@ function* switchBranch(action: ReduxAction<string>) {

yield put(setShowBranchPopupAction(false));
yield put({ type: ReduxActionTypes.SWITCH_GIT_BRANCH_SUCCESS });
yield setLatestGitBranchInLocal(baseApplicationId, trimmedBranch);
const defaultPage = response.data.pages.find((page) => page.isDefault);

if (!existingPage && defaultPage) {
Expand Down
1 change: 0 additions & 1 deletion app/client/src/sagas/InitSagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ export function* getInitResponses({
}: {
applicationId?: string;
basePageId?: string;
branch?: string;
mode?: APP_MODE;
shouldInitialiseUserDetails?: boolean;
// TODO: Fix this the next time the file is edited
Expand Down
43 changes: 43 additions & 0 deletions app/client/src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const STORAGE_KEYS: {
CODE_WIDGET_NAVIGATION_USED: "CODE_WIDGET_NAVIGATION_USED",
OVERRIDDEN_FEATURE_FLAGS: "OVERRIDDEN_FEATURE_FLAGS",
ACTION_TEST_PAYLOAD: "ACTION_TEST_PAYLOAD",
LATEST_GIT_BRANCH: "LATEST_GIT_BRANCH",
};

const store = localforage.createInstance({
Expand Down Expand Up @@ -1087,3 +1088,45 @@ export const storeActionTestPayload = async (payload: {
return false;
}
};

export const setLatestGitBranchInLocal = async (
baseApplicationId: string,
branch: string,
) => {
try {
const storedBranches: Record<string, string> | null = await store.getItem(
STORAGE_KEYS.LATEST_GIT_BRANCH,
);
const newBranches = {
...(storedBranches ?? {}),
[baseApplicationId]: branch,
};

await store.setItem(STORAGE_KEYS.LATEST_GIT_BRANCH, newBranches);

return true;
} catch (error) {
log.error("An error occurred while setting LATEST_GIT_BRANCH");
log.error(error);

return false;
}
};

export const getLatestGitBranchFromLocal = async (
baseApplicationId: string,
) => {
try {
const storedBranches: Record<string, string> | null = await store.getItem(
STORAGE_KEYS.LATEST_GIT_BRANCH,
);
const branch = storedBranches?.[baseApplicationId];

return branch;
} catch (error) {
log.error("An error occurred while fetching LATEST_GIT_BRANCH");
log.error(error);

return null;
}
};

0 comments on commit 8626c26

Please sign in to comment.