| 1 | import { useEffect, useState } from "react"; |
| 2 | import reactLogo from "./assets/react.svg"; |
| 3 | import { |
| 4 | BannerAd, |
| 5 | RewardedAd, |
| 6 | showPrivacyOptionsForm, |
| 7 | } from "tauri-plugin-admob-api"; |
| 8 | import "./App.css"; |
| 9 | |
| 10 | function App() { |
| 11 | const [bannerAd, setBannerAd] = useState<BannerAd>(); |
| 12 | const [rewardedAd, setRewardedAd] = useState<RewardedAd>(); |
| 13 | |
| 14 | const showBannerAd = async () => { |
| 15 | await bannerAd?.show(); |
| 16 | }; |
| 17 | |
| 18 | const showRewardedAd = async () => { |
| 19 | try { |
| 20 | await rewardedAd?.show(); |
| 21 | const rewardPromise = await rewardedAd?.onRewarded(async (reward) => { |
| 22 | console.log("RewardedAd get reward callback:", JSON.stringify(reward)); |
| 23 | }); |
| 24 | console.log(rewardPromise?.event); |
| 25 | } catch (e) { |
| 26 | console.log(e); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | const loadAds = async () => { |
| 31 | const bannerAd = new BannerAd({ |
| 32 | adUnitId: "ca-app-pub-3940256099942544/9214589741", |
| 33 | position: "top", |
| 34 | }); |
| 35 | |
| 36 | const rewardedAd = new RewardedAd({ |
| 37 | adUnitId: "ca-app-pub-3940256099942544/5224354917", |
| 38 | }); |
| 39 | |
| 40 | await bannerAd.load(); |
| 41 | await rewardedAd.load(); |
| 42 | |
| 43 | setBannerAd(bannerAd); |
| 44 | setRewardedAd(rewardedAd); |
| 45 | }; |
| 46 | |
| 47 | useEffect(() => { |
| 48 | loadAds(); |
| 49 | }); |
| 50 | |
| 51 | return ( |
| 52 | <main className="container"> |
| 53 | <h1>Welcome to Tauri + React</h1> |
| 54 | |
| 55 | <div className="row"> |
| 56 | <a href="https://vite.dev" target="_blank"> |
| 57 | <img src="/vite.svg" className="logo vite" alt="Vite logo" /> |
| 58 | </a> |
| 59 | <a href="https://tauri.app" target="_blank"> |
| 60 | <img src="/tauri.svg" className="logo tauri" alt="Tauri logo" /> |
| 61 | </a> |
| 62 | <a href="https://react.dev" target="_blank"> |
| 63 | <img src={reactLogo} className="logo react" alt="React logo" /> |
| 64 | </a> |
| 65 | </div> |
| 66 | <p>Click on the Tauri, Vite, and React logos to learn more.</p> |
| 67 | |
| 68 | <div className="row"> |
| 69 | <button onClick={showPrivacyOptionsForm}>Show Privacy Form</button> |
| 70 | </div> |
| 71 | <div className="row"> |
| 72 | <button onClick={showBannerAd}>Show Banner Ad</button> |
| 73 | </div> |
| 74 | <div className="row"> |
| 75 | <button onClick={showRewardedAd}>Show Rewarded Ad</button> |
| 76 | </div> |
| 77 | </main> |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | export default App; |