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

#360 plugin preferences #361

Merged
merged 7 commits into from
Oct 16, 2023
Merged

Conversation

louis-menlo
Copy link
Contributor

@louis-menlo louis-menlo commented Oct 15, 2023

Problem

  • User can't configure Plugin settings

Solution

  • The Core module supports preferences APIs, and plugins can register available preferences along with their value types
  • Plugin registers available preferences (with preference value type)
import { preferences } from "@janhq/plugin-core";

export function init({ register }: { register: RegisterExtensionPoint }) {
  preferences.registerPreferences<string>(register, PluginName, PreferenceName, DefaultValue);
}
  • Plugin retrieve preference value in it entry codes
import { preferences } from "@janhq/plugin-core";

const setup = async () => {
  const preferenceValue: string = (await preferences.get(PluginName, PreferenceName)) ?? DefaultValue;
}
  • Preferences page execute to retrieve all available preferences then list out (and allow user to set value)
Screenshot 2023-10-16 at 15 08 32
  • Plugin checks for preference settings and function:

No settings available:

Image

Valid settings:

Image

Preferences

To register plugin preferences, you can use the preferences object from the @janhq/plugin-core package. Here's an example of how to register and retrieve plugin preferences:

import { PluginService, preferences } from "@janhq/plugin-core";

const PluginName = "your-first-plugin";

export function init({ register }: { register: RegisterExtensionPoint }) {
  // Register preference update handlers. E.g. update plugin instance with new configuration
  register(PluginService.OnPreferencesUpdate, PluginName, onPreferencesUpdate);

  // Register plugin preferences. E.g. Plugin need apiKey and endpoint to connect to your service
  preferences.registerPreferences<string>(register, PluginName, "apiKey", "");
  preferences.registerPreferences<string>(register, PluginName, "endpoint", "");
}

In this example, we're registering preference update handlers and plugin preferences using the preferences object. We're also defining a PluginName constant to use as the name of the plugin.

To retrieve the values of the registered preferences, we're using the get method of the preferences object and passing in the name of the plugin and the name of the preference.

import { preferences } from "@janhq/plugin-core";

const PluginName = "your-first-plugin";

const setup = async () => {
  // Retrieve apiKey
  const apiKey: string = (await preferences.get(PluginName, "apiKey")) ?? "";

  // Retrieve endpoint
  const endpoint: string = (await preferences.get(PluginName, "endpoint")) ?? "";
}

fixes #360

Action Items:

  • preferences module in @janhq/plugin-core
  • register preference type & handler in Plugin
  • preferences allow get/set using store module
  • app handles preference UI with better CSS (registerPreferences should return json instead of DOM component)
  • clear values
  • grouped settings

@louis-menlo louis-menlo force-pushed the feature/plugin-event-system branch from e42692c to 289a601 Compare October 16, 2023 01:09
@louis-menlo louis-menlo force-pushed the feature/plugin-event-system branch from 289a601 to 016e75d Compare October 16, 2023 01:15
@louis-menlo louis-menlo changed the title feature: #360 plugin preferences #360 plugin preferences Oct 16, 2023
@louis-menlo louis-menlo force-pushed the feature/plugin-event-system branch from 016e75d to 557b238 Compare October 16, 2023 01:18
@louis-menlo louis-menlo changed the title #360 plugin preferences WIP: #360 plugin preferences Oct 16, 2023
@louis-menlo louis-menlo force-pushed the feature/plugin-event-system branch from 557b238 to 1f98ebf Compare October 16, 2023 02:07
@louis-menlo louis-menlo changed the title WIP: #360 plugin preferences #360 plugin preferences Oct 16, 2023
@louis-menlo louis-menlo marked this pull request as ready for review October 16, 2023 08:06
@louis-menlo louis-menlo added P1: important Important feature / fix type: feature request A new feature labels Oct 16, 2023
@louis-menlo louis-menlo merged commit 539e4f4 into feature/plugin-event-system Oct 16, 2023
@louis-menlo louis-menlo deleted the plugin-preferences branch October 16, 2023 08:35
louis-menlo added a commit that referenced this pull request Oct 16, 2023
* feature: #360 plugin preferences

* chore: update core-plugin README.md

* chore: create collections on start
hiento09 added a commit that referenced this pull request Oct 16, 2023
* feature: event based plugin

* chore: update README.md

* Update yarn script for build plugins (#363)

* Update yarn script for build plugins

* Plugin-core install from npmjs instead of from local

---------

Co-authored-by: Hien To <>

* #360 plugin preferences (#361)

* feature: #360 plugin preferences

* chore: update core-plugin README.md

* chore: create collections on start

* chore: bumb core version

* chore: update README

* chore: notify preferences update

* fix: preference update

---------

Co-authored-by: hiento09 <136591877+hiento09@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1: important Important feature / fix type: feature request A new feature
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants