Skip to content

Commit

Permalink
Add Profile export & import
Browse files Browse the repository at this point in the history
  • Loading branch information
undyingwraith committed Jan 18, 2025
1 parent 10e58ba commit c0bd7e3
Show file tree
Hide file tree
Showing 46 changed files with 879 additions and 381 deletions.
17 changes: 14 additions & 3 deletions packages/core/src/Application.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Container, interfaces } from 'inversify';
import { IApplication } from './IApplication';
import { IApplication, IApplicationSymbol } from './IApplication';
import { IModule } from './Modules/IModule';
import { IApplicationRegistration } from './IApplicationRegistration';

export class Application implements IApplication, IApplicationRegistration {
public constructor(container?: Container) {
if (container) {
this.container = container;
}
this.container.bind(IApplicationSymbol).toConstantValue(this);
}

public registerConstant<T>(service: T, identifier: symbol) {
if (this.container.isBound(identifier)) {
if (this.container.isCurrentBound(identifier)) {
this.container.unbind(identifier);
}
this.container.bind<T>(identifier).toConstantValue(service);
Expand All @@ -16,7 +23,7 @@ export class Application implements IApplication, IApplicationRegistration {
}

public register<T>(service: interfaces.Newable<T>, identifier: symbol) {
if (this.container.isBound(identifier)) {
if (this.container.isCurrentBound(identifier)) {
this.container.unbind(identifier);
}
this.container.bind<T>(identifier).to(service);
Expand All @@ -30,6 +37,10 @@ export class Application implements IApplication, IApplicationRegistration {
return this.container.get<T>(identifier);
}

public createChildApplication(): Application {
return new Application(this.container.createChild());
}

/**
* The IOC {@link Container} of the {@link IApplication}.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/IApplication.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const IApplicationSymbol = Symbol.for('IApplication');

/**
* The main application holding everything together.
*/
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/Services/FileExportService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { injectable } from 'inversify';

@injectable()
export class FileExportService {
exportText(contents: string, filename: string) {
const uri = "data:text/plain;charset=utf-8," + encodeURIComponent(contents);
this.export(uri, filename);
}

exportJson(obj: object, filename: string) {
const uri = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(obj));
this.export(uri, filename);
}

export(uri: string, filename: string): void {
var link = document.createElement("a");
link.download = filename;
link.href = uri;
link.click();
}
}
1 change: 1 addition & 0 deletions packages/core/src/Services/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './Indexer';
export { FileExportService } from './FileExportService';
export { HotkeyService } from './HotkeyService';
export { IndexManager } from './IndexManager';
export { LocalStorageKeyValueStore } from './LocalStorageKeyValueStore';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export * from './Services';
export * from './util';
export { Application } from './Application';
export { Defaults } from './Defaults';
export { type IApplication } from './IApplication';
export { type IApplication, IApplicationSymbol } from './IApplication';
export { Regexes } from './Regexes';
export { uuid } from './util';
20 changes: 10 additions & 10 deletions packages/desktop/src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ const nodeService: INodeService = {
}

const agentVersion = `helia/2.0.0 ${libp2pInfo.name}/${libp2pInfo.version} UserAgent=${process.version}`;
const datastore = new LevelDatastore(`${getProfileFolder(profile.name)}/data`);
const datastore = new LevelDatastore(`${getProfileFolder(profile.id)}/data`);
await datastore.open();
const blockstore = new FsBlockstore(`${getProfileFolder(profile.name)}/blocks`);
const blockstore = new FsBlockstore(`${getProfileFolder(profile.id)}/blocks`);
await blockstore.open();

const helia = await createHelia({
Expand Down Expand Up @@ -204,17 +204,17 @@ const configService: IConfigurationService = {
return [];
}
},
getProfile(name: string): IProfile {
return JSON.parse(fs.readFileSync(getProfileFolder(name) + '/profile.json', 'utf-8'));
getProfile(id: string): IProfile {
return JSON.parse(fs.readFileSync(getProfileFolder(id) + '/profile.json', 'utf-8'));
},
setProfile(name: string, profile: IProfile) {
if (!fs.existsSync(getProfileFolder(name))) {
fs.mkdirSync(getProfileFolder(name));
setProfile(id: string, profile: IProfile) {
if (!fs.existsSync(getProfileFolder(id))) {
fs.mkdirSync(getProfileFolder(id));
}
fs.writeFileSync(getProfileFolder(name) + '/profile.json', JSON.stringify(profile));
fs.writeFileSync(getProfileFolder(id) + '/profile.json', JSON.stringify(profile));
},
removeProfile(name) {
fs.rmdirSync(getProfileFolder(name), { recursive: true });
removeProfile(id) {
fs.rmSync(getProfileFolder(id), { recursive: true });
},
};

Expand Down
12 changes: 5 additions & 7 deletions packages/desktop/src/renderer/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { IpmcLauncher, ThemeContextProvider } from "ipmc-ui";
import { IpmcApp } from "ipmc-ui";

function App() {
return (
<ThemeContextProvider>
<IpmcLauncher
nodeService={window.nodeService}
configService={window.configService}
/>
</ThemeContextProvider>
<IpmcApp
nodeService={window.nodeService}
configService={window.configService}
/>
);
}

Expand Down
9 changes: 6 additions & 3 deletions packages/interfaces/src/MetaData/Library/IGenericLibrary.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { IFileInfo } from '../IFileInfo';

export interface IGenericLibrary<TType extends string> {
/**
* Id of the library. Must be unique.
*/
id: string;

/**
* Name of the library. Must be unique.
*/
Expand All @@ -9,7 +12,7 @@ export interface IGenericLibrary<TType extends string> {
/**
* IPNS path to update from.
*/
upstream?: string;
upstream: string;

/**
* Type of the library.
Expand Down
12 changes: 12 additions & 0 deletions packages/interfaces/src/Profile/IInternalProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ import { IBaseProfile } from "./IBaseProfile";

export interface IInternalProfile extends IBaseProfile {
type: 'internal';

/**
* Optional port number for the node to use (default: any available port).
*/
port?: number;

/**
* Swarm key if it is a private network.
*/
swarmKey?: string;

/**
* List of bootstrap adresses.
*/
bootstrap?: string[];
}

Expand Down
24 changes: 13 additions & 11 deletions packages/interfaces/src/Services/IConfigurationService.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import { IProfile } from '../Profile';

export const IConfigurationServiceSymbol = Symbol.for('IConfigurationService');

/**
* Service to manage app configuration.
*/
export interface IConfigurationService {
/**
* Gets a list of all profile names.
* Gets a list of all {@link IProfile} names.
*/
getProfiles(): string[];

/**
* Returns the specified Profile.
* @param name name of the profile
* Returns the specified {@link IProfile}.
* @param id id of the {@link IProfile}.
*/
getProfile(name: string): IProfile;
getProfile(id: string): IProfile;

/**
* Updates the specified Profile.
* @param name name of the profile
* @param profile updated profile
* Updates the specified {@link IProfile}.
* @param id id of the {@link IProfile}.
* @param profile updated {@link IProfile}.
*/
setProfile(name: string, profile: IProfile): void;
setProfile(id: string, profile: IProfile): void;

/**
* Deletes the specified Profile.
* @param name name of the profile
* Deletes the specified {@link IProfile}.
* @param id id of the {@link IProfile}.
*/
removeProfile(name: string): void;
removeProfile(id: string): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ReadonlySignal } from '@preact/signals-core';

export interface IDialogOptions {
title?: ReadonlySignal<string>;
okButtonText?: ReadonlySignal<string>;
cancelButtonText?: ReadonlySignal<string>;
}
24 changes: 24 additions & 0 deletions packages/interfaces/src/Services/IDialogService/IDialogService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IDialogOptions } from './IDialogOptions';
import { IFileDialogOptions } from './IFileDialogOptions';

export const IDialogServiceSymbol = Symbol.for('IDialogService');

/**
* Service thats shows simple dialogs.
*/
export interface IDialogService {
/**
* Shows a simple dialog that returns a boolean.
*/
boolDialog(options: IDialogOptions): Promise<boolean>;

/**
* Shows a simple dialog that returns a string.
*/
stringDialog(options: IDialogOptions): Promise<string>;

/**
* Shows a simple dialog that returns a file.
*/
fileDialog(options: IFileDialogOptions): Promise<any>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IDialogOptions } from './IDialogOptions';

export interface IFileDialogOptions extends IDialogOptions {
accept?: string;
}
3 changes: 3 additions & 0 deletions packages/interfaces/src/Services/IDialogService/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { type IDialogOptions } from './IDialogOptions';
export { IDialogServiceSymbol, type IDialogService } from './IDialogService';
export { type IFileDialogOptions } from './IFileDialogOptions';
24 changes: 24 additions & 0 deletions packages/interfaces/src/Services/IFileExportService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const IFileExportServiceSymbol = Symbol.for('IFileExportService');

export interface IFileExportService {
/**
* Exports a text file.
* @param contents text content of the file.
* @param filename name of the file.
*/
exportText(contents: string, filename: string): void;

/**
* Exports a json file.
* @param contents content of the json file.
* @param filename name of the file.
*/
exportJson(contents: object, filename: string): void;

/**
* Exports a file from a uri.
* @param uri uri of the file to export.
* @param filename name of the file.
*/
export(uri: string, filename: string): void;
}
15 changes: 15 additions & 0 deletions packages/interfaces/src/Services/IHotkeyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,24 @@ export const IHotkeyServiceSymbol = Symbol.for('IHotkeyService');
* A Hotkey definition.
*/
export interface IHotkey {
/**
* The key to be pressed.
*/
key: string;

/**
* Whether or not shift has to be pressed (default: false).
*/
shift?: boolean;

/**
* Whether or not alt has to be pressed (default: false).
*/
alt?: boolean;

/**
* Whether or not control has to be pressed (default: false).
*/
ctrl?: boolean;
}

Expand Down
16 changes: 16 additions & 0 deletions packages/interfaces/src/Services/IPopupService/IPopupOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Options for a popup.
*/
export interface IPopupOptions {
/**
* Whether or not to close on outside click (default: true).
*/
closeOnOutsideClick?: boolean;

/**
* Content of the popup.
* @param close Function to close the popup.
* @returns The component to display.
*/
content: (close: () => void) => any;
}
11 changes: 11 additions & 0 deletions packages/interfaces/src/Services/IPopupService/IPopupService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IPopupOptions } from './IPopupOptions';

export const IPopupServiceSymbol = Symbol.for('IPopupService');

export interface IPopupService {
/**
* Shows a dialog.
* @param options options of the popup.
*/
show(options: IPopupOptions): Promise<void>;
}
2 changes: 2 additions & 0 deletions packages/interfaces/src/Services/IPopupService/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { type IPopupOptions } from './IPopupOptions';
export { IPopupServiceSymbol, type IPopupService } from './IPopupService';
5 changes: 4 additions & 1 deletion packages/interfaces/src/Services/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export * from './IDialogService';
export * from './INotificationService';
export * from './IPopupService';
export * from './ITaskManager';
export * from './ITranslationService';
export { type IConfigurationService } from './IConfigurationService';
export { type IConfigurationService, IConfigurationServiceSymbol } from './IConfigurationService';
export { type IFileExportService, IFileExportServiceSymbol } from './IFileExportService';
export { IHotkeyServiceSymbol, type IHotkeyService, type IHotkey } from './IHotkeyService';
export { type IIndexManager, IIndexManagerSymbol } from './IIndexManager';
export { type IIpfsService, IIpfsServiceSymbol } from './IIpfsService';
Expand Down
Loading

0 comments on commit c0bd7e3

Please sign in to comment.