This document provides an overview of the architectural decisions taken for the Insta application, its scalability, integration with a backend REST API, and the testing strategy. Additionally, it includes instructions on how to run, build, and test the application.
The project is live and can be accessed at https://inaseem.github.io/insta/.
The current scope of the Insta application includes the following features:
- View Image-Based Stories: Users can view stories that are primarily image-based.
- View Story Progress: Users can see the progress of each story as it is being viewed.
- Automatic Story Transition: Once a story is viewed, it automatically moves to the next story.
- Navigate to Next Story: Clicking the right side of the screen takes the user to the next story.
- Navigate to Previous Story: Clicking the left side of the screen takes the user to the previous story.
The future scope of the Insta application includes the following features:
- Video-Based Stories: Users will be able to view stories that are video-based, providing a richer media experience. This is partially implemented.
- View Once Stories: Users will have the option to create stories that can only be viewed once, enhancing privacy and exclusivity.
- Analytics: Detailed analytics for story views, including who viewed the story and how many times it was viewed.
- GitHub Actions for Build and Deployment: Automate the build and deployment process using GitHub Actions. The necessary scripts are already included in the
package.json
file.
The Insta application leverages a variety of technologies to ensure a robust, scalable, and maintainable architecture:
- React: Chosen for its component-based architecture, which promotes reusability and ease of maintenance.
- TypeScript: Provides static typing, which helps catch errors early in the development process and improves code quality.
- Vite: Used as the build tool for its fast build times and modern features.
- React Context API: Utilized for state management to avoid prop drilling and to manage the state of stories efficiently.
- Custom Hooks: Encapsulate reusable logic, making the codebase cleaner and more modular.
- GitHub Pages: Selected for deployment due to its simplicity and seamless integration with GitHub repositories.
- Vitest and @testing-library/react: Chosen for unit testing to ensure the reliability of individual components.
- Playwright: Used for end-to-end testing to simulate user interactions and verify the application’s functionality from the user's perspective.
- Tailwind CSS: A utility-first CSS framework that helps in rapidly building custom designs. It significantly reduces the size of the CSS by purging unused styles.
- PostCSS: A tool for transforming CSS with JavaScript plugins. It helps in optimizing and minimizing the CSS, further reducing the file size.
These technologies were selected to create a performant, scalable, and maintainable application while ensuring a smooth developer experience.
The application is structured into several key components:
- App.tsx: The root component that sets up the router.
- Home.tsx: The home page displaying a list of status items.
- Stories.tsx: The stories page that fetches and displays user stories.
- StoriesView.tsx: The view component for displaying individual stories.
- StoriesContext.tsx: Context provider for managing the state of stories.
- useImageLoader.tsx: Custom hook for loading images.
- useStoryProgress.tsx: Custom hook for managing story progress.
- useStatusData.tsx: Custom hook for fetching status data from the API.
The application uses React's Context API to manage the state of stories. The StoriesContext
is used for centralized state management, making it easier to manage and update the state of stories across different components.
The architecture is designed to be scalable:
- Modular Components: Each component is responsible for a specific part of the application, making it easier to maintain and extend.
- Context API: Using the Context API for state management allows for easy state sharing across components without prop drilling.
- Custom Hooks: Custom hooks like
useImageLoader
,useStoryProgress
, anduseStatusData
encapsulate logic that can be reused across different components. - Varying Timeouts: Although the requirement was to allow 5 seconds story timeouts, the ability to have varying timeouts has been added to handle future cases as well.
The application is designed to integrate seamlessly with a backend REST API:
- API Calls: The
useStatusData
hook fetches status data from the backend using theapi.getStatusByUserId
method. - Error Handling: The hooks include error handling to manage API call failures gracefully.
- Loading State: The hooks manage loading states to provide feedback to the user while data is being fetched.
Writing test cases is a fun process. It helps in making the code quality better and compose the code in a way that it is testable. Testable code goes a long way.
Unit tests are written using vitest
and @testing-library/react
to ensure the core functionality of components:
- StatusItem.test.tsx: Tests the rendering and click functionality of the
StatusItem
component. - PhotoStoryHeader.test.tsx: Tests the rendering and button click functionality of the
PhotoStoryHeader
component. - StoriesView.test.tsx: Tests the rendering and navigation functionality of the
StoriesView
component.
End-to-end tests are written using playwright
to ensure the application works as expected from the user's perspective:
- app.spec.ts: Tests the core functionality of the application, including loading the home page, navigating to the stories page, and interacting with stories.
Refer to the package.json
file for the available scripts:
- Run Development Server:
yarn dev
- Build the Application:
yarn build
- Run Unit Tests:
yarn test
- Run End-to-End Tests:
yarn test:e2e
- Run End-to-End Tests with UI:
yarn test:e2e-ui
- Run Linter:
yarn lint
- Preview Build:
yarn preview
- Deploy to GitHub Pages:
yarn deploy
Before running the application, you need to install the necessary node dependencies:
yarn install
To run the application in development mode:
yarn dev
To build the application for production:
yarn build
To run unit tests:
yarn test
To run end-to-end tests:
yarn test:e2e
To run end-to-end tests with UI:
yarn test:e2e-ui