-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into wiki-page
- Loading branch information
Showing
4 changed files
with
48 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
resources/js/core/browser-title-with-notification-count.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
import { action, autorun, makeObservable, observable, runInAction } from 'mobx'; | ||
import OsuCore from 'osu-core'; | ||
import { formatNumber } from 'utils/html'; | ||
|
||
export default class BrowserTitleWithNotificationCount { | ||
private disposer?: () => void; | ||
@observable private origTitle = ''; | ||
|
||
set title(newTitle: string) { | ||
runInAction(() => { | ||
this.origTitle = `${newTitle} | osu!`; | ||
}); | ||
} | ||
|
||
constructor(private core: OsuCore) { | ||
makeObservable(this); | ||
document.addEventListener('turbolinks:load', this.setTitle); | ||
document.addEventListener('turbolinks:before-cache', this.resetTitle); | ||
} | ||
|
||
@action | ||
private readonly resetTitle = () => { | ||
document.title = this.origTitle; | ||
this.disposer?.(); | ||
}; | ||
|
||
@action | ||
private readonly setTitle = () => { | ||
this.origTitle = document.title; | ||
|
||
this.disposer = autorun(() => { | ||
const count = this.core.dataStore.notificationStore.unreadStacks.total; | ||
const titlePrefix = count === 0 | ||
? '' | ||
: `(${formatNumber(count)}) `; | ||
|
||
document.title = `${titlePrefix}${this.origTitle}`; | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters