Go to file
kegsay e946674df3
Store refactor: use non-global stores in components (#9293)
* Add Stores and StoresContext and use it in MatrixChat and RoomView

Added a new kind of class:
- Add God object `Stores` which will hold refs to all known stores and the `MatrixClient`. This object is NOT a singleton.
- Add `StoresContext` to hold onto a ref of `Stores` for use inside components.

`StoresContext` is created via:
- Create `Stores` in `MatrixChat`, assigning the `MatrixClient` when we have one set. Currently sets the RVS to `RoomViewStore.instance`.
- Wrap `MatrixChat`s `render()` function in a `StoresContext.Provider` so it can be used anywhere.

`StoresContext` is currently only used in `RoomView` via the following changes:
- Remove the HOC, which redundantly set `mxClient` as a prop. We don't need this as `RoomView` was using the client from `this.context`.
- Change the type of context accepted from `MatrixClientContext` to `StoresContext`.
- Modify alllll the places where `this.context` is used to interact with the client and suffix `.client`.
- Modify places where we use `RoomViewStore.instance` and replace them with `this.context.roomViewStore`.

This makes `RoomView` use a non-global instance of RVS.

* Linting

* SDKContext and make client an optional constructor arg

* Move SDKContext to /src/contexts

* Inject all RVS deps

* Linting

* Remove reset calls; deep copy the INITIAL_STATE to avoid test pollution

* DI singletons used in RoomView; DI them in RoomView-test too

* Initial RoomViewStore.instance after all files are imported to avoid cyclical deps

* Lazily init stores to allow for circular dependencies

Rather than stores accepting a list of other stores in their constructors,
which doesn't work when A needs B and B needs A, make new-style stores simply
accept Stores. When a store needs another store, they access it via `Stores`
which then lazily constructs that store if it needs it. This breaks the
circular dependency at constructor time, without needing to introduce
wiring diagrams or any complex DI framework.

* Delete RoomViewStore.instance

Replaced with Stores.instance.roomViewStore

* Linting

* Move OverridableStores to test/TestStores

* Rejig how eager stores get made; don't automatically do it else tests break

* Linting

* Linting and review comments

* Fix new code to use Stores.instance

* s/Stores/SdkContextClass/g

* Update docs

* Remove unused imports

* Update src/stores/RoomViewStore.tsx

Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>

* Remove empty c'tor to make sonar happy

Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
2022-10-19 13:07:03 +01:00
.github Consolidate js-sdk release mode typing CI (#9326) 2022-09-30 17:12:16 +01:00
__mocks__ Move from `browser-request` to `fetch` (#9345) 2022-10-12 18:59:07 +01:00
cypress Allow pressing Enter to send messages in new composer (#9451) 2022-10-19 03:07:21 +00:00
docs Update the documentation for the show_labs_settings parameter (#9395) 2022-10-12 11:58:25 -06:00
res Room call banner (#9378) 2022-10-17 21:36:17 +02:00
scripts Make bash scripts exit on error rather than continue (#9396) 2022-10-12 16:15:57 +01:00
src Store refactor: use non-global stores in components (#9293) 2022-10-19 13:07:03 +01:00
test Store refactor: use non-global stores in components (#9293) 2022-10-19 13:07:03 +01:00
.editorconfig Move more stuff from BK to GHA (#8372) 2022-04-21 11:55:32 +00:00
.eslintignore Step 14: Remove reskindex 2022-03-28 15:30:30 -06:00
.eslintrc.js Prepare for Element Call integration (#9224) 2022-08-30 15:13:39 -04:00
.gitignore Move spaces tests from Puppeteer to Cypress (#8645) 2022-05-26 10:19:00 +01:00
.node-version Declare node 14 requirement 2021-08-04 11:51:55 +01:00
.percy.yml Enable Cypress retries to combat flakiness (#9413) 2022-10-13 19:11:30 +01:00
.stylelintrc.js Disallow invalid inline style comments in stylesheets (#9099) 2022-07-27 14:39:29 +01:00
CHANGELOG.md Prepare changelog for v3.58.1 2022-10-11 17:34:49 +01:00
CONTRIBUTING.md Link contributing guide to Element Web (#9220) 2022-09-01 13:49:52 +02:00
LICENSE Basic structure of a react SDK and start of an implementation. 2015-06-09 17:40:42 +01:00
README.md Link contributing guide to Element Web (#9220) 2022-09-01 13:49:52 +02:00
babel.config.js Step 2: Remove the decorator 2022-03-28 14:02:31 -06:00
cypress.config.ts Enable Cypress retries to combat flakiness (#9413) 2022-10-13 19:11:30 +01:00
package.json Allow pressing Enter to send messages in new composer (#9451) 2022-10-19 03:07:21 +00:00
post-release.sh Simplify releases: move npm publishing to gha, consolidate scripts (#9216) 2022-09-06 12:10:28 +01:00
release.sh Simplify releases: move npm publishing to gha, consolidate scripts (#9216) 2022-09-06 12:10:28 +01:00
release_config.yaml Add release_config for changelogging 2021-08-03 18:08:25 +01:00
sonar-project.properties Tweak sonar-project.properties (#8498) 2022-05-04 15:29:34 -04:00
tsconfig.json Remove e2e test exclude from tsconfig (#9234) 2022-09-02 10:24:07 +02:00
yarn.lock Allow pressing Enter to send messages in new composer (#9451) 2022-10-19 03:07:21 +00:00

README.md

npm Tests Static Analysis matrix-react-sdk This project is using Percy.io for visual regression testing. Weblate Quality Gate Status Coverage Vulnerabilities Bugs

matrix-react-sdk

This is a react-based SDK for inserting a Matrix chat/voip client into a web page.

This package provides the React components needed to build a Matrix web client using React. It is not useable in isolation, and instead must be used from a 'skin'. A skin provides:

  • Customised implementations of presentation components.
  • Custom CSS
  • The containing application
  • Zero or more 'modules' containing non-UI functionality

As of Aug 2018, the only skin that exists is vector-im/element-web; it and matrix-org/matrix-react-sdk should effectively be considered as a single project (for instance, matrix-react-sdk bugs are currently filed against vector-im/element-web rather than this project).

Translation Status

Translation status

Developer Guide

Platform Targets:

All code lands on the develop branch - master is only used for stable releases. Please file PRs against develop!!

We use the same contribution guide as Element. Check it out here: https://github.com/vector-im/element-web/blob/develop/CONTRIBUTING.md

Our code style is also the same as Element's: https://github.com/vector-im/element-web/blob/develop/code_style.md

Code should be committed as follows:

React components in matrix-react-sdk come in two different flavours: 'structures' and 'views'. Structures are stateful components which handle the more complicated business logic of the app, delegating their actual presentation rendering to stateless 'view' components. For instance, the RoomView component that orchestrates the act of visualising the contents of a given Matrix chat room tracks lots of state for its child components which it passes into them for visual rendering via props.

Good separation between the components is maintained by adopting various best practices that anyone working with the SDK needs to be aware of and uphold:

  • Components are named with upper camel case (e.g. views/rooms/EventTile.js)

  • They are organised in a typically two-level hierarchy - first whether the component is a view or a structure, and then a broad functional grouping (e.g. 'rooms' here)

  • The view's CSS file MUST have the same name (e.g. view/rooms/MessageTile.css). CSS for matrix-react-sdk currently resides in https://github.com/matrix-org/matrix-react-sdk/tree/master/res/css.

  • Per-view CSS is optional - it could choose to inherit all its styling from the context of the rest of the app, although this is unusual for any but

  • Theme specific CSS & resources: https://github.com/matrix-org/matrix-react-sdk/tree/master/res/themes structural components (lacking presentation logic) and the simplest view components.

  • The view MUST only refer to the CSS rules defined in its own CSS file. 'Stealing' styling information from other components (including parents) is not cool, as it breaks the independence of the components.

  • CSS classes are named with an app-specific name-spacing prefix to try to avoid CSS collisions. The base skin shipped by Matrix.org with the matrix-react-sdk uses the naming prefix "mx_". A company called Yoyodyne Inc might use a prefix like "yy_" for its app-specific classes.

  • CSS classes use upper camel case when they describe React components - e.g. .mx_MessageTile is the selector for the CSS applied to a MessageTile view.

  • CSS classes for DOM elements within a view which aren't components are named by appending a lower camel case identifier to the view's class name - e.g. .mx_MessageTile_randomDiv is how you'd name the class of an arbitrary div within the MessageTile view.

  • We deliberately use vanilla CSS 3.0 to avoid adding any more magic dependencies into the mix than we already have. App developers are welcome to use whatever floats their boat however. In future we'll start using css-next to pull in features like CSS variable support.

  • The CSS for a component can override the rules for child components. For instance, .mx_RoomList .mx_RoomTile {} would be the selector to override styles of RoomTiles when viewed in the context of a RoomList view. Overrides must be scoped to the View's CSS class - i.e. don't just define .mx_RoomTile {} in RoomList.css - only RoomTile.css is allowed to define its own CSS. Instead, say .mx_RoomList .mx_RoomTile {} to scope the override only to the context of RoomList views. N.B. overrides should be relatively rare as in general CSS inheritance should be enough.

  • Components should render only within the bounding box of their outermost DOM element. Page-absolute positioning and negative CSS margins and similar are generally not cool and stop the component from being reused easily in different places.

Originally matrix-react-sdk followed the Atomic design pattern as per http://patternlab.io to try to encourage a modular architecture. However, we found that the grouping of components into atoms/molecules/organisms made them harder to find relative to a functional split, and didn't emphasise the distinction between 'structural' and 'view' components, so we backed away from it.

Github Issues

All issues should be filed under https://github.com/vector-im/element-web/issues for now.

Development

Ensure you have the latest LTS version of Node.js installed.

Using yarn instead of npm is recommended. Please see the Yarn 1 install guide if you do not have it already. This project has not yet been migrated to Yarn 2, so please ensure yarn --version shows a version from the 1.x series.

matrix-react-sdk depends on matrix-js-sdk. To make use of changes in the latter and to ensure tests run against the develop branch of matrix-js-sdk, you should set up matrix-js-sdk:

git clone https://github.com/matrix-org/matrix-js-sdk
cd matrix-js-sdk
git checkout develop
yarn link
yarn install

Then check out matrix-react-sdk and pull in dependencies:

git clone https://github.com/matrix-org/matrix-react-sdk
cd matrix-react-sdk
git checkout develop
yarn link matrix-js-sdk
yarn install

See the help for yarn link for more details about this.

Running tests

Ensure you've followed the above development instructions and then:

yarn test

Running lint

To check your code complies with the project style, ensure you've followed the above development instructions and then:

yarn lint

Dependency problems

If you see errors (particularly "Cannot find module") running the lint or test commands, and yarn install doesn't fix them, it may be because yarn is not fetching git dependencies eagerly enough.

Try running this:

yarn cache clean && yarn install --force

Now the yarn commands should work as normal.

End-to-End tests

Make sure you've got your Element development server running (by doing yarn start in element-web), and then in this project, run yarn run test:cypress. See docs/cypress.md for more information.