Skip to content

Commit

Permalink
useAttachedFileとuseObserveの変更を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun-Murakami committed May 5, 2024
1 parent 0217365 commit 470024f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
7 changes: 0 additions & 7 deletions src/hooks/useAttachedFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useDialogStore } from '../store/dialogStore';
import { useAppStateStore } from '../store/appStateStore';
import { useTreeStateStore } from '../store/treeStateStore';
import { UniqueIdentifier } from '@dnd-kit/core';
import { useDatabase } from './useDatabase';
import { Capacitor } from '@capacitor/core';
import { Filesystem, Directory } from '@capacitor/filesystem';
import { Share } from '@capacitor/share';
Expand All @@ -14,9 +13,6 @@ export const useAttachedFile = () => {
const showDialog = useDialogStore((state) => state.showDialog);
const items = useTreeStateStore((state) => state.items);
const setItems = useTreeStateStore((state) => state.setItems);
const currentTree = useTreeStateStore((state) => state.currentTree);

const { saveItemsDb } = useDatabase();

// ファイルをFirebaseStorageにアップロードする処理 ------------------------------------------------
const uploadFile = async (file: File, targetTree: UniqueIdentifier): Promise<string | undefined> => {
Expand Down Expand Up @@ -113,7 +109,6 @@ export const useAttachedFile = () => {
const newItems: TreeItem[] = JSON.parse(JSON.stringify(items));
const updatedItems = await deleteAttachedFile(newItems, fileName);
setItems(updatedItems);
await saveItemsDb(updatedItems, currentTree!);
}
}
}
Expand Down Expand Up @@ -147,7 +142,6 @@ export const useAttachedFile = () => {
const updatedItems = await deleteAttachedFile(newItems, fileName);
if (!isSilent) {
setItems(updatedItems);
await saveItemsDb(updatedItems, currentTree!);
}
setIsLoading(false);
} catch (error) {
Expand All @@ -162,7 +156,6 @@ export const useAttachedFile = () => {
const newItems: TreeItem[] = JSON.parse(JSON.stringify(items));
const updatedItems = await deleteAttachedFile(newItems, fileName);
setItems(updatedItems);
await saveItemsDb(updatedItems, currentTree!);
}
}
};
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/useObserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const useObserve = () => {
const setIsLoading = useAppStateStore((state) => state.setIsLoading);
const quickMemoText = useAppStateStore((state) => state.quickMemoText);
const setQuickMemoText = useAppStateStore((state) => state.setQuickMemoText);
const isLoadedItemsFromDb = useAppStateStore((state) => state.isLoadedItemsFromDb);
const setIsLoadedItemsFromDb = useAppStateStore((state) => state.setIsLoadedItemsFromDb);
const isLoadedMemoFromDb = useAppStateStore((state) => state.isLoadedMemoFromDb);
const setIsLoadedMemoFromDb = useAppStateStore((state) => state.setIsLoadedMemoFromDb);
const setTreesList = useTreeStateStore((state) => state.setTreesList);
Expand Down Expand Up @@ -151,6 +153,15 @@ export const useObserve = () => {
return;
}

if (!isLoadedItemsFromDb) {
setIsLoadedItemsFromDb(false);
return;
}

if (items.length === 0) {
return;
}

setPrevItems(items);
const targetTree = currentTree;
const debounceSave = setTimeout(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useTreeManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const useTreeManagement = () => {
const email = useAppStateStore((state) => state.email);
const isLoading = useAppStateStore((state) => state.isLoading);
const setIsLoading = useAppStateStore((state) => state.setIsLoading);
const setIsLoadedItemsFromDb = useAppStateStore((state) => state.setIsLoadedItemsFromDb);
const setIsAccordionExpanded = useAppStateStore((state) => state.setIsAccordionExpanded);
const setIsFocusedTreeName = useAppStateStore((state) => state.setIsFocusedTreeName);

Expand Down Expand Up @@ -76,6 +77,7 @@ export const useTreeManagement = () => {
members.push({ uid: member, email: treeData.membersV2[member] });
}
setCurrentTreeMembers(members);
setIsLoadedItemsFromDb(true);
setItems(treeData.items);
}

Expand Down
4 changes: 4 additions & 0 deletions src/store/appStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type AppState = {
searchKey: string;
isEditingText: boolean;
quickMemoText: string;
isLoadedItemsFromDb: boolean;
isLoadedMemoFromDb: boolean;
setIsOffline: (isOffline: boolean) => void;
setLocalTimestamp: (localTimestamp: number) => void;
Expand All @@ -36,6 +37,7 @@ type AppState = {
setSearchKey: (searchKey: string) => void;
setIsEditingText: (isEditingText: boolean) => void;
setQuickMemoText: (quickMemoText: string) => void;
setIsLoadedItemsFromDb: (isLoadedItemsFromDb: boolean) => void;
setIsLoadedMemoFromDb: (isLoadedMemoFromDb: boolean) => void;
};

Expand All @@ -57,6 +59,7 @@ export const useAppStateStore = create<AppState>((set) => ({
searchKey: '',
isEditingText: false,
quickMemoText: '',
isLoadedItemsFromDb: false,
isLoadedMemoFromDb: false,
setIsOffline: (isOffline) => set({ isOffline }),
setLocalTimestamp: (localTimestamp) => set({ localTimestamp }),
Expand All @@ -75,5 +78,6 @@ export const useAppStateStore = create<AppState>((set) => ({
setSearchKey: (searchKey) => set({ searchKey }),
setIsEditingText: (isEditingText) => set({ isEditingText }),
setQuickMemoText: (quickMemoText) => set({ quickMemoText }),
setIsLoadedItemsFromDb: (isLoadedItemsFromDb) => set({ isLoadedItemsFromDb }),
setIsLoadedMemoFromDb: (isLoadedMemoFromDb) => set({ isLoadedMemoFromDb }),
}));

0 comments on commit 470024f

Please sign in to comment.