Skip to content

Commit

Permalink
refactor(examples): add new mechanism for api key in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mrMetalWood committed Jan 17, 2024
1 parent eb3fb7d commit c98a8b3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/deploy-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ jobs:
working-directory: ./website
run: npm ci


- name: Build website
working-directory: ./website
run: |
npm run build
env:
GOOGLE_MAPS_API_KEY: ${{ secrets.GOOGLE_MAPS_API_KEY }}

- name: Find and Replace - Examples API Key
uses: jacobtomlinson/gha-find-replace@v3
with:
find: 'process.env.GOOGLE_MAPS_API_KEY'
replace: ${{ secrets.GOOGLE_MAPS_API_KEY_EXAMPLES }}
include: './website/build/scripts/**'

- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
Expand Down
5 changes: 4 additions & 1 deletion examples/basic-map/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
<div id="app"></div>
<script type="module">
import '@vis.gl/react-google-maps/examples.css';
import {GOOGLE_MAPS_API_KEY} from '@vis.gl/react-google-maps/examples.js';
import {renderToDom} from './src/app';

renderToDom(document.querySelector('#app'));
renderToDom(document.querySelector('#app'), {
apiKey: GOOGLE_MAPS_API_KEY
});
</script>
</body>
</html>
12 changes: 8 additions & 4 deletions examples/basic-map/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import ControlPanel from './control-panel';

const API_KEY = process.env.GOOGLE_MAPS_API_KEY as string;

const App = () => (
<APIProvider apiKey={API_KEY}>
interface Props {
apiKey?: string;
}

const App = (props: Props) => (
<APIProvider apiKey={props.apiKey ?? API_KEY}>
<Map
zoom={3}
center={{lat: 22.54992, lng: 0}}
Expand All @@ -19,12 +23,12 @@ const App = () => (
);
export default App;

export function renderToDom(container: HTMLElement) {
export function renderToDom(container: HTMLElement, props: Props) {
const root = createRoot(container);

root.render(
<React.StrictMode>
<App />
<App {...props} />
</React.StrictMode>
);
}
6 changes: 6 additions & 0 deletions examples/basic-map/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export default defineConfig(({mode}) => {
return {
define: {
'process.env.GOOGLE_MAPS_API_KEY': JSON.stringify(GOOGLE_MAPS_API_KEY)
},
resolve: {
alias: {
'@vis.gl/react-google-maps/examples.js':
'https://visgl.github.io/react-google-maps/scripts/examples.js'
}
}
};
});
3 changes: 3 additions & 0 deletions examples/vite.config.local.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default defineConfig(({mode}) => {
},
resolve: {
alias: {
'@vis.gl/react-google-maps/examples.js': resolve(
'../../website/static/scripts/examples.js'
),
'@vis.gl/react-google-maps/examples.css': resolve(
'../../examples/examples.css'
),
Expand Down
6 changes: 6 additions & 0 deletions website/static/scripts/examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// For the codesandbox examples, the 'process.env.GOOGLE_MAPS_API_KEY'
// gets replaced in the deploy task in the github action
// to contain the valid key for the examples
export default {
GOOGLE_MAPS_API_KEY: process.env.GOOGLE_MAPS_API_KEY
};

0 comments on commit c98a8b3

Please sign in to comment.