import styled from "@emotion/styled" import type { ItemSection, ItemField } from "opvault.js" import { FieldType } from "opvault.js" import { useCallback, useMemo, useState } from "react" import { useTranslate } from "../i18n" import { parseMonthYear } from "../utils" import { BigTextView } from "./BigTextView" import { ErrorBoundary } from "./ErrorBoundary" import { useItemFieldContextMenu } from "./ItemFieldContextMenu" import { toast, ToastType } from "./Toast" const Container = styled.div` cursor: pointer; &:hover { color: #6fa9ff; text-decoration: underline; } ` export { Container as ClickableContainer } function useCopy(text: string) { const t = useTranslate() return useCallback(() => { navigator.clipboard.writeText(text) toast({ type: ToastType.Secondary, message: t.tips.copied_to_clipboard, }) }, [text, t]) } export { Password as PasswordFieldView } const Password: React.FC<{ field: Pick }> = ({ field }) => { const t = useTranslate() const [show, setShow] = useState(false) const [bigText, showBigText] = useState(false) const { onRightClick, ContextMenuContainer, Item } = useItemFieldContextMenu() const onToggle = useCallback(() => setShow(x => !x), []) const onCopy = useCopy(field.v) const onOpenBigText = useCallback(() => { showBigText(true) }, []) const onCloseBigText = useCallback(() => { showBigText(false) }, []) return ( <> setShow(x => !x)} onClick={onCopy} style={{ fontFamily: "var(--monospace)", ...(!show && { userSelect: "none" }), }} > {show ? field.v : "ยท".repeat(10)} {bigText && {field.v}} {t.action.copy} {show ? t.action.hide : t.action.show} {!bigText && ( {t.action.show_in_big_characters} )} ) } const MonthYear: React.FC<{ field: ItemSection.MonthYear }> = ({ field }) => { const { year, month } = parseMonthYear(field.v) return ( {month.toString().padStart(2, "0")}/{year.toString().padStart(4, "0")} ) } const DateView: React.FC<{ field: ItemSection.Date }> = ({ field }) => { const date = useMemo(() => new Date(field.v * 1000), [field.v]) return {date.toLocaleDateString()} } const TextView: React.FC<{ value: string }> = ({ value }) => { const { onRightClick, ContextMenuContainer, Item } = useItemFieldContextMenu() const onCopy = useCopy(value) return ( <> {value} Copier ) } export const ItemFieldValue: React.FC<{ field: ItemSection.Any }> = ({ field }) => { if (field.v == null) { return null } switch (field.k) { case "concealed": return case "monthYear": return case "date": return case "address": return (
{field.v.street}
{field.v.city}, {field.v.state} ({field.v.zip})
{field.v.country}
) } return ( ) } export const ItemDetailsFieldValue: React.FC<{ field: ItemField }> = ({ field }) => { if (field.type === FieldType.Password || field.designation === "password") { return } return ( ) }