Add data sync starting point

This commit is contained in:
I Made Setia Baruna 2020-11-04 11:29:45 +07:00
parent 3a396169f4
commit d850e0b921
11 changed files with 168 additions and 8 deletions

2
.env.example Normal file
View file

@ -0,0 +1,2 @@
GOOGLE_DRIVE_CLIENT_ID=
GOOGLE_DRIVE_API_KEY=

1
.gitignore vendored
View file

@ -3,3 +3,4 @@
/src/node_modules/@sapper/ /src/node_modules/@sapper/
yarn-error.log yarn-error.log
/__sapper__/ /__sapper__/
.env

View file

@ -24,9 +24,10 @@
"@rollup/plugin-commonjs": "^14.0.0", "@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-dynamic-import-vars": "^1.1.0", "@rollup/plugin-dynamic-import-vars": "^1.1.0",
"@rollup/plugin-node-resolve": "^8.0.0", "@rollup/plugin-node-resolve": "^8.0.0",
"@rollup/plugin-replace": "^2.2.0", "@rollup/plugin-replace": "^2.3.4",
"@rollup/plugin-url": "^5.0.0", "@rollup/plugin-url": "^5.0.0",
"autoprefixer": "^10.0.1", "autoprefixer": "^10.0.1",
"dotenv": "^8.2.0",
"postcss": "^8.1.2", "postcss": "^8.1.2",
"postcss-load-config": "^3.0.0", "postcss-load-config": "^3.0.0",
"postcss-nested": "^5.0.1", "postcss-nested": "^5.0.1",

View file

