option to customize navigation names as per the component props

This commit is contained in:
2025-11-19 23:40:36 +05:30
parent e90fab8c0b
commit cff57f0980
5 changed files with 23 additions and 7 deletions

View File

@@ -67,6 +67,7 @@ export default function Blog(props: { disableCustomTheme?: boolean }) {
type ViewComponentEntry<P> = {
component: React.ComponentType<P>;
extraProps?: (ctx: RouterContext) => Partial<P>;
navigationMap?: Record<string, string>;
};
const VIEW_COMPONENTS: Record<View, ViewComponentEntry<any>> = {
@@ -76,6 +77,9 @@ export default function Blog(props: { disableCustomTheme?: boolean }) {
login: {
component: Login,
navigationMap: {
open_register: 'onRegister',
},
},
register: {
@@ -88,6 +92,9 @@ export default function Blog(props: { disableCustomTheme?: boolean }) {
article: {
component: ArticleView,
navigationMap: {
open_editor: 'onEdit',
},
extraProps: ({ ui, articles }) => ({
article: articles[ui.selectedArticle!],
}) satisfies Partial<ArticleViewProps>,
@@ -110,9 +117,13 @@ export default function Blog(props: { disableCustomTheme?: boolean }) {
const renderView = () => {
const entry = VIEW_COMPONENTS[ui.view];
const navigationMap= entry['navigationMap'] || {}
const ViewComponent = entry.component;
const childNav = navigateToChildren(ui.view);
const childNav = navigateToChildren(
ui.view,
navigationMap
);
const ctx: RouterContext = {
ui,

View File

@@ -28,11 +28,10 @@ const CoverImage = styled('img')({
export default function ArticleView({
article,
onBack,
open_editor,
onEdit,
}: ArticleViewProps) {
const { currentUser } = useAuth();
const onEdit = open_editor;
return (
<ArticleContainer>

View File

@@ -13,7 +13,7 @@ export default function Register({
const [password2, setPassword2] = React.useState('');
const [localError, setLocalError] = React.useState<string | null>(null);
const handleSubmit = async (e: React.FormEvent) => {
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLocalError(null);

View File

@@ -27,7 +27,7 @@ export interface RegisterProps {
export interface ArticleViewProps {
article: ArticleModel;
onBack: () => void;
open_editor: () => void;
onEdit: () => void;
}
export interface ArticleEditorProps {

View File

@@ -57,12 +57,18 @@ export function useViewRouter(setUI: any) {
};
// auto child navigators from children[]
const navigateToChildren = (view: View) => {
const navigateToChildren = (
view: View,
navigationMap?: Record<string, string>,
) => {
const node = VIEW_TREE[view];
const funcs: Record<string, () => void> = {};
node.children?.forEach((child) => {
funcs[`open_${child}`] = () => navigate(child);
const funcName = `open_${child}`;
const customFuncName = navigationMap?.[funcName];
funcs[funcName] = () => navigate(child);
if (customFuncName) funcs[customFuncName] = () => navigate(child);
});
return funcs;