Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

5.0 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type { LocaleKey } from '../../locale/types';
import type { IBusinessComponentProps } from './interface';
import { ILocalStorageService, LocaleService } from '@univerjs/core';
import { borderClassName, clsx, Input } from '@univerjs/design';
import { CheckMarkIcon } from '@univerjs/icons';
import { CURRENCYFORMAT, getDateFormatOptions, NUMBERFORMAT } from '@univerjs/sheets-numfmt';
import { useDependency, useObservable } from '@univerjs/ui';
import { useEffect, useLayoutEffect, useState } from 'react';
import { UserHabitController } from '../../controllers/user-habit.controller';

const key = 'customFormat';
const historyPatternKey = 'numfmt_custom_pattern';

export function CustomFormat(props: IBusinessComponentProps) {
    const { defaultPattern, onActionChange, onChange } = props;
    const userHabitController = useDependency(UserHabitController);
    const localStorageService = useDependency(ILocalStorageService);
    const localeService = useDependency(LocaleService);
    const locale = useObservable(localeService.currentLocale$, localeService.getCurrentLocale());

    const [pattern, setPattern] = useState(defaultPattern);
    useLayoutEffect(() => {
        onActionChange(() => {
            userHabitController.markHabit(key, pattern);
            localStorageService.getItem<string[]>(historyPatternKey).then((list = []) => {
                const _list = [...new Set([pattern, ...(list || [])])].splice(0, 10).filter((e) => !!e);
                return localStorageService.setItem(historyPatternKey, _list);
            }).catch(console.error);
            return pattern;
        });
    }, [localStorageService, onActionChange, pattern, userHabitController]);
    const [options, setOptions] = useState<(string | number)[]>([]);

    useEffect(() => {
        let cancelled = false;

        localStorageService.getItem<string[]>(historyPatternKey).then((historyList) => {
            const list = [
                ...CURRENCYFORMAT.map((item) => item.suffix('$')),
                ...getDateFormatOptions(locale).map((item) => item.value),
                ...NUMBERFORMAT.map((item) => item.suffix),
                ...(historyList ?? []),
            ];
            return userHabitController.addHabit(key, []).then(() => userHabitController.getHabit(key, list));
        }).then((list) => {
            if (!cancelled) {
                setOptions([...new Set(list)]);
            }
        }).catch(console.error);

        return () => {
            cancelled = true;
        };
    }, [locale, localStorageService, userHabitController]);

    const handleClick = (p: string) => {
        setPattern(p);
        onChange(p);
    };

    const handleBlur = () => {
        onChange(pattern);
    };

    return (
        <div>
            <div className="univer-mt-4 univer-text-sm univer-text-gray-400">
                {localeService.t<LocaleKey>('sheets-numfmt-ui.customFormat')}
            </div>
            <Input
                placeholder={localeService.t<LocaleKey>('sheets-numfmt-ui.customFormat')}
                onBlur={handleBlur}
                value={pattern}
                onChange={setPattern}
                className="univer-mt-2 univer-w-full"
            />
            <div
                className={clsx(
                    'univer-mt-2 univer-max-h-[400px] univer-overflow-auto univer-rounded-lg univer-p-2',
                    borderClassName
                )}
            >
                {options.map((p) => (
                    <div
                        key={p}
                        onClick={() => handleClick(p as string)}
                        className={`
                          univer-flex univer-cursor-pointer univer-items-center univer-gap-1.5 univer-py-1.5
                          univer-text-sm
                        `}
                    >
                        <div className="univer-flex univer-w-4 univer-items-center univer-text-primary-600">
                            {pattern === p && <CheckMarkIcon />}
                        </div>
                        <div>
                            {p}
                        </div>
                    </div>
                ))}
            </div>
            <div
                className={`
                  univer-mt-3 univer-text-sm univer-text-gray-600
                  dark:!univer-text-gray-200
                `}
            >
                {localeService.t<LocaleKey>('sheets-numfmt-ui.customFormatDes')}
            </div>
        </div>
    );
}