@ -7,6 +7,8 @@ import svelte from 'rollup-plugin-svelte';
import babel from '@rollup/plugin-babel'; import babel from '@rollup/plugin-babel';
import { terser } from 'rollup-plugin-terser'; import { terser } from 'rollup-plugin-terser';
import config from 'sapper/config/rollup.js'; import config from 'sapper/config/rollup.js';
import { config as envConfig } from 'dotenv';
import pkg from './package.json'; import pkg from './package.json';
const mode = process.env.NODE_ENV; const mode = process.env.NODE_ENV;
@ -27,6 +29,9 @@ export default {
replace({ replace({
'process.browser': true, 'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode), 'process.env.NODE_ENV': JSON.stringify(mode),
__paimon: JSON.stringify({
env: envConfig().parsed,
}),
}), }),
svelte({ svelte({
dev, dev,
@ -85,6 +90,9 @@ export default {
replace({ replace({
'process.browser': false, 'process.browser': false,
'process.env.NODE_ENV': JSON.stringify(mode), 'process.env.NODE_ENV': JSON.stringify(mode),
__paimon: JSON.stringify({
env: envConfig().parsed,
}),
}), }),
svelte({ svelte({
generate: 'ssr', generate: 'ssr',

View file

@ -0,0 +1,96 @@
<script>
import { onMount } from 'svelte';
import { driveSignedIn, driveLoading } from '../stores/dataSync';
const CLIENT_ID = __paimon.env.GOOGLE_DRIVE_CLIENT_ID;
const API_KEY = __paimon.env.GOOGLE_DRIVE_API_KEY;
const DISCOVERY_DOCS = ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'];
const SCOPES = 'https://www.googleapis.com/auth/drive.appdata';
onMount(() => {
const script = document.createElement('script');
script.onload = handleClientLoad;
script.src = 'https://apis.google.com/js/api.js';
document.body.appendChild(script);
});
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
function updateSigninStatus(status) {
console.log('DRIVE signed in:', status);
driveSignedIn.set(status);
driveLoading.set(false);
if (status) {
getFiles();
}
}
async function getFiles() {
try {
const { result } = await gapi.client.drive.files.list({
spaces: 'appDataFolder',
q: "name = 'save.json'",
});
console.log(result);
if (result.files.length === 0) {
createFile();
} else {
const data = await gapi.client.drive.files.get({
fileId: result.files[0].id,
alt: 'media',
});
console.log(data);
}
} catch (err) {
console.error(err);
}
}
async function createFile() {
try {
const { result } = await gapi.client.drive.files.create({
resource: {
name: 'save.json',
parents: ['appDataFolder'],
},
fields: 'id',
});
const data = await gapi.client.request({
path: `/upload/drive/v3/files/${result.id}`,
method: 'PATCH',
params: {
uploadType: 'media',
},
body: JSON.stringify({ v: __paimon.env.CURRENT_VERSION }),
});
console.log(data);
} catch (err) {
console.error(err);
}
}
function initClient() {
gapi.client
.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES,
})
.then(
function () {
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
},
function (error) {
console.error(error);
},
);
}
</script>

View file

@ -62,4 +62,10 @@
image="/images/calculator.png" image="/images/calculator.png"
label="Calculator" label="Calculator"
href="/calculator" /> href="/calculator" />
<SidebarItem
on:clicked={close}
active={segment === 'settings'}
image="/images/settings.png"
label="Settings"
href="/settings" />
</div> </div>

View file

@ -2,6 +2,7 @@
import Tailwind from '../components/Tailwindcss.svelte'; import Tailwind from '../components/Tailwindcss.svelte';
import Sidebar from '../components/Sidebar/Sidebar.svelte'; import Sidebar from '../components/Sidebar/Sidebar.svelte';
import Header from '../components/Header.svelte'; import Header from '../components/Header.svelte';
import DataSync from '../components/DataSync.svelte';
import { showSidebar } from '../stores/sidebar'; import { showSidebar } from '../stores/sidebar';
@ -26,3 +27,5 @@
Paimon.moe is not affiliated with miHoYo.<br /> Paimon.moe is not affiliated with miHoYo.<br />
Genshin Impact, game content and materials are trademarks and copyrights of miHoYo. Genshin Impact, game content and materials are trademarks and copyrights of miHoYo.
</p> </p>
<DataSync />

View file

@ -0,0 +1,34 @@
<script>
import { mdiGoogleDrive, mdiLoading } from '@mdi/js';
import Button from '../components/Button.svelte';
import Icon from '../components/Icon.svelte';
import { driveSignedIn, driveLoading } from '../stores/dataSync';
function signIn() {
gapi.auth2.getAuthInstance().signIn();
}
function signOut() {
gapi.auth2.getAuthInstance().signOut();
}
</script>
<div class="lg:ml-64 pt-20 px-8 lg:pt-8">
{#if $driveLoading}
<Icon path={mdiLoading} color="white" spin />
{:else}
<p class="text-white">Drive signed in: {$driveSignedIn}</p>
{#if !$driveSignedIn}
<Button on:click={signIn}>
<Icon path={mdiGoogleDrive} className="mr-2" />
Sign in to Google Drive
</Button>
{:else}
<Button on:click={signOut}>
<Icon path={mdiGoogleDrive} className="mr-2" />
Sign out Google Drive
</Button>
{/if}
{/if}
</div>

4
src/stores/dataSync.js Normal file
View file

@ -0,0 +1,4 @@
import { writable } from 'svelte/store';
export const driveSignedIn = writable(false);
export const driveLoading = writable(true);

BIN
static/images/settings.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

@ -917,13 +917,13 @@
is-module "^1.0.0" is-module "^1.0.0"
resolve "^1.17.0" resolve "^1.17.0"
"@rollup/plugin-replace@^2.2.0": "@rollup/plugin-replace@^2.3.4":
version "2.3.3" version "2.3.4"
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.3.tgz#cd6bae39444de119f5d905322b91ebd4078562e7" resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.4.tgz#7dd84c17755d62b509577f2db37eb524d7ca88ca"
integrity sha512-XPmVXZ7IlaoWaJLkSCDaa0Y6uVo5XQYHhiMFzOd5qSv5rE+t/UJToPIOE56flKIxBFQI27ONsxb7dqHnwSsjKQ== integrity sha512-waBhMzyAtjCL1GwZes2jaE9MjuQ/DQF2BatH3fRivUF3z0JBFrU0U6iBNC/4WR+2rLKhaAhPWDNPYp4mI6RqdQ==
dependencies: dependencies:
"@rollup/pluginutils" "^3.0.8" "@rollup/pluginutils" "^3.1.0"
magic-string "^0.25.5" magic-string "^0.25.7"
"@rollup/plugin-url@^5.0.0": "@rollup/plugin-url@^5.0.0":
version "5.0.1" version "5.0.1"
@ -1335,6 +1335,11 @@ dir-glob@^3.0.1:
dependencies: dependencies:
path-type "^4.0.0" path-type "^4.0.0"
dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==
electron-to-chromium@^1.3.571: electron-to-chromium@^1.3.571:
version "1.3.582" version "1.3.582"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.582.tgz#1adfac5affce84d85b3d7b3dfbc4ade293a6ffc4" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.582.tgz#1adfac5affce84d85b3d7b3dfbc4ade293a6ffc4"
@ -1753,7 +1758,7 @@ lower-case@^1.1.1:
resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-1.1.4.tgz#9a2cabd1b9e8e0ae993a4bf7d5875c39c42e8eac" resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-1.1.4.tgz#9a2cabd1b9e8e0ae993a4bf7d5875c39c42e8eac"
integrity sha1-miyr0bno4K6ZOkv31YdcOcQujqw= integrity sha1-miyr0bno4K6ZOkv31YdcOcQujqw=
magic-string@^0.25.2, magic-string@^0.25.5, magic-string@^0.25.7: magic-string@^0.25.2, magic-string@^0.25.7:
version "0.25.7" version "0.25.7"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051" resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA== integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==