refactor progress
This commit is contained in:
@@ -17,18 +17,20 @@
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {Logger} from '@app/lib/Logger';
|
||||
import type {Router} from '@app/lib/router/RouterTypes';
|
||||
import {Routes} from '@app/Routes';
|
||||
import * as RouterUtils from '@app/utils/RouterUtils';
|
||||
import {ME} from '@fluxer/constants/src/AppConstants';
|
||||
import {action, makeAutoObservable} from 'mobx';
|
||||
import {ME} from '~/Constants';
|
||||
import type {Router} from '~/lib/router';
|
||||
import NavigationCoordinator from '~/navigation/NavigationCoordinator';
|
||||
import type {NavState} from '~/navigation/routeParser';
|
||||
|
||||
type ChannelId = string;
|
||||
type GuildId = string;
|
||||
type NavigationMode = 'push' | 'replace';
|
||||
|
||||
const logger = new Logger('NavigationStore');
|
||||
|
||||
class NavigationStore {
|
||||
guildId: GuildId | null = null;
|
||||
channelId: ChannelId | null = null;
|
||||
guildId: string | null = null;
|
||||
channelId: string | null = null;
|
||||
messageId: string | null = null;
|
||||
private router: Router | null = null;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
@@ -60,15 +62,13 @@ class NavigationStore {
|
||||
this.guildId = null;
|
||||
this.channelId = null;
|
||||
this.messageId = null;
|
||||
this.updateCoordinator();
|
||||
return;
|
||||
}
|
||||
|
||||
const params = match.params;
|
||||
this.guildId = (params.guildId as GuildId) ?? null;
|
||||
this.channelId = (params.channelId as ChannelId) ?? null;
|
||||
this.guildId = (params.guildId as string) ?? null;
|
||||
this.channelId = (params.channelId as string) ?? null;
|
||||
this.messageId = (params.messageId as string) ?? null;
|
||||
this.updateCoordinator();
|
||||
}
|
||||
|
||||
get pathname(): string {
|
||||
@@ -83,26 +83,103 @@ class NavigationStore {
|
||||
return this.currentLocation?.hash ?? '';
|
||||
}
|
||||
|
||||
@action
|
||||
private updateCoordinator(): void {
|
||||
get context(): 'dm' | 'favorites' | 'guild' {
|
||||
const guildId = this.guildId;
|
||||
let context: NavState['context'];
|
||||
if (!guildId || guildId === ME) return 'dm';
|
||||
if (guildId === '@favorites') return 'favorites';
|
||||
return 'guild';
|
||||
}
|
||||
|
||||
if (guildId === ME || !guildId) {
|
||||
context = {kind: 'dm'};
|
||||
} else if (guildId === '@favorites') {
|
||||
context = {kind: 'favorites'};
|
||||
navigateToGuild(guildId: string, channelId?: string, messageId?: string, mode: NavigationMode = 'push'): void {
|
||||
const path = this.buildGuildPath(guildId, channelId, messageId);
|
||||
logger.debug(`navigateToGuild: ${path} (${mode})`);
|
||||
this.applyNavigation(path, mode);
|
||||
}
|
||||
|
||||
navigateToDM(channelId?: string, messageId?: string, mode: NavigationMode = 'push'): void {
|
||||
const path = this.buildDMPath(channelId, messageId);
|
||||
logger.debug(`navigateToDM: ${path} (${mode})`);
|
||||
this.applyNavigation(path, mode);
|
||||
}
|
||||
|
||||
navigateToFavorites(channelId?: string, messageId?: string, mode: NavigationMode = 'push'): void {
|
||||
const path = this.buildFavoritesPath(channelId, messageId);
|
||||
logger.debug(`navigateToFavorites: ${path} (${mode})`);
|
||||
this.applyNavigation(path, mode);
|
||||
}
|
||||
|
||||
clearMessageIdForChannel(channelId: string, mode: NavigationMode = 'replace'): void {
|
||||
if (this.channelId !== channelId || !this.messageId) return;
|
||||
|
||||
const ctx = this.context;
|
||||
let path: string;
|
||||
if (ctx === 'dm') {
|
||||
path = Routes.dmChannel(channelId);
|
||||
} else if (ctx === 'favorites') {
|
||||
path = Routes.favoritesChannel(channelId);
|
||||
} else {
|
||||
context = {kind: 'guild', guildId};
|
||||
const guildId = this.guildId;
|
||||
if (!guildId) return;
|
||||
path = Routes.guildChannel(guildId, channelId);
|
||||
}
|
||||
|
||||
const navState: NavState = {
|
||||
context,
|
||||
channelId: this.channelId,
|
||||
messageId: this.messageId,
|
||||
};
|
||||
logger.debug(`clearMessageIdForChannel: ${path} (${mode})`);
|
||||
this.applyNavigation(path, mode);
|
||||
}
|
||||
|
||||
NavigationCoordinator.applyRoute(navState);
|
||||
buildChannelPath(guildId: string | null | undefined, channelId: string): string {
|
||||
if (!guildId || guildId === ME) {
|
||||
return Routes.dmChannel(channelId);
|
||||
}
|
||||
if (guildId === '@favorites') {
|
||||
return Routes.favoritesChannel(channelId);
|
||||
}
|
||||
return Routes.guildChannel(guildId, channelId);
|
||||
}
|
||||
|
||||
buildMessagePath(guildId: string | null | undefined, channelId: string, messageId: string): string {
|
||||
if (!guildId || guildId === ME) {
|
||||
return Routes.dmChannelMessage(channelId, messageId);
|
||||
}
|
||||
if (guildId === '@favorites') {
|
||||
return Routes.favoritesChannelMessage(channelId, messageId);
|
||||
}
|
||||
return Routes.channelMessage(guildId, channelId, messageId);
|
||||
}
|
||||
|
||||
private buildGuildPath(guildId: string, channelId?: string, messageId?: string): string {
|
||||
if (messageId && channelId) {
|
||||
return Routes.channelMessage(guildId, channelId, messageId);
|
||||
}
|
||||
return Routes.guildChannel(guildId, channelId);
|
||||
}
|
||||
|
||||
private buildDMPath(channelId?: string, messageId?: string): string {
|
||||
if (messageId && channelId) {
|
||||
return Routes.dmChannelMessage(channelId, messageId);
|
||||
}
|
||||
if (channelId) {
|
||||
return Routes.dmChannel(channelId);
|
||||
}
|
||||
return Routes.ME;
|
||||
}
|
||||
|
||||
private buildFavoritesPath(channelId?: string, messageId?: string): string {
|
||||
if (messageId && channelId) {
|
||||
return Routes.favoritesChannelMessage(channelId, messageId);
|
||||
}
|
||||
if (channelId) {
|
||||
return Routes.favoritesChannel(channelId);
|
||||
}
|
||||
return Routes.FAVORITES;
|
||||
}
|
||||
|
||||
private applyNavigation(path: string, mode: NavigationMode): void {
|
||||
if (mode === 'replace') {
|
||||
RouterUtils.replaceWith(path);
|
||||
} else {
|
||||
RouterUtils.transitionTo(path);
|
||||
}
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
|
||||
Reference in New Issue
Block a user