/** * notion-enhancer * (c) 2023 dragonwocky (https://dragonwocky.me/) * (https://notion-enhancer.github.io/) under the MIT license */ import { setState, useState } from "../state.mjs"; import { Heading } from "../components/Heading.mjs"; import { Description } from "../components/Description.mjs"; import { Checkbox } from "../components/Checkbox.mjs"; import { Button } from "../components/Button.mjs"; import { Input } from "../components/Input.mjs"; import { Popup } from "../components/Popup.mjs"; function Profile({ id }) { const { html, getProfile, initDatabase } = globalThis.__enhancerApi, profile = initDatabase([id]), db = initDatabase(); const getName = async () => { let profileName = await profile.get("profileName"); if (id === "default") profileName ??= "default"; return profileName ?? ""; }, setName = async (name) => { // name only has effect in menu // doesn't need to trigger reload await profile.set("profileName", name); }, isActive = async () => { return id === (await getProfile()); }, setActive = async () => { await db.set("activeProfile", id); setState({ rerender: true, databaseUpdated: true }); }; const $successName = html``, $success = html`<${Popup} onopen=${async () => ($successName.innerText = await getName())} >

The profile ${$successName} has been updated successfully.

`, $error = html`<${Popup}>

An error was encountered attempting to parse the uploaded file.

`, uploadProfile = (event) => { const file = event.target.files[0], reader = new FileReader(); reader.onload = async (progress) => { const res = progress.currentTarget.result; try { await profile.import({ ...JSON.parse(res), profileName: await getName(), }); setState({ rerender: true, databaseUpdated: true }); $success.show(); setTimeout(() => $success.hide(), 2000); } catch (err) { $error.show(); setTimeout(() => $error.hide(), 2000); } }; reader.readAsText(file); }, downloadProfile = async () => { const now = new Date(), year = now.getFullYear().toString(), month = (now.getMonth() + 1).toString().padStart(2, "0"), day = now.getDate().toString().padStart(2, "0"), hour = now.getHours().toString().padStart(2, "0"), min = now.getMinutes().toString().padStart(2, "0"), sec = now.getSeconds().toString().padStart(2, "0"), date = year + month + day + hour + min + sec; const $a = html`