This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/gitgram/mobile/utils/notificationDeepLinkHandler.ts
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

191 lines
4.4 KiB
TypeScript

import { router } from 'expo-router';
import * as Notifications from 'expo-notifications';
import { logger } from './logger';
export type NotificationType =
| 'like'
| 'comment'
| 'follow'
| 'mention'
| 'message'
| 'repost'
| 'story_view'
| 'reel_like'
| 'reel_comment'
| 'follow_request'
| 'scheduled_post_published'
| 'scheduled_post_failed';
interface NotificationData {
type: NotificationType;
postId?: string;
commentId?: string;
userId?: string;
messageId?: string;
conversationId?: string;
storyId?: string;
reelId?: string;
scheduledPostId?: string;
}
/**
* Handles deep linking from push notifications
* Maps notification types to app routes
*/
export const handleNotificationDeepLink = (
response: Notifications.NotificationResponse
): void => {
try {
const data = response.notification.request.content.data as unknown as NotificationData;
if (!data || !data.type) {
logger.warn('Notification data missing or no type specified');
return;
}
const route = getRouteForNotification(data);
if (route) {
logger.log(`Navigating to ${route} from notification type: ${data.type}`);
// Small delay to ensure app is ready
setTimeout(() => {
router.push(route);
}, 100);
}
} catch (error) {
logger.error('Error handling notification deep link:', error);
}
};
/**
* Maps notification data to app routes
*/
function getRouteForNotification(data: NotificationData): string | null {
switch (data.type) {
// Post interactions
case 'like':
if (data.postId) {
return `/post/${data.postId}`;
}
break;
case 'comment':
if (data.postId) {
// Navigate to post with comment section open
return `/post/${data.postId}?showComments=true${data.commentId ? `&commentId=${data.commentId}` : ''}`;
}
break;
case 'mention':
if (data.postId) {
return `/post/${data.postId}`;
} else if (data.commentId && data.postId) {
return `/post/${data.postId}?showComments=true&commentId=${data.commentId}`;
}
break;
case 'repost':
if (data.postId) {
return `/post/${data.postId}`;
}
break;
// User interactions
case 'follow':
if (data.userId) {
return `/profile/${data.userId}`;
}
break;
case 'follow_request':
return '/follow-requests';
// Messages
case 'message':
if (data.conversationId) {
return `/chat/${data.conversationId}`;
} else if (data.userId) {
return `/chat/${data.userId}`;
}
break;
// Stories
case 'story_view':
if (data.storyId) {
return `/story/${data.storyId}`;
}
// Go to stories screen
return '/stories';
// Reels
case 'reel_like':
case 'reel_comment':
if (data.reelId) {
return `/reel/${data.reelId}`;
}
break;
// Scheduled posts
case 'scheduled_post_published':
if (data.postId) {
return `/post/${data.postId}`;
}
break;
case 'scheduled_post_failed':
if (data.scheduledPostId) {
return `/scheduled/${data.scheduledPostId}/edit`;
}
return '/scheduled';
default:
logger.warn(`Unknown notification type: ${data.type}`);
return null;
}
return null;
}
/**
* Get the initial notification if app was opened via notification
*/
export const getInitialNotification = async (): Promise<NotificationData | null> => {
try {
const response = await Notifications.getLastNotificationResponseAsync();
if (response) {
return response.notification.request.content.data as unknown as NotificationData;
}
return null;
} catch (error) {
logger.error('Error getting initial notification:', error);
return null;
}
};
/**
* Setup notification response handler
* Returns cleanup function
*/
export const setupNotificationDeepLinkHandler = (): (() => void) => {
const subscription = Notifications.addNotificationResponseReceivedListener(
handleNotificationDeepLink
);
// Check for initial notification (app opened via notification)
getInitialNotification().then((data) => {
if (data) {
const route = getRouteForNotification(data);
if (route) {
// Delay to ensure navigation is ready
setTimeout(() => {
router.push(route);
}, 500);
}
}
});
return () => {
subscription.remove();
};
};