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

bug: rework how to get config on notifications (Vite Support) #336

Merged
merged 1 commit into from
Dec 31, 2024
Merged
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
3 changes: 1 addition & 2 deletions packages/notifications/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
"@frontile/buttons": "workspace:0.17.0-beta.3",
"@frontile/theme": "workspace:0.17.0-beta.3",
"ember-auto-import": "^2.7.2",
"ember-css-transitions": "^4.4.0",
"ember-get-config": "^2.1.1"
"ember-css-transitions": "^4.4.0"
},
"devDependencies": {
"@babel/core": "7.26.0",
Expand Down
7 changes: 2 additions & 5 deletions packages/notifications/src/-private/get-config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import config from 'ember-get-config';
import { get } from '@ember/object';
import type { DefaultConfig } from './types';

export function getConfigOption<T extends keyof DefaultConfig>(
config: DefaultConfig,
key: T,
defaultValue: NonNullable<DefaultConfig[T]>
): NonNullable<DefaultConfig[T]> {
const value = get(
config['@frontile/notifications'] || ({} as never),
key as never
);
const value = get(config, key);

if (value === undefined) {
return defaultValue;
Expand Down
23 changes: 20 additions & 3 deletions packages/notifications/src/-private/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,40 @@ import Timer from './timer';
import { getConfigOption } from './get-config';
import { tracked } from '@glimmer/tracking';
import { later } from '@ember/runloop';
import { getOwner } from '@ember/owner';
import type { NotificationOptions } from './types';
import type Owner from '@ember/owner';
import { assert } from '@ember/debug';
import type { DefaultConfig } from './types';

export default class NotificationsManager {
@tracked notifications: Notification[] = [];

config: DefaultConfig = {};

constructor(context: object) {
const owner = getOwner(context) as Owner;
assert('NotificationsManager context must have an owner', owner);
const configFactory = owner.factoryFor('config:environment');

if (configFactory && configFactory.class) {
this.config =
(configFactory.class as never)['@frontile/notifications'] || {};
}
}

add(message: string, options: NotificationOptions = {}): Notification {
const notification = new Notification(message, options);
const notification = new Notification(this.config, message, options);
this.notifications = [...this.notifications, notification];

let preserve =
typeof options.preserve === 'undefined'
? getConfigOption('preserve', false)
? getConfigOption(this.config, 'preserve', false)
: options.preserve;

// if default config has set skipTimer to true, we will preserve the
// notification, therefore skiping the timer
if (getConfigOption('skipTimer', false) === true) {
if (getConfigOption(this.config, 'skipTimer', false) === true) {
preserve = true;
}

Expand Down
15 changes: 10 additions & 5 deletions packages/notifications/src/-private/notification.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { tracked } from '@glimmer/tracking';
import Timer from './timer';
import { getConfigOption } from './get-config';
import type { NotificationOptions, CustomAction } from './types';
import type { NotificationOptions, CustomAction, DefaultConfig } from './types';

export default class Notification {
readonly message: string;
Expand All @@ -14,16 +14,21 @@ export default class Notification {
@tracked timer?: Timer;
@tracked isRemoving = false;

constructor(message: string, options: NotificationOptions = {}) {
constructor(
config: DefaultConfig,
message: string,
options: NotificationOptions = {}
) {
this.message = message;
this.appearance =
options.appearance || getConfigOption('appearance', 'info');
options.appearance || getConfigOption(config, 'appearance', 'info');
this.customActions = options.customActions;
this.duration = options.duration || getConfigOption('duration', 5000);
this.duration =
options.duration || getConfigOption(config, 'duration', 5000);
this.transitionDuration =
typeof options.transitionDuration !== 'undefined'
? options.transitionDuration
: getConfigOption('transitionDuration', 200);
: getConfigOption(config, 'transitionDuration', 200);

if (options.allowClosing === false) {
this.allowClosing = false;
Expand Down
2 changes: 1 addition & 1 deletion packages/notifications/src/services/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import NotificationsManager from '../-private/manager';
import type { NotificationOptions } from '../-private/types';

export default class NotificationsService extends Service {
manager = new NotificationsManager();
manager = new NotificationsManager(this);

get notifications(): Notification[] {
return this.manager.notifications;
Expand Down
Loading
Loading