Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
2002Bishwajeet committed Oct 14, 2024
1 parent 428f873 commit 7d0d128
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packages/mobile/src/components/Chat/ChatDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import { tryJsonParse } from '@homebase-id/js-lib/helpers';
import { EmptyChatContainer } from './EmptyChatContainer';
import { getPlainTextFromRichText } from 'homebase-id-app-common';
import { ImageSource } from '../../provider/image/RNImageProvider';
import { ErrorNotification } from '../ui/Alert/ErrorNotification';

export type ChatMessageIMessage = IMessage & HomebaseFile<ChatMessage>;

Expand Down Expand Up @@ -1138,7 +1139,7 @@ const RenderBubble = memo(
);

const RenderReplyMessageView = memo((props: BubbleProps<ChatMessageIMessage>) => {
const { data: replyMessage } = useChatMessage({
const { data: replyMessage, error } = useChatMessage({
conversationId: props.currentMessage?.fileMetadata.appData.groupId,
messageId: props.currentMessage?.fileMetadata.appData.content.replyId,
}).get;
Expand All @@ -1160,6 +1161,7 @@ const RenderReplyMessageView = memo((props: BubbleProps<ChatMessageIMessage>) =>
},
]}
>
<ErrorNotification error={error} onlyLogging={true} />
<View style={chatStyles.replyText}>
{replyMessage ? (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
PostEmojiPickerModalMethods,
} from '../Interacts/Reactions/PostEmojiPickerModal';

import { ErrorNotification } from '../../ui/Alert/ErrorNotification';

const PAGE_SIZE = 10;

const SocialFeedMainContent = memo(() => {
Expand All @@ -33,6 +35,7 @@ const SocialFeedMainContent = memo(() => {
fetchNextPage,
isFetchingNextPage,
refetch: refreshFeed,
error,
} = useSocialFeed({ pageSize: PAGE_SIZE }).fetchAll;

// Flatten all pages, sorted descending and slice on the max number expected
Expand Down Expand Up @@ -127,6 +130,7 @@ const SocialFeedMainContent = memo(() => {

return (
<>
<ErrorNotification error={error} />
<Host>
<Animated.FlatList
ref={scrollRef}
Expand Down
12 changes: 9 additions & 3 deletions packages/mobile/src/components/ui/Alert/ErrorNotification.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { useEffect } from 'react';
import { useErrors } from '../../../hooks/errors/useErrors';

export const ErrorNotification = ({ error }: { error: Error | unknown }) => {
export const ErrorNotification = ({
error,
onlyLogging,
}: {
error: Error | unknown;
onlyLogging?: boolean;
}) => {
const addError = useErrors().add;

useEffect(() => {
if (error) {
addError(error);
addError(error, undefined, undefined, onlyLogging);
}
}, [addError, error]);
}, [addError, error, onlyLogging]);

return <></>;
};
11 changes: 7 additions & 4 deletions packages/mobile/src/hooks/errors/useErrors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getKnownOdinErrorMessages,
getOdinErrorDetails,
} from '@homebase-id/js-lib/core';
import { addLogs } from '../../provider/log/logger';

export interface Error {
type: 'warning' | 'critical';
Expand All @@ -17,7 +18,8 @@ export const addError = (
queryClient: QueryClient,
error: unknown,
title?: string,
message?: string
message?: string,
onlyLogging?: boolean
) => {
const currentErrors = queryClient.getQueryData<Error[]>(['errors']);
const knownErrorMessage = getKnownOdinErrorMessages(error);
Expand All @@ -35,6 +37,8 @@ export const addError = (
details,
};

addLogs(newError);
if (onlyLogging) return;
const updatedErrors = [...(currentErrors || []), newError];
queryClient.setQueryData(['errors'], updatedErrors);
};
Expand All @@ -46,12 +50,11 @@ export const useErrors = () => {
fetch: useQuery({
queryKey: ['errors'],
queryFn: () => [] as Error[],

gcTime: Infinity,
staleTime: Infinity,
}),
add: (error: unknown, title?: string, message?: string) =>
addError(queryClient, error, title, message),
add: (error: unknown, title?: string, message?: string, onlyLogging?: boolean) =>
addError(queryClient, error, title, message, onlyLogging),
dismiss: (error: Error) => {
const currentErrors = queryClient.getQueryData<Error[]>(['errors']);
const updatedErrors = currentErrors?.filter((e) => e !== error);
Expand Down
4 changes: 2 additions & 2 deletions packages/mobile/src/hooks/feed/useSocialFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export const useSocialFeed = ({ pageSize = 10 }: { pageSize: number }) => {
queryFn: ({ pageParam }) => fetchAll({ pageParam }),
getNextPageParam: (lastPage) =>
lastPage &&
lastPage?.results?.length >= 1 &&
(lastPage?.cursorState || lastPage?.ownerCursorState)
lastPage?.results?.length >= 1 &&
(lastPage?.cursorState || lastPage?.ownerCursorState)
? { cursorState: lastPage.cursorState, ownerCursorState: lastPage.ownerCursorState }
: undefined,
refetchOnMount: false,
Expand Down
36 changes: 36 additions & 0 deletions packages/mobile/src/pages/profile/profile-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ActivityIndicator,
Alert,
ScrollView,
Share,
StyleProp,
TouchableOpacity,
ViewStyle,
Expand All @@ -18,6 +19,7 @@ import { Container } from '../../components/ui/Container/Container';
import {
AddressBook,
Download,
Gear,
Logout,
People,
RecycleBin,
Expand All @@ -30,6 +32,8 @@ import { useAuthenticatedPushNotification } from '../../hooks/push-notification/

import { ProfileInfo } from '../../components/Profile/ProfileInfo';
import { t } from 'homebase-id-app-common';
import { getLogs } from '../../provider/log/logger';
import Toast from 'react-native-toast-message';

type SettingsProps = NativeStackScreenProps<ProfileStackParamList, 'Overview'>;

Expand Down Expand Up @@ -212,6 +216,38 @@ export const ProfilePage = (_props: SettingsProps) => {
</Text>
{logoutPending ? <ActivityIndicator style={{ marginLeft: 'auto' }} /> : null}
</TouchableOpacity>
<TouchableOpacity
onPress={async () => {
const path = await getLogs();
if (!path) {
Toast.show({
type: 'info',
text1: 'No Logs recorded',
position: 'bottom',
});
return;
}
Share.share({
url: path,
});
}}
style={{
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
paddingVertical: 12,
width: '100%',
}}
>
<Gear size={'lg'} />
<Text
style={{
marginLeft: 16,
}}
>
Share Debug Logs
</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
Alert.alert(
Expand Down
10 changes: 8 additions & 2 deletions packages/mobile/src/provider/log/logger.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { appendFile, CachesDirectoryPath, exists, writeFile } from 'react-native-fs';
import { Error } from '../../hooks/errors/useErrors';

const fileName = 'homebase-id-app-logs.txt';
const path = `${CachesDirectoryPath}/homebase-id-app-logs.txt`;

export async function addLogs(error: Error) {
const path = `${CachesDirectoryPath}/${fileName}`;
const err = `${new Date().toISOString()}: ${error.title} - ${error.message}\n Details: ${JSON.stringify(error.details)}\n\n`;
if (!(await exists(path))) {
return writeFile(path, err);
}
return appendFile(path, err);
}

export async function getLogs() {
if (await exists(path)) {
return `file://${path}`;
}
return;
}

0 comments on commit 7d0d128

Please sign in to comment.