Skip to content

Commit

Permalink
Merge pull request #8 from projectatomic/feature/setup-communication
Browse files Browse the repository at this point in the history
Feature/setup communication
  • Loading branch information
axel7083 authored Jan 10, 2024
2 parents d1e7ec8 + 1e276d2 commit cf82059
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 109 deletions.
File renamed without changes.
51 changes: 51 additions & 0 deletions packages/backend/src/studio-api-impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { StudioAPI } from '@shared/StudioAPI';
import { Category } from '@shared/models/ICategory';
import { Recipe } from '@shared/models/IRecipe';
import content from './ai.json';
import { Task } from '@shared/models/ITask';

export const RECENT_CATEGORY_ID = 'recent-category';

export class StudioApiImpl implements StudioAPI {

private status: Map<string, Task[]> = new Map<string, Task[]>();

async getPullingStatus(recipeId: string): Promise<Task[]> {
return [];
}

async ping(): Promise<string> {
return 'pong';
}

async getRecentRecipes(): Promise<Recipe[]> {
return content.recipes.toSpliced(0, 10);
}

async getCategories(): Promise<Category[]> {
return content.categories;
}

async getRecipesByCategory(categoryId: string): Promise<Recipe[]> {
if (categoryId === RECENT_CATEGORY_ID) return this.getRecentRecipes();

return content.recipes.filter(recipe => recipe.categories.includes(categoryId));
}

async getRecipeById(recipeId: string): Promise<Recipe> {
const recipe = (content.recipes as Recipe[]).find(recipe => recipe.id === recipeId);
if (recipe) return recipe;
throw new Error('Not found');
}

async searchRecipes(query: string): Promise<Recipe[]> {
return []; // todo: not implemented
}

async pullApplication(recipeId: string): Promise<void> {
const recipe: Recipe = await this.getRecipeById(recipeId);

//todo: stuff here
return Promise.resolve(undefined);
}
}
20 changes: 10 additions & 10 deletions packages/backend/src/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
import type { ExtensionContext, WebviewOptions, WebviewPanel } from '@podman-desktop/api';
import { Uri, window } from '@podman-desktop/api';
import { promises } from 'node:fs';
import { RpcExtension } from '@shared/MessageProxy';
import { StudioApiImpl } from './studio-api-impl';

