2022-02-24 14:11:57 +00:00
|
|
|
# Fleet front-end
|
2017-03-10 22:13:29 +00:00
|
|
|
|
2021-09-16 17:32:33 +00:00
|
|
|
The Fleet front-end is a Single Page Application using React with Typescript and Hooks.
|
|
|
|
|
2022-02-24 14:11:57 +00:00
|
|
|
## Table of contents
|
2021-09-16 17:32:33 +00:00
|
|
|
- [Running the Fleet web app](#running-the-fleet-web-app)
|
2021-11-07 06:41:09 +00:00
|
|
|
- [Storybook](#storybook)
|
2021-09-16 17:32:33 +00:00
|
|
|
- [Directory Structure](#directory-structure)
|
|
|
|
- [Deprecated](#deprecated)
|
|
|
|
- [Patterns](#patterns)
|
2017-03-10 22:13:29 +00:00
|
|
|
|
2019-01-24 17:39:32 +00:00
|
|
|
## Running the Fleet web app
|
2017-03-10 22:13:29 +00:00
|
|
|
|
2019-01-24 17:39:32 +00:00
|
|
|
For details instruction on building and serving the Fleet web application
|
2022-02-23 18:17:55 +00:00
|
|
|
consult the [Contributing documentation](../docs/Contributing/README.md).
|
2017-03-10 22:13:29 +00:00
|
|
|
|
2021-11-07 06:41:09 +00:00
|
|
|
## Storybook
|
|
|
|
|
2022-07-27 17:36:39 +00:00
|
|
|
[Storybook](https://storybook.js.org/) is a tool to document and visualize components, and we
|
|
|
|
use it to capture our global components used across Fleet. Storybook is key when developing new
|
2021-11-07 06:41:09 +00:00
|
|
|
features and testing components before release. It runs a separate server exposed on port `6006`.
|
|
|
|
To run this server, do the following:
|
|
|
|
|
|
|
|
- Go to your root fleet project directory
|
|
|
|
- Run `make deps`
|
|
|
|
- Run `yarn storybook`
|
|
|
|
|
|
|
|
The URL `localhost:6006` should automatically show in your browser. If not, visit it manually.
|
|
|
|
|
|
|
|
As mentioned, there are two key times Storybook should be used:
|
|
|
|
|
|
|
|
1. When building new features
|
|
|
|
|
2022-07-27 17:36:39 +00:00
|
|
|
As we create new features, we re-use Fleet components often. Running Storybook before implementing
|
|
|
|
new UI elements can clarify if new components need to be created or already exist. This helps us
|
2021-11-07 06:41:09 +00:00
|
|
|
avoid duplicating code.
|
|
|
|
|
|
|
|
2. Testing components
|
|
|
|
|
2022-07-27 17:36:39 +00:00
|
|
|
After creating a component, create a new file, `component.stories.tsx`, within its directory. Then,
|
|
|
|
fill it with the appropriate Storybook code to create a new Storybook entry. You will be able to visualize
|
2021-11-07 06:41:09 +00:00
|
|
|
the component within Storybook to determine if it looks and behaves as expected.
|
|
|
|
|
2022-02-24 14:11:57 +00:00
|
|
|
## Directory structure
|
2017-03-10 22:13:29 +00:00
|
|
|
|
2019-01-24 17:39:32 +00:00
|
|
|
Component directories in the Fleet front-end application encapsulate the entire
|
2021-09-16 17:32:33 +00:00
|
|
|
component, including files for the component and its styles. The
|
2017-03-10 22:13:29 +00:00
|
|
|
typical directory structure for a component is as follows:
|
|
|
|
|
|
|
|
```
|
2021-09-16 17:32:33 +00:00
|
|
|
└── ComponentName
|
|
|
|
├── _styles.scss
|
|
|
|
├── ComponentName.tsx
|
|
|
|
├── index.ts
|
2017-03-10 22:13:29 +00:00
|
|
|
```
|
|
|
|
|
2021-06-07 00:30:54 +00:00
|
|
|
- `_styles.scss`: The component css styles
|
2021-09-16 17:32:33 +00:00
|
|
|
- `ComponentName.tsx`: The React component
|
|
|
|
- `index.ts`: Exports the React component
|
2021-06-07 00:30:54 +00:00
|
|
|
- This file is helpful as it allows other components to import the component
|
2017-03-10 22:13:29 +00:00
|
|
|
by it's directory name. Without this file the component name would have to
|
2021-06-07 00:30:54 +00:00
|
|
|
be duplicated during imports (`components/ComponentName` vs. `components/ComponentName/ComponentName`).
|
2017-03-10 22:13:29 +00:00
|
|
|
|
|
|
|
### [components](./components)
|
2021-06-07 00:30:54 +00:00
|
|
|
|
2022-04-22 16:45:35 +00:00
|
|
|
The component directory contains global React components rendered by pages, receiving props from
|
|
|
|
their parent components to render data and handle user interactions.
|
2017-03-10 22:13:29 +00:00
|
|
|
|
2021-09-16 17:32:33 +00:00
|
|
|
### [context](./context)
|
|
|
|
|
|
|
|
The context directory contains the React Context API pattern for various entities.
|
|
|
|
Only entities that are needed across the app has a global context. For example,
|
2022-07-27 17:36:39 +00:00
|
|
|
the [logged in user](./context/app.tsx) (`currentUser`) has multiple pages and components
|
2021-09-16 17:32:33 +00:00
|
|
|
where its information is pulled.
|
|
|
|
|
2017-03-10 22:13:29 +00:00
|
|
|
### [interfaces](./interfaces)
|
|
|
|
|
2021-09-16 17:32:33 +00:00
|
|
|
Files in the interfaces directory are used to specify the Typescript interface for a reusable Fleet
|
2017-03-10 22:13:29 +00:00
|
|
|
entity. This is designed to DRY up the code and increase re-usability. These
|
2021-09-16 17:32:33 +00:00
|
|
|
interfaces are imported in to component files and implemented when defining the
|
|
|
|
component's props.
|
2017-03-10 22:13:29 +00:00
|
|
|
|
2021-09-16 17:32:33 +00:00
|
|
|
**Additionally, local interfaces are used for props of local components.**
|
2017-03-10 22:13:29 +00:00
|
|
|
|
2021-06-21 21:40:15 +00:00
|
|
|
### [layouts](https://github.com/fleetdm/fleet/tree/main/frontend/layouts)
|
2017-03-10 22:13:29 +00:00
|
|
|
|
2019-01-24 17:39:32 +00:00
|
|
|
The Fleet application has only 1 layout, the [Core Layout](./layouts/CoreLayout/CoreLayout.jsx).
|
2022-07-27 17:36:39 +00:00
|
|
|
The Layout is rendered from the [router](./router/index.tsx) and are used to set up the general
|
2021-09-16 17:32:33 +00:00
|
|
|
app UI (header, sidebar) and render child components.
|
2017-03-10 22:13:29 +00:00
|
|
|
The child components rendered by the layout are typically page components.
|
|
|
|
|
|
|
|
### [pages](./pages)
|
|
|
|
|
|
|
|
Page components are React components typically rendered from the [router](./router).
|
2022-07-27 17:36:39 +00:00
|
|
|
React Router passed props to these pages in case they are needed. Examples include
|
2021-09-16 17:32:33 +00:00
|
|
|
the `router`, `location`, and `params` objects.
|
2017-03-10 22:13:29 +00:00
|
|
|
|
|
|
|
### [router](./router)
|
|
|
|
|
|
|
|
The router directory is where the react router lives. The router decides which
|
|
|
|
component will render at a given URL. Components rendered from the router are
|
|
|
|
typically located in the [pages directory](./pages). The router directory also holds a `paths`
|
|
|
|
file which holds the application paths as string constants for reference
|
|
|
|
throughout the app. These paths are typically referenced from the [App
|
|
|
|
Constants](./app_constants) object.
|
|
|
|
|
2021-09-16 17:32:33 +00:00
|
|
|
### [services](./services)
|
|
|
|
|
|
|
|
CRUD functions for all Fleet entities (e.g. `query`) that link directly to the Fleet API.
|
|
|
|
|
2017-03-10 22:13:29 +00:00
|
|
|
### [styles](./styles)
|
|
|
|
|
|
|
|
The styles directory contains the general app style setup and variables. It
|
|
|
|
includes variables for the app color hex codes, fonts (families, weights and sizes), and padding.
|
|
|
|
|
|
|
|
### [templates](./templates)
|
|
|
|
|
|
|
|
The templates directory contains the HTML file that renders the React application via including the `bundle.js`
|
2021-06-07 00:30:54 +00:00
|
|
|
and `bundle.css` files. The HTML page also includes the HTML element in which the React application is mounted.
|
2017-03-10 22:13:29 +00:00
|
|
|
|
|
|
|
### [utilities](./utilities)
|
|
|
|
|
2021-09-16 17:32:33 +00:00
|
|
|
The utilities directory contains re-usable functions and constants for use throughout the
|
2017-03-10 22:13:29 +00:00
|
|
|
application. The functions include helpers to convert an array of objects to
|
|
|
|
CSV, debounce functions to prevent multiple form submissions, format API errors,
|
|
|
|
etc.
|
|
|
|
|
2021-09-16 17:32:33 +00:00
|
|
|
## Deprecated
|
|
|
|
|
2022-04-22 16:45:35 +00:00
|
|
|
These directories and files are still used (as of 4/22/22) but are being replaced by newer code:
|
2021-09-16 17:32:33 +00:00
|
|
|
|
2021-09-30 19:32:06 +00:00
|
|
|
- [Form.jsx Higher Order Component](./components/forms/README.md); now creating forms with local states with React Hooks (i.e. `useState`)
|
2021-09-16 17:32:33 +00:00
|
|
|
|
|
|
|
To view the deprecated documentation, [click here](./README_deprecated.md).
|
|
|
|
|
|
|
|
## Patterns
|
|
|
|
|
2022-07-27 17:36:39 +00:00
|
|
|
The list of patterns used in the Fleet UI codebase can be found [here](./docs/patterns.md).
|