import React, { ChangeEventHandler, FormEventHandler, useCallback, useMemo, useState } from 'react'; import { IPushRule, IPushRules, PushRuleKind } from 'matrix-js-sdk'; import { Box, Text, Badge, Button, Input, config, IconButton, Icons, Icon, Spinner } from 'folds'; import { useAccountData } from '../../../hooks/useAccountData'; import { AccountDataEvent } from '../../../../types/matrix/accountData'; import { SequenceCard } from '../../../components/sequence-card'; import { SequenceCardStyle } from '../styles.css'; import { SettingTile } from '../../../components/setting-tile'; import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { getNotificationModeActions, NotificationMode, NotificationModeOptions, useNotificationModeActions, } from '../../../hooks/useNotificationMode'; import { NotificationModeSwitcher } from './NotificationModeSwitcher'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; const NOTIFY_MODE_OPS: NotificationModeOptions = { highlight: true, }; function KeywordInput() { const mx = useMatrixClient(); const [keyword, setKeyword] = useState(''); const [keywordState, addKeyword] = useAsyncCallback( useCallback( async (k: string) => { mx.addPushRule('global', PushRuleKind.ContentSpecific, k, { actions: getNotificationModeActions(NotificationMode.Notify, NOTIFY_MODE_OPS), pattern: k, }); setKeyword(''); }, [mx] ) ); const addingKeyword = keywordState.status === AsyncStatus.Loading; const handleChange: ChangeEventHandler = (evt) => { const k = evt.currentTarget.value; setKeyword(k); }; const handleReset = () => { setKeyword(''); }; const handleSubmit: FormEventHandler = (evt) => { evt.preventDefault(); if (addingKeyword) return; const target = evt.target as HTMLFormElement | undefined; const keywordInput = target?.keywordInput as HTMLInputElement | undefined; const k = keywordInput?.value.trim(); if (!k) return; addKeyword(k); }; return ( ) } /> ); } type PushRulesProps = { pushRule: IPushRule; }; function KeywordCross({ pushRule }: PushRulesProps) { const mx = useMatrixClient(); const [removeState, remove] = useAsyncCallback( useCallback( () => mx.deletePushRule('global', PushRuleKind.ContentSpecific, pushRule.rule_id), [mx, pushRule] ) ); const removing = removeState.status === AsyncStatus.Loading; return ( {removing ? : } ); } function KeywordModeSwitcher({ pushRule }: PushRulesProps) { const mx = useMatrixClient(); const getModeActions = useNotificationModeActions(NOTIFY_MODE_OPS); const handleChange = useCallback( async (mode: NotificationMode) => { const actions = getModeActions(mode); await mx.setPushRuleActions( 'global', PushRuleKind.ContentSpecific, pushRule.rule_id, actions ); }, [mx, getModeActions, pushRule] ); return ; } export function KeywordMessagesNotifications() { const pushRulesEvt = useAccountData(AccountDataEvent.PushRules); const pushRules = useMemo( () => pushRulesEvt?.getContent() ?? { global: {} }, [pushRulesEvt] ); const keywordPushRules = useMemo(() => { const content = pushRules.global.content ?? []; return content.filter( (pushRule) => pushRule.default === false && typeof pushRule.pattern === 'string' ); }, [pushRules]); return ( Keyword Messages Badge: 1 {keywordPushRules.map((pushRule) => ( } after={} /> ))} ); }