Skip to content

Commit

Permalink
fix: default wiki not loaded when store load slow
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Feb 16, 2024
1 parent 946cf6d commit 15d4611
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
21 changes: 16 additions & 5 deletions src/hooks/useAutoOpenDefaultWiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { compact } from 'lodash';
import { useEffect, useState } from 'react';
import type { RootStackParameterList } from '../App';
import { useConfigStore } from '../store/config';
import { IWikiWorkspace, useWorkspaceStore } from '../store/workspace';
import { IWikiWorkspace, useWorkspaceStore, WikiState } from '../store/workspace';
import { navigateIfNotAlreadyThere } from '../utils/RootNavigation';

/**
Expand Down Expand Up @@ -36,13 +36,24 @@ export function useAutoOpenDefaultWiki(preventOpen?: boolean) {
}, [navigation, fromWikiID, route.name, autoOpenDefaultWiki, hadPreventOpen]);
}

/**
* @param wikis Be aware that this is loaded using asyncStorage, so it maybe empty or not loaded yet.
*/
export function openDefaultWikiIfNotAlreadyThere() {
/**
* Be aware that this is loaded using asyncStorage, so it maybe empty or not loaded yet.
*/
const defaultWiki = compact(useWorkspaceStore.getState().workspaces).find((w): w is IWikiWorkspace => w.type === 'wiki');
console.log(`openDefaultWiki ${defaultWiki?.id ?? 'undefined'}`);
if (defaultWiki !== undefined) {
if (defaultWiki === undefined) {
const unsubscribe = useWorkspaceStore.subscribe(onStoreLoaded);
setTimeout(unsubscribe, 1000);
// wait for 1s for asyncStorage to load
// eslint-disable-next-line no-inner-declarations
function onStoreLoaded(state: WikiState, previousState: WikiState) {
if (previousState.workspaces.length !== state.workspaces.length) {
openDefaultWikiIfNotAlreadyThere();
unsubscribe();
}
}
} else {
navigateIfNotAlreadyThere('WikiWebView', { id: defaultWiki.id, quickLoad: defaultWiki.enableQuickLoad });
}
}
20 changes: 10 additions & 10 deletions src/pages/MainMenu/EditItemModel/WikiModelContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
/* eslint-disable unicorn/no-null */
import * as Haptics from 'expo-haptics';
import React, { useState } from 'react';
import React, { useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Alert } from 'react-native';
import { Button, Modal, Portal, Text, TextInput, useTheme } from 'react-native-paper';
Expand All @@ -12,7 +12,6 @@ import Collapsible from 'react-native-collapsible';
import { ServerList } from '../../../components/ServerList';
import { SyncTextButton } from '../../../components/SyncButton';
import { backgroundSyncService } from '../../../services/BackgroundSyncService';
import { useServerStore } from '../../../store/server';
import { IWikiWorkspace, useWorkspaceStore } from '../../../store/workspace';
import { deleteWikiFile } from '../../Config/Developer/useClearAllWikiData';
import { ServerEditModalContent } from '../../Config/ServerAndSync/ServerEditModal';
Expand Down Expand Up @@ -43,6 +42,15 @@ export function WikiEditModalContent({ id, onClose }: WikiEditModalProps): JSX.E
const [performanceToolsModelVisible, setPerformanceToolsModelVisible] = useState(false);
const [expandServerList, setExpandServerList] = useState(false);

const handleSave = useCallback(() => {
if (id === undefined) return;
updateWiki(id, {
name: editedName,
selectiveSyncFilter: editedSelectiveSyncFilter,
});
onClose();
}, [editedName, editedSelectiveSyncFilter, id, onClose, updateWiki]);

if (id === undefined || wiki === undefined) {
return (
<ModalContainer>
Expand All @@ -51,14 +59,6 @@ export function WikiEditModalContent({ id, onClose }: WikiEditModalProps): JSX.E
);
}

const handleSave = () => {
updateWiki(id, {
name: editedName,
selectiveSyncFilter: editedSelectiveSyncFilter,
});
onClose();
};

return (
<ModalContainer>
<StyledTextInput label={t('EditWorkspace.Name')} value={editedName} onChangeText={setEditedName} />
Expand Down
2 changes: 1 addition & 1 deletion src/store/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface IWikiServerSync {
* use `1` (1970 - 1 - 1 00:00:00:001 UTC) to sync every thing to the newly added server.
*/
const LAST_SYNC_TO_SYNC_ALL = 1;
interface WikiState {
export interface WikiState {
workspaces: IWorkspace[];
}
interface WikiActions {
Expand Down

0 comments on commit 15d4611

Please sign in to comment.