From 45bee3520c96bc310d36ba6e119b15ab9305e417 Mon Sep 17 00:00:00 2001 From: Amr Elsayed Date: Thu, 3 Apr 2025 12:10:12 +1100 Subject: [PATCH] Thread sharing refactoring and bug fixes --- .../{[threadUuid].tsx => [shareThreadId].tsx} | 10 +++++----- src/components/share/SharePopup.tsx | 6 +++--- src/services/ChatService.ts | 14 +++++++------- src/store/actions/chatActions.ts | 16 ++++++++-------- src/store/actions/index.ts | 2 +- src/types/types.ts | 2 +- 6 files changed, 25 insertions(+), 25 deletions(-) rename src/app/share/{[threadUuid].tsx => [shareThreadId].tsx} (83%) diff --git a/src/app/share/[threadUuid].tsx b/src/app/share/[shareThreadId].tsx similarity index 83% rename from src/app/share/[threadUuid].tsx rename to src/app/share/[shareThreadId].tsx index 3ed14d6..cf292d9 100644 --- a/src/app/share/[threadUuid].tsx +++ b/src/app/share/[shareThreadId].tsx @@ -13,23 +13,23 @@ import { useRouter, useLocalSearchParams } from 'expo-router' * Handles thread creation and message sending with real-time updates. */ const ShareScreen: React.FC = () => { - const { threadUuid } = useLocalSearchParams<{ threadUuid?: string }>() + const { shareThreadId } = useLocalSearchParams<{ shareThreadId?: string }>() const dispatch = useDispatch() const { abortRequest } = useChat() const router = useRouter() // Fetches thread details on threadId change useEffect(() => { - if (threadUuid && Helpers.isValidateUUID(threadUuid)) { - dispatch(fetchSharedThread(threadUuid)) + if (shareThreadId) { + dispatch(fetchSharedThread(shareThreadId)) .unwrap() - .catch(() => { + .catch((error) => { router.push('/404') }) } else { router.push('/404') } - }, [threadUuid, dispatch, router]) + }, [shareThreadId, dispatch, router]) // Clean up the abort controller on unmount or when the component is no longer active useEffect(() => { diff --git a/src/components/share/SharePopup.tsx b/src/components/share/SharePopup.tsx index 045bd01..c5e87c4 100644 --- a/src/components/share/SharePopup.tsx +++ b/src/components/share/SharePopup.tsx @@ -2,7 +2,7 @@ import { CheckIcon, CloseIcon } from '@/components/svg' import { useChat, useDirection, useScreenInfo } from '@/hooks' import { AppDispatch, RootState } from '@/store' -import { getShareThreadUUID } from '@/store/actions/chatActions' +import { getShareThreadId } from '@/store/actions/chatActions' import { createGeneralThemedStyles, GetEnv } from '@/utils' import React, { useState } from 'react' import { useTranslation } from 'react-i18next' @@ -44,11 +44,11 @@ const SharePopup: React.FC = ({ visible, onClose }) => { const threadId = activeThread?.id setIsCopying(1) const response = await dispatch( - getShareThreadUUID({ + getShareThreadId({ threadId: threadId!, }), ).unwrap() - Clipboard.setString(`${shareURL}/share/${response?.share_uuid}`) + Clipboard.setString(`${shareURL}/share/${response?.share_id}`) setIsCopying(2) setTimeout(() => { setIsCopying(0) diff --git a/src/services/ChatService.ts b/src/services/ChatService.ts index 21427f9..c586e99 100644 --- a/src/services/ChatService.ts +++ b/src/services/ChatService.ts @@ -184,7 +184,7 @@ class ChatService { } } - async getShareThreadUUID(threadId: string, dispatch: Dispatch): Promise { + async getShareThreadId(threadId: string, dispatch: Dispatch): Promise { const response = await this.fetchWithAuthRetry( `${this.baseURL}/share/${threadId}`, { @@ -196,16 +196,16 @@ class ChatService { ) if (!response.ok) { - throw new Error('Error getting share uuid for a thread') + throw new Error('Error getting share id for a thread') } return await response.json() } - async getSharedThread(sharedThreadUUID: string, dispatch: Dispatch): Promise { + async getSharedThread(shareThreadId: string, dispatch: Dispatch): Promise { // This is a public sharing endpoint which doesn't require an access token const response = await this.fetchWithAuthRetry( - `${this.baseURL}/share/${sharedThreadUUID}`, + `${this.baseURL}/share/${shareThreadId}`, { headers: { 'Content-Type': 'application/json', @@ -214,17 +214,17 @@ class ChatService { dispatch, ) if (!response.ok) { - throw new ApplicationError('Error fetching shared thread ' + sharedThreadUUID) + throw new ApplicationError('Error fetching shared thread ' + shareThreadId) } const data = await response.json() if (Helpers.isBlank(data)) { - throw new NotFoundError('Unable to load shared thread ' + sharedThreadUUID) + throw new NotFoundError('Unable to load shared thread ' + shareThreadId) } else { // The API returns { thread_id: 1 } and we need to convert it to the Thread type // No messages are returned in the creation response, so initializing with an empty array const thread: Thread = { - id: String(sharedThreadUUID), // Convert thread_id to a string to match the Thread interface + id: String(shareThreadId), // Convert thread_id to a string to match the Thread interface name: data.content.thread_name ?? null, // API response doesn't include name messages: data.content.messages, // Initialize with an empty array since the API response doesn't include messages } diff --git a/src/store/actions/chatActions.ts b/src/store/actions/chatActions.ts index 7a91db4..de97486 100644 --- a/src/store/actions/chatActions.ts +++ b/src/store/actions/chatActions.ts @@ -218,19 +218,19 @@ export const setThreadName = createAsyncThunk( ) /** - * Get Share uuid for a thread asynchronously. + * Get Share id for a thread asynchronously. * * @param threadId - The ID of the thread. - * @returns A Promise that resolves when the share thread uuid generated successfully. + * @returns A Promise that resolves when the share thread id generated successfully. */ -export const getShareThreadUUID = createAsyncThunk( - 'chat/getShareThreadUUID', +export const getShareThreadId = createAsyncThunk( + 'chat/getShareThreadId', async ({ threadId }: { threadId: string }, { dispatch, getState }) => { try { const { isAuthenticated, accessToken } = (getState() as RootState).auth const chatService = new ChatService(isAuthenticated, accessToken) dispatch(setLoading(true)) - const response = await chatService.getShareThreadUUID(threadId, dispatch) + const response = await chatService.getShareThreadId(threadId, dispatch) if (response.status === 'success') { return response as ShareThreadResponse } @@ -245,7 +245,7 @@ export const getShareThreadUUID = createAsyncThunk( /** * Fetches a shared thread from the chat service. * - * @param sharedThreadUUID - The uuid of the shared thread to fetch. + * @param shareThreadId - The id of the shared thread to fetch. * @param dispatch - The dispatch function from the Redux store. * @param getState - The getState function from the Redux store. * @returns A Promise that resolves to the fetched thread. @@ -254,12 +254,12 @@ export const getShareThreadUUID = createAsyncThunk( */ export const fetchSharedThread = createAsyncThunk( 'chat/fetchSharedThread', - async (sharedThreadUUID: string, { dispatch, getState }) => { + async (shareThreadId: string, { dispatch, getState }) => { try { const { isAuthenticated, accessToken } = (getState() as RootState).auth const chatService = new ChatService(isAuthenticated, accessToken) dispatch(setLoading(true)) - const thread = await chatService.getSharedThread(sharedThreadUUID, dispatch) + const thread = await chatService.getSharedThread(shareThreadId, dispatch) if (!thread.messages) { throw NotFoundError } diff --git a/src/store/actions/index.ts b/src/store/actions/index.ts index dcde296..8a27602 100644 --- a/src/store/actions/index.ts +++ b/src/store/actions/index.ts @@ -8,5 +8,5 @@ export { sendFeedback, setThreadName, fetchSharedThread, - getShareThreadUUID, + getShareThreadId, } from './chatActions' diff --git a/src/types/types.ts b/src/types/types.ts index 349c697..9bfe523 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -55,7 +55,7 @@ export type Error = { // Type for ShareThred response data export type ShareThreadResponse = { status: string - share_uuid: string + share_id: string } export type ResetPasswordResponse = { status: string }