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

Cleaning up the code #66

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
23 changes: 12 additions & 11 deletions src/components/BranchListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,17 @@ export default function BranchListItem({
title="Open Branch in Gitpod"
onAction={async () => {
visitBranch?.(branch, repository);
if (dashboardPreferences.access_token) {
const defaultOrg = await LocalStorage.getItem("default_organization");
if (defaultOrg !== undefined && WorkspaceManager.api) {
createWorksapceFromContext(defaultOrg.toString(),branchURL);
} else {
push(<DefaultOrgForm />)
}
} else {
OpenInGitpod(branchURL, "Branch", repository, branch.branchName);
}
OpenInGitpod(branchURL, "Branch", repository, push, branch.branchName);
// if (dashboardPreferences.access_token) {
// const defaultOrg = await LocalStorage.getItem("default_organization");
// if (defaultOrg !== undefined && WorkspaceManager.api) {
// createWorksapceFromContext(defaultOrg.toString(), branchURL);
// } else {
// push(<DefaultOrgForm />)
// }
// } else {
// OpenInGitpod(branchURL, "Branch", repository, branch.branchName);
// }
}}
shortcut={{ modifiers: ["cmd"], key: "g" }}
/>
Expand Down Expand Up @@ -228,7 +229,7 @@ export default function BranchListItem({
}
shortcut={{ modifiers: ["cmd"], key: "e" }}
/>
<Action.Push title="Switch Default Organization" shortcut={{ modifiers: ["cmd", "shift"], key: "o"}} target={<DefaultOrgForm />}/>
<Action.Push title="Switch Default Organization" shortcut={{ modifiers: ["cmd", "shift"], key: "o" }} target={<DefaultOrgForm />} />
</ActionPanel>
}
/>
Expand Down
121 changes: 67 additions & 54 deletions src/components/DefaultOrgForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Action, ActionPanel, Detail, Form, LocalStorage, Toast, getPreferenceValues, showToast, useNavigation } from "@raycast/api"
import { Action, ActionPanel, Detail, Form, LocalStorage, Toast, getPreferenceValues, showToast, useNavigation, Preferences } from "@raycast/api"
import { usePromise } from "@raycast/utils"
import { useState } from "react";
import { useEffect, useState } from "react";

import { IOrganizationError } from "../api/Gitpod/Models/IOrganizationError";
import { IOrganization } from "../api/Gitpod/Models/IOrganizations"
Expand All @@ -9,64 +9,77 @@ import { dashboardPreferences } from "../preferences/dashboard_preferences";
import { ErrorListView, errorMessage } from "./errorListView";

interface defaultOrgParams {
revalidate?: () => Promise<void>;
revalidate?: () => Promise<void>;
}

export default function DefaultOrgForm({ revalidate }: defaultOrgParams) {
const preferences = getPreferenceValues<dashboardPreferences>();

export default function DefaultOrgForm({revalidate} : defaultOrgParams) {
const { pop } = useNavigation();

const preferences = getPreferenceValues<dashboardPreferences>();
const [defaultPrefValue, setDefaultPrefValue] = useState<string | undefined>(undefined);

const { pop } = useNavigation();
const [isUnauthorised, setIsUnauthorized] = useState<boolean>(false);
const [isNetworkError, setNetworkError] = useState<boolean>(false);
const [data, setData] = useState<IOrganization[]>([])

const [isUnauthorised, setIsUnauthorized] = useState<boolean>(false);
const [isNetworkError, setNetworkError] = useState<boolean>(false);
const [data, setData] = useState<IOrganization[]>([])

const { isLoading , error } = usePromise(async () => {
const data = await IOrganization.fetchOrganization(preferences.access_token ?? "");
setData(data)
}, [], {
onError: (error : Error) => {
const e = ( error as any ) as {code: string }
if (e.code === "ENOTFOUND"){
setNetworkError(true);
}
const gitpodError = error as IOrganizationError;
if (gitpodError.code === 401 || gitpodError.code === 500){
setIsUnauthorized(true);
}
}
})
useEffect(() => {
if (data) {
const loadDefaultValues = async () => {
const res = await LocalStorage.getItem<string>("default_organization") as string;
setDefaultPrefValue(res);
};
loadDefaultValues();
}
}, [data]);

if (isUnauthorised || isNetworkError){
return <ErrorListView message={isUnauthorised ? errorMessage.invalidAccessToken : errorMessage.networkError} />
const { isLoading, error } = usePromise(async () => {
const data = await IOrganization.fetchOrganization(preferences.access_token ?? "");
setData(data)
}, [], {
onError: (error: Error) => {
const e = (error as any) as { code: string }
if (e.code === "ENOTFOUND") {
setNetworkError(true);
}
const gitpodError = error as IOrganizationError;
if (gitpodError.code === 401 || gitpodError.code === 500) {
setIsUnauthorized(true);
}
}
})

return (
error ? <Detail metadata={"Failed to Fetch Organization, Try Again"} /> :
<Form navigationTitle="Select default organization" isLoading={isLoading} actions={
<ActionPanel>
<Action.SubmitForm
onSubmit={
async (values) => {
await LocalStorage.setItem("default_organization", values["default_organization"]);
const toast = await showToast({
title: "Saving default organization",
style: Toast.Style.Animated
})
revalidate && await revalidate();
setTimeout(() => {
toast.hide()
pop();
}, 2000);
}} />
</ActionPanel>
} >
<Form.Dropdown id="default_organization" placeholder="Select Default Organization" title="Default Organization">
{ data.length === 0 ? <Form.Dropdown.Item title="Loading Organization" value="" /> :
data?.map((org) => <Form.Dropdown.Item title={org.name} value={org.orgId} />)}
</Form.Dropdown>
</Form>
)
}
if (isUnauthorised || isNetworkError) {
return <ErrorListView message={isUnauthorised ? errorMessage.invalidAccessToken : errorMessage.networkError} />
}

return (
(defaultPrefValue && data.length > 0) && (<Form navigationTitle="Select default organization" isLoading={isLoading} actions={
<ActionPanel>
<Action.SubmitForm
onSubmit={
async (values) => {
await LocalStorage.setItem("default_organization", values["default_organization"]);
const toast = await showToast({
title: "Saving default organization",
style: Toast.Style.Animated
})
revalidate && await revalidate();
setTimeout(() => {
toast.hide()
pop();
}, 1000);
}} />
</ActionPanel>
} >
<Form.Dropdown
id="default_organization"
info="This Organization will be used as the default to Create Workspaces from within Raycast"
title="Default Organization"
defaultValue={defaultPrefValue}
>
{data.map(org => <Form.Dropdown.Item title={org.name} value={org.orgId} />)}
</Form.Dropdown>
</Form>)
)
}
25 changes: 13 additions & 12 deletions src/components/IssueListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,17 @@ export default function IssueListItem({
title="Open Issue in Gitpod"
onAction={async () => {
visitIssue?.(issue);
if (dashboardPreferences.access_token) {
const defaultOrg = await LocalStorage.getItem("default_organization");
if (defaultOrg !== undefined && WorkspaceManager.api) {
createWorksapceFromContext(defaultOrg.toString(),issue.url);
} else {
push(<DefaultOrgForm />)
}
} else {
OpenInGitpod(issue.url, "Issue", issue.repository.nameWithOwner, issue.title);
}
OpenInGitpod(issue.url, "Issue", issue.repository.nameWithOwner, push, issue.title);
// if (dashboardPreferences.access_token) {
// const defaultOrg = await LocalStorage.getItem("default_organization");
// if (defaultOrg !== undefined && WorkspaceManager.api) {
// createWorksapceFromContext(defaultOrg.toString(),issue.url);
// } else {
// push(<DefaultOrgForm />)
// }
// } else {
// OpenInGitpod(issue.url, "Issue", issue.repository.nameWithOwner, issue.title);
// }
}}
shortcut={{ modifiers: ["cmd"], key: "g" }}
/>
Expand Down Expand Up @@ -148,7 +149,7 @@ export default function IssueListItem({
{!fromCache && (
<Action
title="Add Issue to Recents"
onAction={async() => {
onAction={async () => {
visitIssue?.(issue);
await showToast({
title: `Added Issue "#${issue.number}" to Recents`,
Expand Down Expand Up @@ -185,7 +186,7 @@ export default function IssueListItem({
}
shortcut={{ modifiers: ["cmd"], key: "e" }}
/>
<Action.Push title="Switch Default Organization" shortcut={{ modifiers: ["cmd", "shift"], key: "o"}} target={<DefaultOrgForm />}/>
<Action.Push title="Switch Default Organization" shortcut={{ modifiers: ["cmd", "shift"], key: "o" }} target={<DefaultOrgForm />} />
</ActionPanel>
}
/>
Expand Down
24 changes: 8 additions & 16 deletions src/components/PullRequestListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,13 @@ export default function PullRequestListItem({
title="Open PR in Gitpod"
onAction={async () => {
visitPullReq?.(pullRequest);
if (dashboardPreferences.access_token) {
const defaultOrg = await LocalStorage.getItem("default_organization");
if (defaultOrg !== undefined && WorkspaceManager.api) {
createWorksapceFromContext(defaultOrg.toString(), pullRequest.permalink);
} else {
push(<DefaultOrgForm />)
}
} else {
OpenInGitpod(
pullRequest.permalink,
"Pull Request",
pullRequest.repository.nameWithOwner,
pullRequest.title
);
}
OpenInGitpod(
pullRequest.permalink,
"Pull Request",
pullRequest.repository.nameWithOwner,
push,
pullRequest.title,
);
}}
shortcut={{ modifiers: ["cmd"], key: "g" }}
/>
Expand Down Expand Up @@ -208,7 +200,7 @@ export default function PullRequestListItem({
}}
/>
)}
<Action.Push title="Switch Default Organization" shortcut={{ modifiers: ["cmd", "shift"], key: "o"}} target={<DefaultOrgForm />}/>
<Action.Push title="Switch Default Organization" shortcut={{ modifiers: ["cmd", "shift"], key: "o" }} target={<DefaultOrgForm />} />
</ActionPanel>
}
/>
Expand Down
25 changes: 13 additions & 12 deletions src/components/RepositoryListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Color, List, ActionPanel, Action, open, useNavigation, Icon, showToast, Toast, getPreferenceValues, LocalStorage} from "@raycast/api";
import { Color, List, ActionPanel, Action, open, useNavigation, Icon, showToast, Toast, getPreferenceValues, LocalStorage } from "@raycast/api";
import { MutatePromise, usePromise } from "@raycast/utils";

import { GitpodIcons, UIColors } from "../../constants";
Expand Down Expand Up @@ -141,16 +141,17 @@ export default function RepositoryListItem({
title="Trigger Workspace"
onAction={async () => {
onVisit(repository);
if (dashboardPreferences.access_token) {
const defaultOrg = await LocalStorage.getItem("default_organization");
if (defaultOrg !== undefined && WorkspaceManager.api) {
createWorksapceFromContext(defaultOrg.toString(),repository.url);
} else {
push(<DefaultOrgForm />)
}
} else {
OpenInGitpod(repository.url, "Repository", repository.nameWithOwner)
}
OpenInGitpod(repository.url, "Repository", repository.nameWithOwner, push)
// if (dashboardPreferences.access_token) {
// const defaultOrg = await LocalStorage.getItem("default_organization");
// if (defaultOrg !== undefined && WorkspaceManager.api) {
// createWorksapceFromContext(defaultOrg.toString(), repository.url);
// } else {
// push(<DefaultOrgForm />)
// }
// } else {
// OpenInGitpod(repository.url, "Repository", repository.nameWithOwner)
// }
}}
shortcut={{ modifiers: ["cmd"], key: "g" }}
/>
Expand All @@ -161,7 +162,7 @@ export default function RepositoryListItem({
}
shortcut={{ modifiers: ["cmd"], key: "e" }}
/>
<Action.Push title="Switch Default Organization" shortcut={{ modifiers: ["cmd", "shift"], key: "o"}} target={<DefaultOrgForm />}/>
<Action.Push title="Switch Default Organization" shortcut={{ modifiers: ["cmd", "shift"], key: "o" }} target={<DefaultOrgForm />} />
</ActionPanel>
}
/>
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/createWorkspaceFromContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { WorkspaceManager } from "../api/Gitpod/WorkspaceManager";
import { Preferences } from "../preferences/repository_preferences";

export default async function createWorksapceFromContext(defaultOrg: string,context_url: string) {

const EditorPreferences = getPreferenceValues<Preferences>();

IWorkspace.create(WorkspaceManager.api, {
Expand All @@ -28,4 +27,4 @@ export default async function createWorksapceFromContext(defaultOrg: string,cont
type: LaunchType.UserInitiated
})
}, 3000);
}
}
29 changes: 26 additions & 3 deletions src/helpers/openInGitpod.ts → src/helpers/openInGitpod.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { LocalStorage, open, showToast, Toast } from "@raycast/api";
import { getPreferenceValues } from "@raycast/api";

import { WorkspaceManager } from "../api/Gitpod/WorkspaceManager";
import DefaultOrgForm from "../components/DefaultOrgForm";
import { dashboardPreferences } from "../preferences/dashboard_preferences";
import { getGitpodEndpoint } from "../preferences/gitpod_endpoint";

import createWorksapceFromContext from "./createWorkspaceFromContext";

interface Preferences {
preferredEditor: string;
useLatest: boolean;
preferredEditorClass: "g1-standard" | "g1-large";
}


export async function getPreferencesForContext(
type: "Branch" | "Pull Request" | "Issue" | "Repository",
repository: string,
Expand Down Expand Up @@ -38,21 +42,40 @@ export async function getPreferencesForContext(
return preferences;
}

async function createWorksapce(
contextUrl: string,
push: (jsx: JSX.Element) => void,
) {
const defaultOrg = await LocalStorage.getItem("default_organization");
if (defaultOrg && WorkspaceManager.api) {
createWorksapceFromContext(defaultOrg.toString(), contextUrl);
} else {
push(<DefaultOrgForm />);
}
}

export default async function OpenInGitpod(
contextUrl: string,
type: "Branch" | "Pull Request" | "Issue" | "Repository",
repository: string,
context?: string
push: (jsx: JSX.Element) => void,
context?: string,
) {
const gitpodEndpoint = getGitpodEndpoint();
const preferences = await getPreferencesForContext(type, repository, context);

const dashboardPreferences = getPreferenceValues<dashboardPreferences>();
if (dashboardPreferences.access_token) {
return createWorksapce(contextUrl, push);
}

try {
await showToast({
const toast = await showToast({
title: "Launching your workspace",
style: Toast.Style.Animated,
});
setTimeout(() => {
toast.hide();
if (preferences.preferredEditor === "ssh") {
// TODO: Add a check if dotsh files are loaded in future
open(
Expand Down
Loading