Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Karrotframe] Suggest plugin to fix refresh issue #98

Closed
irrationnelle opened this issue Jan 25, 2022 · 0 comments
Closed

[Karrotframe] Suggest plugin to fix refresh issue #98

irrationnelle opened this issue Jan 25, 2022 · 0 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@irrationnelle
Copy link
Collaborator

This would be fallback plugin to fix refresh issue

import { PluginType, NavigatorPluginType } from '@karrotframe/plugin'
import React, {createContext, useContext, useState} from 'react'

export const ContextPersistentPathPlugin = createContext<{
    guideMessage: any
    setGuideMessage: (guideMessage: string) => void
}>(null as any)

export const PersistentPathPluginProvider: React.FC = (props) => {
    const [guideMessage, setGuideMessage] = useState('')
    return (
        <ContextPersistentPathPlugin.Provider value={{ guideMessage, setGuideMessage }}>
            {props.children}
        </ContextPersistentPathPlugin.Provider>
    )
}

export const usePersistentPlugin = (): PluginType  & {
    setGuideMessage: (params: string) => any;
    guideMessage: string;
} => {
    const {guideMessage, setGuideMessage} = useContext(ContextPersistentPathPlugin)
        return {
          lifeCycleHooks: {
            onInsertScreenInstance: async (ctx) => {
              if(ctx.ptr === -1 && ctx.screenInstance.screenId !== '/' && ctx.screenInstances.length === 0) {
                  const previousPathInfoString = localStorage.getItem('screenPath');
                  if (!previousPathInfoString) {
                      setGuideMessage('Can not go back anymore')
                      return;
                  }
                  const {currentScreenInstances, isMatchedPath } = JSON.parse(previousPathInfoString);

                  const hasHistoryStack = isMatchedPath === ctx.screenInstance.screenId;
                  if (!hasHistoryStack) {
                      setGuideMessage('Can not go back anymore')
                      localStorage.removeItem('screenPath');
                      return;
                  }

                  const previousScreenPathArr = Object.keys(currentScreenInstances).map(index => currentScreenInstances[index]);
                  const screenPath = [...previousScreenPathArr, {...ctx.screenInstance, id: (previousScreenPathArr.length + 1).toString(), nestedRouteCount: 0}]
                  ctx.options?.setScreenInstancePtr?.(-1 + previousScreenPathArr.length);
                  ctx.options?.setScreenInstances?.(screenPath)
              }
            },
              beforePush: async (ctx) => {
                const currentScreenInstances = ctx.screenInstances.reduce((acc: any, curr: any, index: number) => {
                    acc = {
                        ...acc,
                        [index+1]: curr
                    }
                    return acc
                }, {});
                const pathInfo = {
                    currentScreenInstances,
                    isMatchedPath: ctx.to
                }
                localStorage.setItem('screenPath', JSON.stringify(pathInfo));
              }
         },
            setGuideMessage,
            guideMessage
        }
}

export const persistentPathPlugin: NavigatorPluginType = {
    name: 'persistentPathPlugin',
    provider: PersistentPathPluginProvider,
    executor: usePersistentPlugin,
}
@irrationnelle irrationnelle added good first issue Good for newcomers help wanted Extra attention is needed labels Jan 26, 2022
@tonyfromundefined tonyfromundefined changed the title Suggest plugin to fix refresh issue [Karrotframe] Suggest plugin to fix refresh issue Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant