Skip to content

Commit

Permalink
barrel for endpoint connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofstetter Benjamin (extern) committed Apr 6, 2023
1 parent 2245c2e commit 0f7915c
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 123 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@
"commands": [
{
"command": "sparql-notebook.connect",
"title": "Connect to Database",
"title": "Connect to SPARQL Endpoint",
"icon": {
"dark": "assets/dark/dbconnection.svg",
"light": "assets/light/dbconnection.svg"
"dark": "assets/dark/endpoint-connected.svg",
"light": "assets/light/endpoint-connected.svg"
}
},
{
Expand Down
31 changes: 15 additions & 16 deletions src/extension/commands.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as vscode from "vscode";
import {
ConnectionData,
ConnectionListItem,
SparqlNotebookConnections,
} from "./db-connection";
EndpointConfiguration,
EndpointConnectionListItem,
EndpointConnections,
} from "./sparql-connection-menu";

import { SparqlClient } from "./simple-client";

Expand All @@ -12,11 +12,11 @@ import { storageKey, globalConnection } from "./extension";
export const deleteConnectionConfiguration =
(
context: vscode.ExtensionContext,
connectionsSidepanel: SparqlNotebookConnections
connectionsSidepanel: EndpointConnections
) =>
async (item: ConnectionListItem) => {
async (item: EndpointConnectionListItem) => {
const without = context.globalState
.get<ConnectionData[]>(storageKey, [])
.get<EndpointConfiguration[]>(storageKey, [])
.filter(({ name }) => name !== item.config.name);
context.globalState.update(storageKey, without);
await context.secrets.delete(item.config.name);
Expand All @@ -31,7 +31,7 @@ export const deleteConnectionConfiguration =
export const addNewConnectionConfiguration =
(
context: vscode.ExtensionContext,
connectionsSidepanel: SparqlNotebookConnections
connectionsSidepanel: EndpointConnections
) =>
async () => {
const displayName = await getUserInput("Database Display Name ", true);
Expand All @@ -51,14 +51,14 @@ export const addNewConnectionConfiguration =
});
const passwordKey = `sparql-notebook.${displayName}`;
await context.secrets.store(passwordKey, password || "");
const config: ConnectionData = {
const config: EndpointConfiguration = {
name: displayName,
endpointURL: endpointURL || "",
user: user ?? "",
passwordKey,
};
const existing = context.globalState
.get<ConnectionData[]>(storageKey, [])
.get<EndpointConfiguration[]>(storageKey, [])
.filter(({ name }) => name !== displayName);
existing.push(config);
context.globalState.update(storageKey, existing);
Expand All @@ -68,9 +68,9 @@ export const addNewConnectionConfiguration =
export const connectToDatabase =
(
context: vscode.ExtensionContext,
connectionsSidepanel: SparqlNotebookConnections
connectionsSidepanel: EndpointConnections
) =>
async (item?: ConnectionListItem) => {
async (item?: EndpointConnectionListItem) => {
let selectedName: string;
if (!item) {
const names = context.globalState
Expand All @@ -88,7 +88,7 @@ export const connectToDatabase =
selectedName = item.config.name;
}
const match = context.globalState
.get<ConnectionData[]>(storageKey, [])
.get<EndpointConfiguration[]>(storageKey, [])
.find(({ name }) => name === selectedName);
if (!match) {
vscode.window.showErrorMessage(
Expand Down Expand Up @@ -121,11 +121,10 @@ export const connectToDatabase =
vscode.window.showInformationMessage(
`Successfully connected to "${match.name}"`
);
} catch (err) {
} catch (err: any) {
console.log(err);
vscode.window.showErrorMessage(
// @ts-ignore
`Failed to connect to "${match.name}": ${err.message}`
`Failed to connect to "${match.name}": ${err?.message}`
);
globalConnection.connection = null;
connectionsSidepanel.setActive(null);
Expand Down
96 changes: 0 additions & 96 deletions src/extension/db-connection.ts

This file was deleted.

15 changes: 9 additions & 6 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import * as vscode from "vscode";

import { SparqlNotebookController } from "./sparql-notebook-controller";
import { SparqlNotebookSerializer } from "./sparql-notebook-serializer";
import { ConnectionData, SparqlNotebookConnections } from "./db-connection";
import { EndpointConfiguration, EndpointConnections } from "./sparql-connection-menu";

import {
addNewConnectionConfiguration,
connectToDatabase,
deleteConnectionConfiguration,
} from "./commands";
import { exportToMarkdown } from "./export-command";

export const storageKey = "sparql-notebook-connections";
export const globalConnection: { connection: DbConnection | null } = {

export const globalConnection: { connection: EndpointConnection | null } = {
connection: null,
};

Expand All @@ -23,7 +26,7 @@ export function activate(context: vscode.ExtensionContext) {
);
context.subscriptions.push(new SparqlNotebookController());

const connectionsSidepanel = new SparqlNotebookConnections(context);
const connectionsSidepanel = new EndpointConnections(context);
vscode.window.registerTreeDataProvider(storageKey, connectionsSidepanel);

vscode.commands.registerCommand(
Expand All @@ -48,8 +51,8 @@ export function activate(context: vscode.ExtensionContext) {
);
}

export function deactivate() {}
export function deactivate() { }

export interface DbConnection {
data: ConnectionData;
export interface EndpointConnection {
data: EndpointConfiguration;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export interface EndpointConfiguration {
name: string;
endpointURL: string;
user: string;
passwordKey: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as vscode from "vscode";
import * as path from "path";
import { EndpointConfiguration } from "./endpoint-configuration.model";


export class EndpointConnectionListItem extends vscode.TreeItem {
constructor(
public readonly config: EndpointConfiguration,
public readonly isActive: boolean,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public readonly command?: vscode.Command
) {
super(config.name, collapsibleState);

if (isActive) {
this.iconPath = {
dark: path.join(assetsPath, "dark", "endpoint-connected.svg"),
light: path.join(assetsPath, "light", "endpoint-connected.svg"),
};
this.description = "Connected";
} else {
this.iconPath = {
dark: path.join(assetsPath, "dark", "endpoint.svg"),
light: path.join(assetsPath, "light", "endpoint.svg"),
};
console.log(this.iconPath)
this.description = "Inactive";
}
this.contextValue = "database";
}
}

export const assetsPath = path.join(__filename, "..", "..", "assets");
63 changes: 63 additions & 0 deletions src/extension/sparql-connection-menu/endpoint-connections.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as vscode from "vscode";
import { storageKey } from "../extension";

import { EndpointConfiguration } from "./endpoint-configuration.model";
import { EndpointConnectionListItem } from "./endpoint-connection-list-item.class";

export class EndpointConnections
implements vscode.TreeDataProvider<EndpointConnectionListItem | vscode.TreeItem>
{
private _onDidChangeTreeData: vscode.EventEmitter<
EndpointConnectionListItem | undefined | void
> = new vscode.EventEmitter<EndpointConnectionListItem | undefined | void>();
readonly onDidChangeTreeData: vscode.Event<
EndpointConnectionListItem | undefined | void
> = this._onDidChangeTreeData.event;

constructor(public readonly context: vscode.ExtensionContext) {
this.refresh();
this.activeConn = null;
}

refresh(): void {
this._onDidChangeTreeData.fire();
}

getTreeItem(element: EndpointConnectionListItem): vscode.TreeItem {
return element;
}

public setActive(connName: string | null) {
this.activeConn = connName;
this.refresh();
}
private activeConn: string | null;

getChildren(element?: EndpointConnectionListItem): Thenable<vscode.TreeItem[]> {
if (element) {
return Promise.resolve([
new vscode.TreeItem(
`EndPointURL: ${element.config.endpointURL}`,
vscode.TreeItemCollapsibleState.None
),
new vscode.TreeItem(
`User: ${element.config.user}`,
vscode.TreeItemCollapsibleState.None
),
]);
}
const connections =
this.context.globalState.get<EndpointConfiguration[] | null>(storageKey) ?? [];

return Promise.resolve(
connections.map(
(config) =>
new EndpointConnectionListItem(
config,
config.name === this.activeConn,
vscode.TreeItemCollapsibleState.Collapsed
)
)
);
}
}
4 changes: 4 additions & 0 deletions src/extension/sparql-connection-menu/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// barrel file for sparql endpoint connection menu
export { EndpointConfiguration } from './endpoint-configuration.model';
export { EndpointConnectionListItem } from "./endpoint-connection-list-item.class";
export { EndpointConnections } from "./endpoint-connections.class";

0 comments on commit 0f7915c

Please sign in to comment.