Skip to content

Commit 778ed8e

Browse files
committed
Refactor Export components to use getSetting for consistent settings retrieval
- Updated ExportDialog and ExportGithub components to replace localStorageGet with getSetting for fetching GitHub OAuth and repository settings. - Modified extensionSettings utility functions to use arrow functions and provide a default value for getSetting. - Improved code readability and maintainability by standardizing the method of accessing settings.
1 parent 379fd91 commit 778ed8e

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

apps/roam/src/components/Export.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const ExportDialog: ExportDialogComponent = ({
119119
initialExportDestination,
120120
}) => {
121121
const [selectedRepo, setSelectedRepo] = useState<string>(
122-
getSetting<string>("selected-repo", ""),
122+
getSetting("selected-repo"),
123123
);
124124
const exportId = useMemo(() => nanoid(), []);
125125
useEffect(() => {
@@ -171,7 +171,7 @@ const ExportDialog: ExportDialogComponent = ({
171171
}, [initialPanel]);
172172
const [includeDiscourseContext, setIncludeDiscourseContext] = useState(false);
173173
const [gitHubAccessToken, setGitHubAccessToken] = useState<string | null>(
174-
getSetting<string | null>("oauth-github", null),
174+
getSetting<string>("oauth-github"),
175175
);
176176

177177
const [canSendToGitHub, setCanSendToGitHub] = useState(false);
@@ -185,8 +185,8 @@ const ExportDialog: ExportDialogComponent = ({
185185
content: string;
186186
setError: (error: string) => void;
187187
}): Promise<{ status: number }> => {
188-
const gitHubAccessToken = localStorageGet("github-oauth");
189-
const selectedRepo = localStorageGet("github-repo");
188+
const gitHubAccessToken = getSetting("github-oauth");
189+
const selectedRepo = getSetting("github-repo");
190190

191191
const encoder = new TextEncoder();
192192
const uint8Array = encoder.encode(content);
@@ -233,8 +233,8 @@ const ExportDialog: ExportDialogComponent = ({
233233
setError: (error: string) => void;
234234
pageUid: string;
235235
}): Promise<{ status: number }> => {
236-
const gitHubAccessToken = localStorageGet("github-oauth");
237-
const selectedRepo = localStorageGet("github-repo");
236+
const gitHubAccessToken = getSetting("github-oauth");
237+
const selectedRepo = getSetting("github-repo");
238238
try {
239239
// https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#create-an-issue
240240
const response = await apiPost({
@@ -253,7 +253,7 @@ const ExportDialog: ExportDialogComponent = ({
253253
});
254254
if (response.status === 401) {
255255
setError("Authentication failed. Please log in again.");
256-
localStorageSet("github-oauth", "");
256+
setSetting("github-oauth", "");
257257
return { status: 401 };
258258
}
259259

@@ -699,7 +699,7 @@ const ExportDialog: ExportDialogComponent = ({
699699
if (activeExportDestination === "github") {
700700
const { title, content } = files[0];
701701
const githubDestination =
702-
localStorageGet("github-destination");
702+
getSetting("github-destination");
703703
try {
704704
let status;
705705
if (githubDestination === "File") {

apps/roam/src/components/ExportGithub.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import apiGet from "roamjs-components/util/apiGet";
1212
import apiPost from "roamjs-components/util/apiPost";
1313
import { getNodeEnv } from "roamjs-components/util/env";
1414
import getExtensionApi from "roamjs-components/util/extensionApiContext";
15-
import { setSetting } from "~/utils/extensionSettings";
15+
import { getSetting, setSetting } from "~/utils/extensionSettings";
1616

1717
export type UserReposResponse = {
1818
data: [
@@ -45,7 +45,7 @@ export const fetchInstallationStatus = async () => {
4545
domain: "https://api.github.com",
4646
path: "user/installations",
4747
headers: {
48-
Authorization: `token ${localStorageGet("github-oauth")}`,
48+
Authorization: `token ${getSetting("github-oauth")}`,
4949
},
5050
});
5151
const installations = res.installations;
@@ -75,14 +75,12 @@ export const ExportGithub = ({
7575
const [repos, setRepos] = useState<UserRepos>(initialRepos);
7676
const [state, setState] = useState("");
7777
const [gitHubAccessToken, _setGitHubAccessToken] = useState<string>(
78-
localStorageGet("github-oauth"),
78+
getSetting("github-oauth"),
7979
);
8080
const [githubDestination, _setGithubDestination] =
81-
useState<GitHubDestination>(
82-
(localStorageGet("github-destination") as GitHubDestination) || "File",
83-
);
84-
const [selectedRepo, _setSelectedRepo] = useState(
85-
localStorageGet("github-repo"),
81+
useState<GitHubDestination>(getSetting("github-destination") || "File");
82+
const [selectedRepo, _setSelectedRepo] = useState<string>(
83+
getSetting("github-repo"),
8684
);
8785
const showGitHubLogin = isGitHubAppInstalled && !gitHubAccessToken;
8886
const repoAndDestinationSelectEnabled =
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import getExtensionAPI from "roamjs-components/util/extensionApiContext";
22

3-
export function getSetting<T>(key: string, defaultValue?: T): T {
3+
export const getSetting = <T>(key: string, defaultValue: T = null as T): T => {
44
const extensionAPI = getExtensionAPI();
5-
const value = extensionAPI.settings.get(key);
5+
const value = extensionAPI.settings.get(key) as T;
6+
return value ?? defaultValue;
7+
};
68

7-
if (value !== undefined && value !== null) {
8-
return value as T;
9-
}
10-
return defaultValue as T;
11-
}
12-
13-
export function setSetting<T>(key: string, value: T): void {
9+
export const setSetting = <T>(key: string, value: T): void => {
1410
const extensionAPI = getExtensionAPI();
1511
extensionAPI.settings.set(key, value);
16-
}
12+
};

0 commit comments

Comments
 (0)