Skip to content

Commit

Permalink
Submit Transaction: default to last selected method (#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
quietbits authored Oct 21, 2024
1 parent f484c92 commit 747a926
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/app/(sidebar)/transaction/submit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { useStore } from "@/store/useStore";
import * as StellarXdr from "@/helpers/StellarXdr";
import { delayedAction } from "@/helpers/delayedAction";
import { openUrl } from "@/helpers/openUrl";
import { getBlockExplorerLink } from "@/helpers/getBlockExplorerLink";
import { localStorageSubmitMethod } from "@/helpers/localStorageSubmitMethod";

import { Routes } from "@/constants/routes";
import { XDR_TYPE_TRANSACTION_ENVELOPE } from "@/constants/settings";

import { getBlockExplorerLink } from "@/helpers/getBlockExplorerLink";

import { useIsXdrInit } from "@/hooks/useIsXdrInit";
import { useScrollIntoView } from "@/hooks/useScrollIntoView";

Expand Down Expand Up @@ -55,6 +55,8 @@ const SUBMIT_OPTIONS = [
},
];

const SETTING_KEY = "submitMethod";

export default function SubmitTransaction() {
const { network, xdr, transaction } = useStore();
const { blob, updateXdrBlob } = xdr;
Expand Down Expand Up @@ -111,7 +113,14 @@ export default function SubmitTransaction() {

// Set default submit method
useEffect(() => {
setSubmitMethod(isRpcAvailable ? "rpc" : "horizon");
const localStorageMethod = localStorageSubmitMethod.get(SETTING_KEY);

if (localStorageMethod) {
setSubmitMethod(localStorageMethod);
} else {
setSubmitMethod(isRpcAvailable ? "rpc" : "horizon");
}

resetSubmitState();
// Not including resetSubmitState
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -481,6 +490,11 @@ export default function SubmitTransaction() {
setSubmitMethod(s.id);
toggleDropdown(false);
resetSubmitState();

localStorageSubmitMethod.set({
key: SETTING_KEY,
value: s.id,
});
}}
>
<div className="SubmitTx__floater__item__title">
Expand Down
1 change: 1 addition & 0 deletions src/constants/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Networks } from "@stellar/stellar-sdk";
import { Network } from "@/types/types";

export const LOCAL_STORAGE_SETTINGS = "stellar_lab_settings";
export const LOCAL_STORAGE_SAVED_NETWORK = "stellar_lab_network";
export const LOCAL_STORAGE_SAVED_ENDPOINTS_HORIZON =
"stellar_lab_saved_horizon_endpoints";
Expand Down
42 changes: 42 additions & 0 deletions src/helpers/localStorageSubmitMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { LOCAL_STORAGE_SETTINGS } from "@/constants/settings";
import { isEmptyObject } from "@/helpers/isEmptyObject";
import { LabSettings } from "@/types/types";

export const localStorageSubmitMethod = {
get: (key: string) => {
const labSettings = getSettings();
return labSettings[key];
},
set: ({ key, value }: LabSettings) => {
const updatedSettings = {
...getSettings(),
[key]: value,
};

return localStorage.setItem(
LOCAL_STORAGE_SETTINGS,
JSON.stringify(updatedSettings),
);
},
remove: (key: string) => {
const updatedSettings = { ...getSettings() };

if (updatedSettings[key]) {
delete updatedSettings[key];

if (isEmptyObject(updatedSettings)) {
return localStorage.removeItem(LOCAL_STORAGE_SETTINGS);
}
}

return localStorage.setItem(
LOCAL_STORAGE_SETTINGS,
JSON.stringify(updatedSettings),
);
},
};

const getSettings = () => {
const labSettings = localStorage.getItem(LOCAL_STORAGE_SETTINGS);
return labSettings ? (JSON.parse(labSettings) as LabSettings) : {};
};
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TransactionBuildParams } from "@/store/createStore";
export type AnyObject = { [key: string]: any };
export type EmptyObj = Record<PropertyKey, never>;
export type ThemeColorType = "sds-theme-dark" | "sds-theme-light";
export type LabSettings = Record<string, string>;

// =============================================================================
// Helpers
Expand Down

0 comments on commit 747a926

Please sign in to comment.