Skip to content

Commit

Permalink
feat: add config for rememberLastVisitState
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Sep 9, 2023
1 parent c36903b commit 862f5fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/pages/Config/TiddlyWiki.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import useDebouncedCallback from 'beautiful-react-hooks/useDebouncedCallback';
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';

import { Text, TextInput } from 'react-native-paper';
import { Switch, Text, TextInput } from 'react-native-paper';
import { FlexibleText, SwitchContainer } from '../../components/PreferenceWidgets';
import { useConfigStore } from '../../store/config';

export function TiddlyWiki(): JSX.Element {
const { t } = useTranslation();

const initialUserName = useConfigStore(state => state.userName);
const [initialUserName, rememberLastVisitState] = useConfigStore(state => [state.userName, state.rememberLastVisitState]);
const [userName, userNameSetter] = useState(initialUserName);
const setConfig = useConfigStore(state => state.set);

Expand All @@ -26,6 +27,15 @@ export function TiddlyWiki(): JSX.Element {
userNameTextFieldOnChange(newText);
}}
/>
<SwitchContainer>
<FlexibleText>{t('Preference.RememberLastVisitState')}</FlexibleText>
<Switch
value={rememberLastVisitState}
onValueChange={(value) => {
setConfig({ rememberLastVisitState: value });
}}
/>
</SwitchContainer>
</>
);
}
6 changes: 4 additions & 2 deletions src/pages/WikiWebView/WikiViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { styled } from 'styled-components/native';
import { useRequestNativePermissions } from '../../services/NativeService/hooks';
import { useRegisterService } from '../../services/registerServiceOnWebView';
import { useSetWebViewReferenceToService } from '../../services/WikiHookService/hooks';
import { useConfigStore } from '../../store/config';
import { IWikiWorkspace } from '../../store/wiki';
import { useStreamChunksToWebView } from './useStreamChunksToWebView';
import { onErrorHandler } from './useStreamChunksToWebView/onErrorHandler';
Expand Down Expand Up @@ -39,6 +40,7 @@ export const WikiViewer = ({ wikiWorkspace }: WikiViewerProps) => {
const { t } = useTranslation();

const [loaded, setLoaded] = useState(false);
const [rememberLastVisitState] = useConfigStore(state => [state.rememberLastVisitState]);
const [webViewReference, onMessageReference, registerWikiStorageServiceOnWebView, servicesOfWorkspace] = useRegisterService(wikiWorkspace);
const [webViewKeyToReloadAfterRecycleByOS, setWebViewKeyToReloadAfterRecycleByOS] = useState(0);
const triggerFullReload = () => {
Expand All @@ -50,7 +52,7 @@ export const WikiViewer = ({ wikiWorkspace }: WikiViewerProps) => {
const { loadHtmlError } = useTiddlyWiki(wikiWorkspace, injectHtmlAndTiddlersStore, loaded && webViewReference.current !== null, webViewKeyToReloadAfterRecycleByOS);
const windowMetaScript = useWindowMeta(wikiWorkspace);
const preloadScript = useMemo(() => `
var lastLocationHash = \`${wikiWorkspace.lastLocationHash ?? ''}\`;
var lastLocationHash = \`${rememberLastVisitState ? wikiWorkspace.lastLocationHash ?? '' : ''}\`;
location.hash = lastLocationHash;
${windowMetaScript}
Expand All @@ -64,7 +66,7 @@ export const WikiViewer = ({ wikiWorkspace }: WikiViewerProps) => {
${webviewSideReceiver}
true; // note: this is required, or you'll sometimes get silent failures
`, [registerWikiStorageServiceOnWebView, webviewSideReceiver, windowMetaScript]);
`, [registerWikiStorageServiceOnWebView, rememberLastVisitState, webviewSideReceiver, wikiWorkspace.lastLocationHash, windowMetaScript]);

if (loadHtmlError) {
return (
Expand Down
2 changes: 2 additions & 0 deletions src/store/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export interface ConfigState {
autoOpenDefaultWiki: boolean;
keepAliveInBackground: boolean;
preferredLanguage?: string;
rememberLastVisitState: boolean;
syncInBackground: boolean;
syncInterval: number;
syncIntervalBackground: number;
userName: string;
}
const defaultConfig: ConfigState = {
autoOpenDefaultWiki: true,
rememberLastVisitState: true,
keepAliveInBackground: true,
syncInBackground: true,
preferredLanguage: undefined,
Expand Down

0 comments on commit 862f5fa

Please sign in to comment.