export class Studio {
readonly #extensionContext: ExtensionContext;

#panel: WebviewPanel | undefined;

rpcExtension: RpcExtension;
studioApi: StudioApiImpl;

constructor(readonly extensionContext: ExtensionContext) {
this.#extensionContext = extensionContext;
}
Expand All @@ -36,7 +41,6 @@ export class Studio {

// register webview
this.#panel = window.createWebviewPanel('studio', 'Studio extension', this.getWebviewOptions(extensionUri));
this.#extensionContext.subscriptions.push(this.#panel);

// update html

Expand Down Expand Up @@ -74,15 +78,11 @@ export class Studio {

this.#panel.webview.html = indexHtml;

// send a message to the webview 10s after it is created
setTimeout(() => {
this.#panel?.webview.postMessage({ command: 'hello' });
}, 10000);

// handle messages from the webview
this.#panel.webview.onDidReceiveMessage(message => {
console.log('received message from webview', message);
});
// Let's create the api that the front will be able to call
this.rpcExtension = new RpcExtension(this.#panel.webview);
this.studioApi = new StudioApiImpl();
// Register the instance
this.rpcExtension.registerInstance<StudioApiImpl>(StudioApiImpl, this.studioApi);
}

public async deactivate(): Promise<void> {
Expand Down
44 changes: 25 additions & 19 deletions packages/backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,39 @@
"moduleResolution": "Node",
"resolveJsonModule": true,
"lib": [
"ES2017",
"webworker"
"ES2017",
"webworker"
],
"sourceMap": true,
"rootDir": "src",
"outDir": "dist",
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"types": [
"node",
]
},
"include": [
"node",
],
"paths": {
"@shared/*": ["../shared/*"]
}
},
"include": [
"src",
"types/*.d.ts",
"../../types/**/*.d.ts"
],
"ts-node": {
"compilerOptions": {
"module": "CommonJS",
"lib": [
"ES2020",
"DOM"
],
"types": [
"node"
]
"../../types/**/*.d.ts",
"../shared/*.ts",
"../shared/**/*.ts"
],
"ts-node": {
"compilerOptions": {
"module": "CommonJS",
"lib": [
"ES2020",
"DOM"
],
"types": [
"node"
]
}
}
}

}
1 change: 1 addition & 0 deletions packages/backend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const config = {
alias: {
'/@/': join(PACKAGE_ROOT, 'src') + '/',
'/@gen/': join(PACKAGE_ROOT, 'src-generated') + '/',
'@shared/': join(PACKAGE_ROOT, '../shared') + '/',
},
},
build: {
Expand Down
34 changes: 8 additions & 26 deletions packages/frontend/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
<script lang="ts">
import './app.css';
import { onMount } from 'svelte';
import './app.css';
import '@fortawesome/fontawesome-free/css/all.min.css';
import { router } from 'tinro';
import Route from '/@/Route.svelte';
import Navigation from '/@/lib/Navigation.svelte';
import Dashboard from '/@/pages/Dashboard.svelte';
import Recipes from '/@/pages/Recipes.svelte';
import Environments from '/@/pages/Environments.svelte';
import Preferences from '/@/pages/Preferences.svelte';
import Registries from '/@/pages/Registries.svelte';
import Models from '/@/pages/Models.svelte';
import Recipe from '/@/pages/Recipe.svelte';
import Dashboard from '/@/pages/Dashboard.svelte';
import Recipes from '/@/pages/Recipes.svelte';
import Environments from '/@/pages/Environments.svelte';
import Preferences from '/@/pages/Preferences.svelte';
import Registries from '/@/pages/Registries.svelte';
import Models from '/@/pages/Models.svelte';
import Recipe from '/@/pages/Recipe.svelte';
router.mode.hash();
onMount(() => {
const podmanDesktopApi = acquirePodmanDesktopApi();
// Update state
podmanDesktopApi.setState({ studioValue: 1 });
// Handle messages sent from the extension to the webview
window.addEventListener('message', event => {
console.log('received the message from the backend with data:', event.data);
});
// after 20s, send a message to the backend
setTimeout(() => {
podmanDesktopApi.postMessage({ message: 'Hello from the webview!' });
}, 20000);
});
</script>


Expand Down
7 changes: 1 addition & 6 deletions packages/frontend/src/pages/Recipe.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ onMount(async () => {
})
const onPullingRequest = () => {
pulling = [
{state: 'success', name: 'Pulling image:latest'},
{state: 'error', name: 'Pulling database:latest'},
{state: 'loading', name: 'Pulling redis:latest'},
{state: 'loading', name: 'Downloading model:latest'},
]
studioClient.pullApplication(recipeId);
}
</script>

Expand Down
40 changes: 4 additions & 36 deletions packages/frontend/src/utils/client.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
import type { Recipe } from '@shared/models/IRecipe';
import type { Category } from '@shared/models/ICategory';
import content from './ai.json';
import type { StudioAPI } from '@shared/StudioAPI';
import { RpcBrowser } from '@shared/MessageProxy';

export const RECENT_CATEGORY_ID = 'recent-category';
const podmanDesktopApi = acquirePodmanDesktopApi();
const rpcBrowser: RpcBrowser = new RpcBrowser(window, podmanDesktopApi);

export class StudioClient implements StudioAPI {
// podmanDesktopApi = acquirePodmanDesktopApi?.();

async ping(): Promise<string> {
return 'pong';
}

async getRecentRecipes(): Promise<Recipe[]> {
return content.recipes.toSpliced(0, 10);
}

async getCategories(): Promise<Category[]> {
return content.categories;
}

async getRecipesByCategory(categoryId: string): Promise<Recipe[]> {
if (categoryId === RECENT_CATEGORY_ID) return this.getRecentRecipes();

return content.recipes.filter(recipe => recipe.categories.includes(categoryId));
}

async getRecipeById(recipeId: string): Promise<Recipe> {
const recipe = (content.recipes as Recipe[]).find(recipe => recipe.id === recipeId);
if (recipe) return recipe;
throw new Error('Not found');
}

async searchRecipes(query: string): Promise<Recipe[]> {
return []; // todo: not implemented
}
}

export const studioClient = new StudioClient();
export const studioClient: StudioAPI = rpcBrowser.getProxy<StudioAPI>();
3 changes: 2 additions & 1 deletion packages/frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"src/**/*.ts",
"src/**/*.js",
"src/**/*.svelte",
"../../types/**/*.d.ts"
"../../types/**/*.d.ts",
"../shared/**/*.ts"
]
}
1 change: 1 addition & 0 deletions packages/frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default defineConfig({
resolve: {
alias: {
'/@/': join(PACKAGE_ROOT, 'src') + '/',
'@shared/': join(PACKAGE_ROOT, '../shared') + '/',
},
},
plugins: [svelte({ hot: !process.env.VITEST })],
Expand Down
Loading

0 comments on commit cf82059

Please sign in to comment.