mirror of
https://github.com/QingWei-Li/notea.git
synced 2024-12-01 19:34:54 +03:00
Merge pull request #150 from notea-org/change/promise-handling
This commit is contained in:
commit
1342ec2314
@ -32,7 +32,7 @@ export const EditContainer = () => {
|
||||
async (id: string) => {
|
||||
// daily notes
|
||||
if (/^\d{4}-\d{1,2}-\d{1,2}$/.test(id)) {
|
||||
findOrCreateNote(id, {
|
||||
await findOrCreateNote(id, {
|
||||
id,
|
||||
title: id,
|
||||
content: '\n',
|
||||
@ -41,24 +41,24 @@ export const EditContainer = () => {
|
||||
} else if (id === 'new') {
|
||||
const url = `/${genNewId()}?new` + (pid ? `&pid=${pid}` : '');
|
||||
|
||||
router.replace(url, undefined, { shallow: true });
|
||||
await router.replace(url, undefined, { shallow: true });
|
||||
} else if (id && !isNew) {
|
||||
try {
|
||||
const result = await fetchNote(id);
|
||||
if (!result) {
|
||||
router.replace({ query: { ...router.query, new: 1 } });
|
||||
await router.replace({ query: { ...router.query, new: 1 } });
|
||||
return;
|
||||
}
|
||||
} catch (msg) {
|
||||
const err = msg as Error;
|
||||
if (err.name !== 'AbortError') {
|
||||
toast(err.message, 'error');
|
||||
router.push('/', undefined, { shallow: true });
|
||||
await router.push('/', undefined, { shallow: true });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (await noteCache.getItem(id)) {
|
||||
router.push(`/${id}`, undefined, { shallow: true });
|
||||
await router.push(`/${id}`, undefined, { shallow: true });
|
||||
return;
|
||||
}
|
||||
|
||||
@ -69,7 +69,7 @@ export const EditContainer = () => {
|
||||
}
|
||||
|
||||
if (!isNew && id !== 'new') {
|
||||
mutateSettings({
|
||||
await mutateSettings({
|
||||
last_visit: `/${id}`,
|
||||
});
|
||||
}
|
||||
@ -89,7 +89,8 @@ export const EditContainer = () => {
|
||||
|
||||
useEffect(() => {
|
||||
abortFindNote();
|
||||
loadNoteById(id);
|
||||
loadNoteById(id)
|
||||
?.catch((v) => console.error('Could not load note: %O', v));
|
||||
}, [loadNoteById, abortFindNote, id]);
|
||||
|
||||
useEffect(() => {
|
||||
|
@ -9,7 +9,8 @@ const Backlinks: FC = () => {
|
||||
const { t } = useI18n();
|
||||
|
||||
useEffect(() => {
|
||||
getBackLinks();
|
||||
getBackLinks()
|
||||
?.catch((v) => console.error('Error whilst getting backlinks: %O', v));
|
||||
}, [getBackLinks]);
|
||||
|
||||
if (!backlinks?.length) {
|
||||
|
@ -30,7 +30,8 @@ const EditTitle: FC<{ readOnly?: boolean }> = ({ readOnly }) => {
|
||||
const onTitleChange = useCallback(
|
||||
(event: ChangeEvent<HTMLTextAreaElement>) => {
|
||||
const title = event.target.value;
|
||||
onNoteChange.callback({ title });
|
||||
onNoteChange.callback({ title })
|
||||
?.catch((v) => console.error('Error whilst changing title: %O', v));
|
||||
},
|
||||
[onNoteChange]
|
||||
);
|
||||
|
@ -20,7 +20,8 @@ const MenuButton = () => {
|
||||
const onToggle = useCallback(
|
||||
(e: MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
sidebar.toggle();
|
||||
sidebar.toggle()
|
||||
?.catch((v) => console.error('Error whilst toggling sidebar: %O', v));
|
||||
},
|
||||
[sidebar]
|
||||
);
|
||||
|
@ -28,13 +28,15 @@ const PreviewModal: FC = () => {
|
||||
|
||||
const gotoLink = useCallback(() => {
|
||||
if (note?.id) {
|
||||
router.push(note.id, undefined, { shallow: true });
|
||||
router.push(note.id, undefined, { shallow: true })
|
||||
?.catch((v) => console.error('Error whilst pushing to router: %O', v));
|
||||
}
|
||||
}, [note?.id, router]);
|
||||
|
||||
useEffect(() => {
|
||||
if (data?.id) {
|
||||
findNote(data?.id);
|
||||
findNote(data?.id)
|
||||
?.catch((v) => console.error('Error whilst finding note %s: %O', data.id, v));
|
||||
}
|
||||
}, [data?.id, findNote]);
|
||||
|
||||
|
@ -19,7 +19,8 @@ const SearchModal: FC = () => {
|
||||
|
||||
const onEnter = useCallback(
|
||||
(item: NoteModel) => {
|
||||
router.push(`/${item.id}`, `/${item.id}`, { shallow: true });
|
||||
router.push(`/${item.id}`, `/${item.id}`, { shallow: true })
|
||||
?.catch((v) => console.error('Error whilst pushing item to router: %O', v));
|
||||
close();
|
||||
},
|
||||
[router, close]
|
||||
|
@ -27,7 +27,8 @@ const ShareModal: FC = () => {
|
||||
(_event: unknown, checked: boolean) => {
|
||||
updateNote({
|
||||
shared: checked ? NOTE_SHARED.PUBLIC : NOTE_SHARED.PRIVATE,
|
||||
});
|
||||
})
|
||||
?.catch((v) => console.error('Error whilst updating note: %O', v));
|
||||
},
|
||||
[updateNote]
|
||||
);
|
||||
|
@ -39,15 +39,18 @@ export const SidebarMenuItem = forwardRef<HTMLLIElement, ItemProps>(
|
||||
close();
|
||||
if (data?.id) {
|
||||
// TODO: merge with mutateNote
|
||||
removeNote(data.id);
|
||||
removeNote(data.id)
|
||||
?.catch((v) => console.error('Error whilst removing note: %O', v));
|
||||
mutateNote(data.id, {
|
||||
pinned: NOTE_PINNED.UNPINNED,
|
||||
});
|
||||
})
|
||||
?.catch((v) => console.error('Error whilst mutating item: %O', v));
|
||||
}
|
||||
}, [close, data, mutateNote, removeNote]);
|
||||
|
||||
const doCopyLink = useCallback(() => {
|
||||
navigator.clipboard.writeText(location.origin + '/' + data?.id);
|
||||
navigator.clipboard.writeText(location.origin + '/' + data?.id)
|
||||
?.catch((v) => console.error('Error whilst writing to clipboard: %O', v));
|
||||
close();
|
||||
}, [close, data?.id]);
|
||||
|
||||
@ -56,7 +59,8 @@ export const SidebarMenuItem = forwardRef<HTMLLIElement, ItemProps>(
|
||||
if (data?.id) {
|
||||
mutateNote(data.id, {
|
||||
pinned: NOTE_PINNED.PINNED,
|
||||
});
|
||||
})
|
||||
?.catch((v) => console.error('Error whilst mutating note: %O', v));
|
||||
}
|
||||
}, [close, data, mutateNote]);
|
||||
|
||||
@ -65,7 +69,8 @@ export const SidebarMenuItem = forwardRef<HTMLLIElement, ItemProps>(
|
||||
if (data?.id) {
|
||||
mutateNote(data.id, {
|
||||
pinned: NOTE_PINNED.UNPINNED,
|
||||
});
|
||||
})
|
||||
?.catch((v) => console.error('Error whilst mutating note: %O', v));
|
||||
}
|
||||
}, [close, data, mutateNote]);
|
||||
|
||||
@ -78,7 +83,8 @@ export const SidebarMenuItem = forwardRef<HTMLLIElement, ItemProps>(
|
||||
|
||||
mutateNote(data.id, {
|
||||
editorsize: (resolvedNoteWidth + 1) % editorSizesCount,
|
||||
});
|
||||
})
|
||||
.catch((v) => console.error('Error whilst mutating note: %O', v));
|
||||
}
|
||||
}, [close, data, mutateNote, settings.editorsize]);
|
||||
|
||||
|
@ -24,12 +24,12 @@ const TrashItem: FC<{
|
||||
|
||||
const onClickRestore = useCallback(async () => {
|
||||
await restoreNote(note);
|
||||
filterNotes(keyword);
|
||||
await filterNotes(keyword);
|
||||
}, [filterNotes, keyword, note, restoreNote]);
|
||||
|
||||
const onClickDelete = useCallback(async () => {
|
||||
await deleteNote(note.id);
|
||||
filterNotes(keyword);
|
||||
await filterNotes(keyword);
|
||||
}, [deleteNote, note.id, filterNotes, keyword]);
|
||||
|
||||
useScrollView(ref, selected);
|
||||
|
@ -19,7 +19,8 @@ const TrashModal: FC = () => {
|
||||
|
||||
const onEnter = useCallback(
|
||||
(item: NoteModel) => {
|
||||
router.push(`/${item.id}`, `/${item.id}`, { shallow: true });
|
||||
router.push(`/${item.id}`, `/${item.id}`, { shallow: true })
|
||||
?.catch((v) => console.error('Error whilst pushing to router: %O', v));
|
||||
close();
|
||||
},
|
||||
[router, close]
|
||||
@ -27,7 +28,8 @@ const TrashModal: FC = () => {
|
||||
|
||||
useEffect(() => {
|
||||
if (visible) {
|
||||
filterNotes();
|
||||
filterNotes()
|
||||
?.catch((v) => console.error('Error whilst filtering notes: %O', v));
|
||||
}
|
||||
}, [visible, filterNotes]);
|
||||
|
||||
|
@ -31,7 +31,8 @@ const Resizable: FC<{ width: number; children: ReactNodeLike }> = ({
|
||||
const lastWidth = lastWidthRef.current;
|
||||
|
||||
if (width && lastWidth) {
|
||||
resize(lastWidth / width);
|
||||
resize(lastWidth / width)
|
||||
?.catch((v) => console.error('Error whilst resizing: %O', v));
|
||||
}
|
||||
lastWidthRef.current = width;
|
||||
}, [resize, width]);
|
||||
|
@ -21,9 +21,10 @@ export const DailyNotes: FC = () => {
|
||||
const [selected, setSelected] = useState(defaultSelected ?? options[0]);
|
||||
|
||||
const handleChange = useCallback(
|
||||
(_event: unknown, item: TreeOption | null) => {
|
||||
async (_event: unknown, item: TreeOption | null) => {
|
||||
if (item) {
|
||||
updateSettings({ daily_root_id: item.id });
|
||||
await updateSettings({ daily_root_id: item.id })
|
||||
?.catch((v) => console.error('Error whilst updating settings: %O', v));
|
||||
setSelected(item);
|
||||
}
|
||||
},
|
||||
|
@ -32,7 +32,8 @@ export const SnippetInjection: FC = () => {
|
||||
|
||||
useEffect(() => {
|
||||
if (IS_DEMO && settings.injection !== DEMO_INJECTION) {
|
||||
updateSettings({ injection: DEMO_INJECTION });
|
||||
updateSettings({ injection: DEMO_INJECTION })
|
||||
?.catch((v) => console.error('Error whilst updating settings: %O', v));
|
||||
setSettings((prev) => ({ ...prev, injection: DEMO_INJECTION }));
|
||||
}
|
||||
}, [settings.injection, IS_DEMO, updateSettings, setSettings]);
|
||||
|
@ -55,10 +55,12 @@ const SidebarListItem: FC<{
|
||||
const onAddNote = useCallback(
|
||||
(e: MouseEvent) => {
|
||||
e.preventDefault();
|
||||
router.push(`/new?pid=` + item.id, undefined, { shallow: true });
|
||||
router.push(`/new?pid=` + item.id, undefined, { shallow: true })
|
||||
?.catch((v) => console.error('Error whilst pushing to router: %O', v));
|
||||
mutateItem(item.id, {
|
||||
isExpanded: true,
|
||||
});
|
||||
})
|
||||
?.catch((v) => console.error('Error whilst mutating item: %O', v));
|
||||
},
|
||||
[item.id, mutateItem]
|
||||
);
|
||||
|
@ -21,7 +21,8 @@ const SideBarList = () => {
|
||||
(id: string | number) => {
|
||||
mutateItem(String(id), {
|
||||
isExpanded: true,
|
||||
});
|
||||
})
|
||||
?.catch((v) => console.error('Error whilst mutating item: %O', v));
|
||||
},
|
||||
[mutateItem]
|
||||
);
|
||||
@ -30,7 +31,8 @@ const SideBarList = () => {
|
||||
(id: string | number) => {
|
||||
mutateItem(String(id), {
|
||||
isExpanded: false,
|
||||
});
|
||||
})
|
||||
?.catch((v) => console.error('Error whilst mutating item: %O', v));
|
||||
},
|
||||
[mutateItem]
|
||||
);
|
||||
@ -62,7 +64,8 @@ const SideBarList = () => {
|
||||
);
|
||||
|
||||
const onCreateNote = useCallback(() => {
|
||||
router.push('/new', undefined, { shallow: true });
|
||||
router.push('/new', undefined, { shallow: true })
|
||||
.catch((v) => console.error('Error whilst pushing to router: %O', v));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
@ -41,7 +41,8 @@ const ButtonMenu = () => {
|
||||
sidebar: { toggle, isFold },
|
||||
} = UIState.useContainer();
|
||||
const onFold = useCallback(() => {
|
||||
toggle();
|
||||
toggle()
|
||||
?.catch((v) => console.error('Error whilst toggling tool: %O', v));
|
||||
}, [toggle]);
|
||||
|
||||
return (
|
||||
|
@ -9,7 +9,8 @@ const Sidebar: FC = () => {
|
||||
const { initTree } = NoteTreeState.useContainer();
|
||||
|
||||
useEffect(() => {
|
||||
initTree();
|
||||
initTree()
|
||||
?.catch((v) => console.error('Error whilst initialising tree: %O', v));
|
||||
}, [initTree]);
|
||||
|
||||
return ua?.isMobileOnly ? <MobileSidebar /> : <BrowserSidebar />;
|
||||
|
@ -61,7 +61,7 @@ export default function useNoteAPI() {
|
||||
}
|
||||
const note = await find(id);
|
||||
if (note) {
|
||||
noteCache.setItem(id, note);
|
||||
await noteCache.setItem(id, note);
|
||||
}
|
||||
|
||||
return note;
|
||||
|
@ -87,7 +87,8 @@ const useEditor = (initNote?: NoteModel) => {
|
||||
const onClickLink = useCallback(
|
||||
(href: string) => {
|
||||
if (isNoteLink(href.replace(location.origin, ''))) {
|
||||
router.push(href, undefined, { shallow: true });
|
||||
router.push(href, undefined, { shallow: true })
|
||||
.catch((v) => console.error('Error whilst pushing href to router: %O', v));
|
||||
} else {
|
||||
window.open(href, '_blank');
|
||||
}
|
||||
@ -161,7 +162,8 @@ const useEditor = (initNote?: NoteModel) => {
|
||||
|
||||
const onEditorChange = useCallback(
|
||||
(value: () => string): void => {
|
||||
onNoteChange.callback({ content: value() });
|
||||
onNoteChange.callback({ content: value() })
|
||||
?.catch((v) => console.error('Error whilst updating note: %O', v));
|
||||
},
|
||||
[onNoteChange]
|
||||
);
|
||||
|
@ -153,7 +153,7 @@ const useNote = (initData?: NoteModel) => {
|
||||
};
|
||||
delete newNote.content;
|
||||
setNote(newNote);
|
||||
mutateItem(newNote.id, {
|
||||
await mutateItem(newNote.id, {
|
||||
data: newNote,
|
||||
});
|
||||
await mutate(note.id, data);
|
||||
|
@ -204,7 +204,8 @@ const useNoteTree = (initData: TreeModel = DEFAULT_TREE) => {
|
||||
const showItem = useCallback(
|
||||
(note: NoteModel) => {
|
||||
const parents = findParentTreeItems(treeRef.current, note);
|
||||
setItemsExpandState(parents, true);
|
||||
setItemsExpandState(parents, true)
|
||||
?.catch((v) => console.error('Error whilst expanding item: %O', v));
|
||||
},
|
||||
[setItemsExpandState]
|
||||
);
|
||||
@ -222,7 +223,8 @@ const useNoteTree = (initData: TreeModel = DEFAULT_TREE) => {
|
||||
const expandedItems = TreeActions.flattenTree(treeRef.current).filter(
|
||||
(item) => item.isExpanded
|
||||
);
|
||||
setItemsExpandState(expandedItems, false);
|
||||
setItemsExpandState(expandedItems, false)
|
||||
.catch((v) => console.error('Error whilst collapsing item: %O', v));
|
||||
}, [setItemsExpandState]);
|
||||
|
||||
const pinnedTree = useMemo(() => {
|
||||
|
@ -24,11 +24,13 @@ export default function useSidebar(initState = false, isMobileOnly = false) {
|
||||
);
|
||||
|
||||
const open = useCallback(() => {
|
||||
toggle(true);
|
||||
toggle(true)
|
||||
?.catch((v) => console.error('Error whilst opening sidebar: %O', v));
|
||||
}, [toggle]);
|
||||
|
||||
const close = useCallback(() => {
|
||||
toggle(false);
|
||||
toggle(false)
|
||||
?.catch((v) => console.error('Error whilst closing sidebar: %O', v));
|
||||
}, [toggle]);
|
||||
|
||||
return { isFold, toggle, open, close };
|
||||
|
@ -18,7 +18,8 @@ const EditNotePage: NextPage<{ tree: TreeModel }> = ({ tree }) => {
|
||||
|
||||
useEffect(() => {
|
||||
if (ua.isMobileOnly) {
|
||||
Router.push('/new');
|
||||
Router.push('/new')
|
||||
?.catch((v) => console.error('Error whilst pushing /new route: %O', v));
|
||||
}
|
||||
}, [ua.isMobileOnly]);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user