@knapsack/app-ui v3.110.1--canary.2429.d8cc505.0
Knapsack Cloud App UI
Handy Links
Setup
Install dependencies
yarnMake sure your setup is healthy
yarn drGet Account Access; if these links open, you're good:
Ensure a local .env file exists at the root of the project with at least the following content:
HASURA_ADMIN_SECRET="hasura admin secret code goes here"Replace the value above with the x-hasura-admin-secret found within the Request Headers section of the Hasura GraphQL Explorer.
Commands
QuickStart
yarn # install deps
yarn start
# open http://localhost:8080 when you see it
# optionally open up 2nd Terminal tab and run `yarn test:watch`Main Commands
yarn startDev starting local work, kick off all watches & servers- Run
HMR=yes yarn startto opt-in to using Hot Module Replacement to speed up WebPack rebuilds. Note that sometimes weird state can happen that requires a browser refresh to reset. Once those scenarios are worked out, then this will become the default. yarn start:allto run App & Storybook
- Run
yarn sb:watchto run Storybookyarn test:watchGet quick feedback from TypeScript, Eslint, and other semi-quick running tests (i.e. no Cypress tests)yarn drRun Doctor to check your setupyarn newScaffold out new files using Plop. Config inplopfile.ts.yarn fixAuto-fix codesyarn resetClean all built code, toast & re-installnode_modules
Other Commands
yarn wp:statsRun to find out why your Webpack bundle is so big! Runs a clean prod build, then opens up./public/stats.html
Getting Started
- Follow the "Setup" steps above
- Run
yarn start- be patient, read all the terminal output & wait forhttp://localhost:8080to say it's ready, you have two options on opening your browser:- Just open
http://localhost:8080 - In VS Code, ensure you have the Debugger for Chrome extension, then:
- In main menu click "View > Run"
- In top left's "Run" menu select either "Launch Chrome" or "Launch Chrome Canary"
- In any
.tsxor.tsfile in VS Code you can now create breakpoints that will allow rich runtime information about variables and more that can be triggered when navigated to in Chrome. - To configure, adjust
.vscode/launch.json. More help here
- Just open
- When you first open http://localhost:8080 it might take just a bit the first time since we're using
webpack-dev-serverand HTTP requests trigger in-memory Webpack compiling; this makes re-compiles much faster though since it's in-memory & doesn't write to disk! PS That's why nothing is in./publicuntilyarn build - Open your dev tools, make sure you can see the console and pay attention to what it says - if it says something is wrong, then fix it. Devs should be intentional about their use of
console.log,console.warn,console.error(can fail Cypress tests), &console.debug<== Use the last one all you want; that can be messy & noisy. - You've probably opened your browser to the url path
/or/ks, if that's confusing, open the Bootstrap Demo by going to/site/ks-demo-bootstrap/latest - To get lint & type test feedback as you code, do one of the following:
- Run
yarn test:watchin a new Terminal tab - In VS Code's menu select "Terminal > Run Task...", then select "Tests watch". This will:
- Run
yarn test:watchin VS Code's terminal - Fill out the "Problems" panel with results. This is convenient b/c you can click on the individual problems to go right to them.
- Run
- Run
Conventions
Ideally conventions are enforced through lint rules or tests, if not then they are listed here:
- Prefer named exports over
export default- unless you want to use it inReact.lazy() - Functions should only take a single parameter, use an object to pass in more than 1 var
Knapsack Demo Sites
ks-demo-bootstrap- https://github.com/knapsack-cloud/ks-demo-bootstrap
Getting client side data
Redux Store
import { useSelector } from '@ks-app/app';
const MyComponent = () => {
// Autocompletion is your friend for accessing items in the datastore
const title = useSelector((s) => s?.settingsState?.settings?.title);
return <div>The title: {title}</div>;
};Knapsack Cloud GraphQL DB
View the data and use the Graphiql playground by going to https://db.knapsack.cloud - you'll be asked for the Admin Secret, which can be found inside your .env file (run yarn vc:env-pull if it's not found).
Start by either adding to or creating a my-data.gql file inside the ./src directory with something like:
query MyCustomData {
sites {
id
title
}
}
query MySingleSite($siteId: String!) {
sites_by_pk(id: $siteId) {
title
}
}Run yarn gql:build (taken care of for you during yarn start) and you'll notice that the file my-data.gql.generated.tsx was made right next to it. Import and use the custom Apollo useQuery hooks functions inside:
import {
useMyCustomDataQuery,
useMySingleSiteQuery,
} from './my-data.gql.generated';
const MyComponent = () => {
const { loading, data, error } = useMyCustomDataQuery();
const { data: singleSiteData } = useMySingleSiteQuery({
variables: {
siteId: 'my-site-id',
},
});
const singlSiteTitle = singleSiteData?.sites_by_ok?.title;
return (
<ul>
{data.sites.map((site) => (
<li key={site.id}>{site.title}</li>
))}
</ul>
);
};Front End
Storybook (Obsolete)
yarn sb:watchto run Storybook- On any deployed site, go to
/storybookto view, for example: https://next.app.knapsack.cloud/storybook - Any file ending in
*.stories.tsxis pulled in by Storybook - To easily create a new story, run
yarn new story ./path/to/component.tsx- That template can be adjusted by editing
./scripts/plop/stories.tsx.hbs
- That template can be adjusted by editing
- Storybook configuration files are found in
./.storybook/
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago