-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathsetup-app.ts
More file actions
104 lines (91 loc) · 4.24 KB
/
setup-app.ts
File metadata and controls
104 lines (91 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2025, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
// Third-Party
import React from 'react'
import {RouteProps} from 'react-router-dom'
// Platform Imports
import {
ApplicationExtension,
SliceInitializer,
withApplicationExtensionStore
} from '@salesforce/pwa-kit-extension-sdk/react'
import {applyHOCs} from '@salesforce/pwa-kit-extension-sdk/react/utils'
// Local Imports
import {Config} from './types'
import SamplePage from './pages/sample'
// Overridable Imports
// Using the `overridable` loader means that you are opting in to the override module resolution flow. As a result this module
// will be resolved by first looking in the base projects `overrides` folder then the overrides folders of any extensions configured
// after this one. Only if no module is found will the referenced module in this project be used.
import sampleHOC from 'overridable!./components/sample-hoc'
// Others
import extensionMeta from '../extension-meta.json'
interface StoreSlice {
count: number
increment: () => void
decrement: () => void
}
// This is safe to delete if your extension does not use state. If you aren't using this, ensure you remove the
// `withApplicationExtensionStore` usage below as well.
const sliceInitializer: SliceInitializer<StoreSlice> = (set) => ({
count: 0,
increment: () => set((state) => ({count: state.count + 1})),
decrement: () => set((state) => ({count: state.count - 1}))
})
class Sample extends ApplicationExtension<Config> {
static readonly id = extensionMeta.id
/**
* Use this method to wrap or enhance your PWA-Kit application using [React higher-order components](https://legacy.reactjs.org/docs/higher-order-components.html).
* You can use this to add visual treatments to your application, change the props that are supplied to the application component
* or add things like providers and contexts to be used throughout your app.
*/
extendApp<T extends React.ComponentType<T>>(
App: React.ComponentType<T>
): React.ComponentType<T> {
// DEBUG
console.log(this.getConfig().path)
const HOCs = [
// Example higher-order component, this can be safely removed.
sampleHOC,
// Optionally include state for this extension using `withApplicationExtensionStore`
(component: React.ComponentType<any>) =>
withApplicationExtensionStore(component, {
id: extensionMeta.id,
initializer: sliceInitializer
})
]
return applyHOCs(App, HOCs)
}
/**
* This method is used to make changes to the PWA-Kit application routes. If your extension adds a new page to the application
* then you can add it to the router here. The routes passed to this method is an accrued list of routes that have been added
* from extensions applied before it. It is called during the `getRoutes` phase on both the server and the client.
*
* NOTE: If you instead want to modify a list of all the routes, refer to the `beforeRouteMatch` below.
*/
// TODO: do we need to pass in additional params like request.path? Get familiar with the SEO API.
// TODO: how can developers call SCAPI? How can we pass in the api client (with the access token)?
getRoutes(): Promise<RouteProps[]> {
return Promise.resolve([
{
exact: true,
path: this.getConfig().path,
component: SamplePage
}
])
}
/**
* TODO: update comment to make it clearer that beforeRouteMatch is also called on the client side
* This method is used on the server during the rendering pipeline. It's provided a list of all the routes that your application
* is configured with, including those defined in the base application and those added by all the extensions. You can use this
* method to modify these routes in any way you want, but you must return an array of routes as a result.
*/
beforeRouteMatch(allRoutes: RouteProps[]): RouteProps[] {
return allRoutes
}
}
export default Sample