el.xwx.moe/pages/settings/account.tsx

462 lines
15 KiB
TypeScript
Raw Normal View History

2023-10-18 16:50:55 -05:00
import { useState, useEffect } from "react";
import useAccountStore from "@/store/account";
import { AccountSettings } from "@/types/global";
import { toast } from "react-hot-toast";
import SettingsLayout from "@/layouts/SettingsLayout";
import TextInput from "@/components/TextInput";
import { resizeImage } from "@/lib/client/resizeImage";
import ProfilePhoto from "@/components/ProfilePhoto";
import SubmitButton from "@/components/SubmitButton";
import React from "react";
import { MigrationFormat, MigrationRequest } from "@/types/global";
import Link from "next/link";
import Checkbox from "@/components/Checkbox";
2024-01-14 09:09:09 -06:00
import { dropdownTriggerer } from "@/lib/client/utils";
2024-05-16 14:02:22 -05:00
import EmailChangeVerificationModal from "@/components/ModalContent/EmailChangeVerificationModal";
import Button from "@/components/ui/Button";
import { i18n } from "next-i18next.config";
2024-05-16 14:02:22 -05:00
const emailEnabled = process.env.NEXT_PUBLIC_EMAIL_PROVIDER;
2023-10-18 16:50:55 -05:00
2023-10-18 23:09:28 -05:00
export default function Account() {
2024-05-16 14:02:22 -05:00
const [emailChangeVerificationModal, setEmailChangeVerificationModal] =
useState(false);
2023-10-18 16:50:55 -05:00
const [submitLoader, setSubmitLoader] = useState(false);
const { account, updateAccount } = useAccountStore();
const [user, setUser] = useState<AccountSettings>(
!objectIsEmpty(account)
? account
: ({
// @ts-ignore
id: null,
name: "",
username: "",
email: "",
emailVerified: null,
2024-05-16 14:02:22 -05:00
password: undefined,
image: "",
2023-10-18 16:50:55 -05:00
isPrivate: true,
// @ts-ignore
createdAt: null,
whitelistedUsers: [],
} as unknown as AccountSettings)
);
function objectIsEmpty(obj: object) {
return Object.keys(obj).length === 0;
}
useEffect(() => {
if (!objectIsEmpty(account)) setUser({ ...account });
}, [account]);
const handleImageUpload = async (e: any) => {
const file: File = e.target.files[0];
const fileExtension = file.name.split(".").pop()?.toLowerCase();
const allowedExtensions = ["png", "jpeg", "jpg"];
if (allowedExtensions.includes(fileExtension as string)) {
const resizedFile = await resizeImage(file);
if (
resizedFile.size < 1048576 // 1048576 Bytes == 1MB
) {
const reader = new FileReader();
reader.onload = () => {
setUser({ ...user, image: reader.result as string });
2023-10-18 16:50:55 -05:00
};
reader.readAsDataURL(resizedFile);
} else {
toast.error("Please select a PNG or JPEG file thats less than 1MB.");
}
} else {
toast.error("Invalid file format.");
}
};
2024-05-16 14:02:22 -05:00
const submit = async (password?: string) => {
2023-10-18 16:50:55 -05:00
setSubmitLoader(true);
const load = toast.loading("Applying...");
const response = await updateAccount({
...user,
2024-05-16 14:02:22 -05:00
// @ts-ignore
password: password ? password : undefined,
2023-10-18 16:50:55 -05:00
});
toast.dismiss(load);
if (response.ok) {
2024-05-16 14:02:22 -05:00
const emailChanged = account.email !== user.email;
if (emailChanged) {
toast.success("Settings Applied!");
toast.success(
"Email change request sent. Please verify the new email address."
);
setEmailChangeVerificationModal(false);
} else toast.success("Settings Applied!");
2023-10-18 16:50:55 -05:00
} else toast.error(response.data as string);
setSubmitLoader(false);
};
const importBookmarks = async (e: any, format: MigrationFormat) => {
2023-12-19 16:20:09 -06:00
setSubmitLoader(true);
2023-10-18 16:50:55 -05:00
const file: File = e.target.files[0];
if (file) {
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = async function (e) {
const load = toast.loading("Importing...");
const request: string = e.target?.result as string;
const body: MigrationRequest = {
format,
data: request,
};
const response = await fetch("/api/v1/migration", {
2023-10-18 16:50:55 -05:00
method: "POST",
body: JSON.stringify(body),
});
const data = await response.json();
toast.dismiss(load);
2023-12-19 16:20:09 -06:00
if (response.ok) {
toast.success("Imported the Bookmarks! Reloading the page...");
setTimeout(() => {
location.reload();
}, 2000);
} else toast.error(data.response as string);
2023-10-18 16:50:55 -05:00
};
reader.onerror = function (e) {
console.log("Error:", e);
};
}
2023-12-19 16:20:09 -06:00
setSubmitLoader(false);
2023-10-18 16:50:55 -05:00
};
const [whitelistedUsersTextbox, setWhiteListedUsersTextbox] = useState("");
useEffect(() => {
setWhiteListedUsersTextbox(account?.whitelistedUsers?.join(", "));
}, [account]);
useEffect(() => {
setUser({
...user,
whitelistedUsers: stringToArray(whitelistedUsersTextbox),
});
}, [whitelistedUsersTextbox]);
const stringToArray = (str: string) => {
const stringWithoutSpaces = str?.replace(/\s+/g, "");
const wordsArray = stringWithoutSpaces?.split(",");
return wordsArray;
};
return (
<SettingsLayout>
2023-11-20 11:48:41 -06:00
<p className="capitalize text-3xl font-thin inline">Account Settings</p>
<div className="divider my-3"></div>
2023-11-20 11:48:41 -06:00
<div className="flex flex-col gap-5">
2023-10-18 16:50:55 -05:00
<div className="grid sm:grid-cols-2 gap-3 auto-rows-auto">
<div className="flex flex-col gap-3">
<div>
2023-11-24 07:39:55 -06:00
<p className="mb-2">Display Name</p>
2023-10-18 16:50:55 -05:00
<TextInput
value={user.name || ""}
2023-12-01 13:00:52 -06:00
className="bg-base-200"
2023-10-18 16:50:55 -05:00
onChange={(e) => setUser({ ...user, name: e.target.value })}
/>
</div>
<div>
2023-11-24 07:39:55 -06:00
<p className="mb-2">Username</p>
2023-10-18 16:50:55 -05:00
<TextInput
value={user.username || ""}
2023-12-01 13:00:52 -06:00
className="bg-base-200"
2023-10-18 16:50:55 -05:00
onChange={(e) => setUser({ ...user, username: e.target.value })}
/>
</div>
{emailEnabled ? (
<div>
2023-11-24 07:39:55 -06:00
<p className="mb-2">Email</p>
2023-10-18 16:50:55 -05:00
<TextInput
value={user.email || ""}
2023-12-01 13:00:52 -06:00
className="bg-base-200"
2023-10-18 16:50:55 -05:00
onChange={(e) => setUser({ ...user, email: e.target.value })}
/>
</div>
) : undefined}
<div>
<p className="mb-2">Language</p>
<select
onChange={(e) => {
setUser({ ...user, locale: e.target.value });
}}
className="select border border-neutral-content focus:outline-none focus:border-primary duration-100 w-full bg-base-200 rounded-[0.375rem] min-h-0 h-[2.625rem] leading-4 p-2"
>
{i18n.locales.map((locale) => (
<option
key={locale}
value={locale}
selected={user.locale === locale}
>
{new Intl.DisplayNames(locale, { type: "language" }).of(
locale
) || ""}
</option>
))}
</select>
</div>
2023-10-18 16:50:55 -05:00
</div>
<div className="sm:row-span-2 sm:justify-self-center my-3">
<p className="mb-2 sm:text-center">Profile Photo</p>
<div className="w-28 h-28 flex gap-3 sm:flex-col items-center">
2023-10-18 16:50:55 -05:00
<ProfilePhoto
priority={true}
src={user.image ? user.image : undefined}
2023-12-17 22:32:33 -06:00
large={true}
2023-10-18 16:50:55 -05:00
/>
<div className="dropdown dropdown-bottom">
<Button
tabIndex={0}
role="button"
size="small"
intent="secondary"
onMouseDown={dropdownTriggerer}
className="text-sm"
2023-10-18 16:50:55 -05:00
>
<i className="bi-pencil-square text-md duration-100"></i>
Edit
</Button>
<ul className="shadow menu dropdown-content z-[1] bg-base-200 border border-neutral-content rounded-box mt-1 w-60">
<li>
<label tabIndex={0} role="button">
Upload a new photo...
<input
type="file"
name="photo"
id="upload-photo"
accept=".png, .jpeg, .jpg"
className="hidden"
onChange={handleImageUpload}
/>
</label>
</li>
{user.image && (
<li>
<div
tabIndex={0}
role="button"
onClick={() =>
setUser({
...user,
image: "",
})
}
>
Remove Photo
</div>
</li>
)}
</ul>
2023-10-18 16:50:55 -05:00
</div>
</div>
</div>
</div>
2024-05-16 14:02:22 -05:00
<div className="sm:-mt-3">
<Checkbox
label="Make profile private"
state={user.isPrivate}
onClick={() => setUser({ ...user, isPrivate: !user.isPrivate })}
/>
<p className="text-neutral text-sm">
This will limit who can find and add you to new Collections.
</p>
{user.isPrivate && (
<div className="pl-5">
<p className="mt-2">Whitelisted Users</p>
<p className="text-neutral text-sm mb-3">
Please provide the Username of the users you wish to grant
visibility to your profile. Separated by comma.
</p>
<textarea
className="w-full resize-none border rounded-md duration-100 bg-base-200 p-2 outline-none border-neutral-content focus:border-primary"
placeholder="Your profile is hidden from everyone right now..."
value={whitelistedUsersTextbox}
onChange={(e) => setWhiteListedUsersTextbox(e.target.value)}
/>
</div>
)}
</div>
<SubmitButton
onClick={() => {
if (account.email !== user.email) {
setEmailChangeVerificationModal(true);
} else {
submit();
}
}}
loading={submitLoader}
label="Save Changes"
className="mt-2 w-full sm:w-fit"
/>
2023-10-18 16:50:55 -05:00
<div>
<div className="flex items-center gap-2 w-full rounded-md h-8">
2023-11-24 07:39:55 -06:00
<p className="truncate w-full pr-7 text-3xl font-thin">
2023-10-18 16:50:55 -05:00
Import & Export
</p>
</div>
<div className="divider my-3"></div>
2023-10-18 16:50:55 -05:00
<div className="flex gap-3 flex-col">
<div>
2023-11-24 07:39:55 -06:00
<p className="mb-2">Import your data from other platforms.</p>
<div className="dropdown dropdown-bottom">
<Button
tabIndex={0}
role="button"
intent="secondary"
2024-01-14 09:09:09 -06:00
onMouseDown={dropdownTriggerer}
className="text-sm"
2023-10-18 16:50:55 -05:00
id="import-dropdown"
>
2023-12-17 22:32:33 -06:00
<i className="bi-cloud-upload text-xl duration-100"></i>
Import From
</Button>
<ul className="shadow menu dropdown-content z-[1] bg-base-200 border border-neutral-content rounded-box mt-1 w-60">
2023-12-01 13:00:52 -06:00
<li>
<label
tabIndex={0}
role="button"
2023-12-01 13:00:52 -06:00
htmlFor="import-linkwarden-file"
title="JSON File"
>
From Linkwarden
<input
type="file"
name="photo"
id="import-linkwarden-file"
accept=".json"
className="hidden"
onChange={(e) =>
importBookmarks(e, MigrationFormat.linkwarden)
}
/>
</label>
</li>
<li>
<label
tabIndex={0}
role="button"
2023-12-01 13:00:52 -06:00
htmlFor="import-html-file"
title="HTML File"
>
From Bookmarks HTML file
<input
type="file"
name="photo"
id="import-html-file"
accept=".html"
className="hidden"
onChange={(e) =>
importBookmarks(e, MigrationFormat.htmlFile)
}
/>
</label>
</li>
2024-05-25 17:26:24 -05:00
<li>
<label
tabIndex={0}
role="button"
htmlFor="import-wallabag-file"
title="Wallabag File"
>
From Wallabag (JSON file)
<input
type="file"
name="photo"
id="import-wallabag-file"
accept=".json"
className="hidden"
onChange={(e) =>
importBookmarks(e, MigrationFormat.wallabag)
}
/>
</label>
</li>
2023-12-01 13:00:52 -06:00
</ul>
</div>
2023-10-18 16:50:55 -05:00
</div>
<div>
2023-11-24 07:39:55 -06:00
<p className="mb-2">Download your data instantly.</p>
<Link className="w-fit" href="/api/v1/migration">
<div className="select-none relative duration-200 rounded-lg text-sm text-center w-fit flex justify-center items-center gap-2 disabled:pointer-events-none disabled:opacity-50 bg-neutral-content text-secondary-foreground hover:bg-neutral-content/80 border border-neutral/30 h-10 px-4 py-2">
2023-12-17 22:32:33 -06:00
<i className="bi-cloud-download text-xl duration-100"></i>
2023-12-07 11:29:45 -06:00
<p>Export Data</p>
2023-10-18 16:50:55 -05:00
</div>
</Link>
</div>
</div>
</div>
<div>
<div className="flex items-center gap-2 w-full rounded-md h-8">
<p className="text-red-500 dark:text-red-500 truncate w-full pr-7 text-3xl font-thin">
Delete Account
</p>
</div>
<div className="divider my-3"></div>
<p>
This will permanently delete ALL the Links, Collections, Tags, and
archived data you own.{" "}
{process.env.NEXT_PUBLIC_STRIPE
? "It will also cancel your subscription. "
: undefined}
</p>
</div>
<Link href="/settings/delete" className="underline">
Account deletion page
</Link>
2023-10-18 16:50:55 -05:00
</div>
2024-05-16 14:02:22 -05:00
{emailChangeVerificationModal ? (
<EmailChangeVerificationModal
onClose={() => setEmailChangeVerificationModal(false)}
onSubmit={submit}
oldEmail={account.email || ""}
newEmail={user.email || ""}
/>
) : undefined}
2023-10-18 16:50:55 -05:00
</SettingsLayout>
);
}