initial commit
This commit is contained in:
141
fluxer_app/src/navigation/NavigationCoordinator.ts
Normal file
141
fluxer_app/src/navigation/NavigationCoordinator.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {action, makeAutoObservable} from 'mobx';
|
||||
import * as RouterUtils from '~/utils/RouterUtils';
|
||||
import type {NavState} from './routeParser';
|
||||
|
||||
type ViewContext = {kind: 'dm'} | {kind: 'favorites'} | {kind: 'guild'; guildId: string};
|
||||
|
||||
class NavigationStore {
|
||||
context: ViewContext = {kind: 'dm'};
|
||||
channelId: string | null = null;
|
||||
messageId: string | null = null;
|
||||
|
||||
constructor() {
|
||||
makeAutoObservable(this, {}, {autoBind: true});
|
||||
}
|
||||
|
||||
@action
|
||||
setState(state: NavState): void {
|
||||
this.context = state.context;
|
||||
this.channelId = state.channelId;
|
||||
this.messageId = state.messageId;
|
||||
}
|
||||
|
||||
@action
|
||||
reset(): void {
|
||||
this.context = {kind: 'dm'};
|
||||
this.channelId = null;
|
||||
this.messageId = null;
|
||||
}
|
||||
}
|
||||
|
||||
class NavigationCoordinator {
|
||||
private readonly store: NavigationStore;
|
||||
|
||||
constructor() {
|
||||
this.store = new NavigationStore();
|
||||
}
|
||||
|
||||
get currentContext(): ViewContext {
|
||||
return this.store.context;
|
||||
}
|
||||
|
||||
get currentChannelId(): string | null {
|
||||
return this.store.channelId;
|
||||
}
|
||||
|
||||
get currentMessageId(): string | null {
|
||||
return this.store.messageId;
|
||||
}
|
||||
|
||||
@action
|
||||
applyRoute(state: NavState): void {
|
||||
this.store.setState(state);
|
||||
}
|
||||
|
||||
@action
|
||||
navigateToGuild(guildId: string, channelId?: string, messageId?: string): void {
|
||||
const state: NavState = {
|
||||
context: {kind: 'guild', guildId},
|
||||
channelId: channelId ?? null,
|
||||
messageId: messageId ?? null,
|
||||
};
|
||||
|
||||
this.applyRoute(state);
|
||||
|
||||
if (messageId) {
|
||||
RouterUtils.transitionTo(`/channels/${guildId}/${channelId}/${messageId}`);
|
||||
} else if (channelId) {
|
||||
RouterUtils.transitionTo(`/channels/${guildId}/${channelId}`);
|
||||
} else {
|
||||
RouterUtils.transitionTo(`/channels/${guildId}`);
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
navigateToDM(channelId?: string, messageId?: string): void {
|
||||
const state: NavState = {
|
||||
context: {kind: 'dm'},
|
||||
channelId: channelId ?? null,
|
||||
messageId: messageId ?? null,
|
||||
};
|
||||
|
||||
this.applyRoute(state);
|
||||
|
||||
if (messageId && channelId) {
|
||||
RouterUtils.transitionTo(`/channels/@me/${channelId}/${messageId}`);
|
||||
} else if (channelId) {
|
||||
RouterUtils.transitionTo(`/channels/@me/${channelId}`);
|
||||
} else {
|
||||
RouterUtils.transitionTo('/channels/@me');
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
navigateToFavorites(channelId?: string, messageId?: string): void {
|
||||
const state: NavState = {
|
||||
context: {kind: 'favorites'},
|
||||
channelId: channelId ?? null,
|
||||
messageId: messageId ?? null,
|
||||
};
|
||||
|
||||
this.applyRoute(state);
|
||||
|
||||
if (messageId && channelId) {
|
||||
RouterUtils.transitionTo(`/channels/@favorites/${channelId}/${messageId}`);
|
||||
} else if (channelId) {
|
||||
RouterUtils.transitionTo(`/channels/@favorites/${channelId}`);
|
||||
} else {
|
||||
RouterUtils.transitionTo('/channels/@favorites');
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
reset(): void {
|
||||
this.store.reset();
|
||||
}
|
||||
}
|
||||
|
||||
const navigationCoordinator = new NavigationCoordinator();
|
||||
|
||||
export type {ViewContext, NavState};
|
||||
export {NavigationStore, NavigationCoordinator};
|
||||
export default navigationCoordinator;
|
||||
85
fluxer_app/src/navigation/routeParser.ts
Normal file
85
fluxer_app/src/navigation/routeParser.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {ME} from '~/Constants';
|
||||
import type {ViewContext} from './NavigationCoordinator';
|
||||
|
||||
interface NavState {
|
||||
context: ViewContext;
|
||||
channelId: string | null;
|
||||
messageId: string | null;
|
||||
}
|
||||
|
||||
interface LocationLike {
|
||||
pathname: string;
|
||||
}
|
||||
|
||||
function parseNavState(location: LocationLike): NavState {
|
||||
const pathname = location.pathname;
|
||||
const segments = pathname.split('/').filter(Boolean);
|
||||
|
||||
if (segments.length < 2 || segments[0] !== 'channels') {
|
||||
return {
|
||||
context: {kind: 'dm'},
|
||||
channelId: null,
|
||||
messageId: null,
|
||||
};
|
||||
}
|
||||
|
||||
const guildId = segments[1];
|
||||
const channelId = segments[2] ?? null;
|
||||
const messageId = segments[3] ?? null;
|
||||
|
||||
let context: ViewContext;
|
||||
|
||||
if (guildId === ME) {
|
||||
context = {kind: 'dm'};
|
||||
} else if (guildId === '@favorites') {
|
||||
context = {kind: 'favorites'};
|
||||
} else {
|
||||
context = {kind: 'guild', guildId};
|
||||
}
|
||||
|
||||
return {
|
||||
context,
|
||||
channelId,
|
||||
messageId,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeRoute(location: LocationLike): string | null {
|
||||
const state = parseNavState(location);
|
||||
|
||||
if (state.context.kind === 'dm' && !state.channelId) {
|
||||
return `/channels/@me`;
|
||||
}
|
||||
|
||||
if (state.context.kind === 'favorites' && !state.channelId) {
|
||||
return `/channels/@favorites`;
|
||||
}
|
||||
|
||||
if (state.context.kind === 'guild' && !state.channelId) {
|
||||
return `/channels/${state.context.guildId}`;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export type {NavState};
|
||||
export {parseNavState, normalizeRoute};
|
||||
Reference in New Issue
Block a user