option to customize navigation names as per the component props
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ export interface RegisterProps {
|
||||
export interface ArticleViewProps {
|
||||
article: ArticleModel;
|
||||
onBack: () => void;
|
||||
open_editor: () => void;
|
||||
onEdit: () => void;
|
||||
}
|
||||
|
||||
export interface ArticleEditorProps {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user