How to use the ahooks.usePersistFn function in ahooks

To help you get started, we’ve selected a few ahooks examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pingcap / tidb-dashboard / ui / lib / apps / Configuration / InlineEditor.tsx View on Github external
function InlineEditor({
  value,
  displayValue,
  title,
  onSave,
}: IInlineEditorProps) {
  const [isVisible, setIsVisible] = useState(false)
  const [inputVal, setInputVal] = useState(displayValue)
  const [isPosting, setIsPosting] = useState(false)

  const handleCancel = useCallback(() => {
    setIsVisible(false)
    setInputVal(displayValue)
  }, [displayValue])

  const handleSave = usePersistFn(async () => {
    if (!onSave) {
      setIsVisible(false)
      return
    }
    try {
      setIsPosting(true)
      // PD only accept modified config in the same value type,
      // i.e. true => false, but not true => "false"
      const r = await onSave(valueWithSameType(inputVal, value))
      if (r !== false) {
        // When onSave returns non-false, input value is not reverted and only popup is hidden
        setIsVisible(false)
      } else {
        // When onSave returns false, popup is not hidden and value is reverted
        setInputVal(displayValue)
      }
github pingcap / tidb-dashboard / ui / lib / components / MultiSelect / index.tsx View on Github external
function MultiSelect(props: IMultiSelectProps) {
  const [internalVal, setInternalVal] = useControllableValue(props)
  const setInternalValPersist = usePersistFn(setInternalVal)
  const {
    items,
    filterFn,
    selectedValueTransKey,
    columnTitle,
    placeholder,
    value, // only to exclude from restProps
    onChange, // only to exclude from restProps
    ...restProps
  } = props

  const { t } = useTranslation()

  const columns: IColumn[] = useMemo(
    () => [
      {
github pingcap / tidb-dashboard / ui / lib / apps / Configuration / index.tsx View on Github external
function Value({ item, onSaved }: IValueProps) {
  const handleSave = usePersistFn(async (newValue) => {
    try {
      const resp = await client.getInstance().configurationEdit({
        id: item.id,
        kind: item.kind,
        new_value: newValue,
      })
      if ((resp?.data?.warnings?.length ?? 0) > 0) {
        Modal.warning({
          title: 'Edit configuration is partially done',
          content: (
            <pre>{resp.data.warnings?.map((w) =&gt; w.message).join('\n\n')}</pre>
          ),
        })
      }
    } catch (e) {
      return false
github pingcap / tidb-dashboard / ui / lib / components / CardTable / GroupHeader.tsx View on Github external
function BaseAntCheckboxGroupHeader(props: IGroupHeaderProps) {
  const _classNames = getClassNames(props.styles, {
    theme: props.theme!,
    className: props.className,
    selected: props.selected,
    isCollapsed: props.group?.isCollapsed,
    compact: props.compact,
  })

  const _onHeaderClick = usePersistFn(() =&gt; {
    if (props.onToggleSelectGroup) {
      props.onToggleSelectGroup(props.group!)
    }
  })

  const _onToggleSelectGroupClick = usePersistFn(
    (ev: React.MouseEvent) =&gt; {
      if (props.onToggleSelectGroup) {
        props.onToggleSelectGroup(props.group!)
      }
      ev.preventDefault()
      ev.stopPropagation()
    }
  )

  const _onToggleCollapse = usePersistFn(
    (ev: React.MouseEvent) =&gt; {
      if (props.onToggleCollapse) {
        props.onToggleCollapse(props.group!)
      }
      ev.stopPropagation()
      ev.preventDefault()
github pingcap / tidb-dashboard / ui / lib / components / CardTable / GroupHeader.tsx View on Github external
function BaseAntCheckboxGroupHeader(props: IGroupHeaderProps) {
  const _classNames = getClassNames(props.styles, {
    theme: props.theme!,
    className: props.className,
    selected: props.selected,
    isCollapsed: props.group?.isCollapsed,
    compact: props.compact,
  })

  const _onHeaderClick = usePersistFn(() =&gt; {
    if (props.onToggleSelectGroup) {
      props.onToggleSelectGroup(props.group!)
    }
  })

  const _onToggleSelectGroupClick = usePersistFn(
    (ev: React.MouseEvent) =&gt; {
      if (props.onToggleSelectGroup) {
        props.onToggleSelectGroup(props.group!)
      }
      ev.preventDefault()
      ev.stopPropagation()
    }
  )

  const _onToggleCollapse = usePersistFn(
github pingcap / tidb-dashboard / ui / lib / apps / SystemReport / components / ReportHistory.tsx View on Github external
export default function ReportHistory() {
  const navigate = useNavigate()
  const { t } = useTranslation()
  const { data, isLoading, error } = useClientRequest((reqConfig) =&gt;
    client.getInstance().diagnoseReportsGet(reqConfig)
  )
  const columns = useMemo(() =&gt; tableColumns(t), [t])

  const handleRowClick = usePersistFn(
    (rec, _idx, ev: React.MouseEvent) =&gt; {
      openLink(`/system_report/detail?id=${rec.id}`, ev, navigate)
    }
  )

  return (
    
  )
}
github pingcap / tidb-dashboard / ui / lib / components / CardTable / GroupHeader.tsx View on Github external
if (props.onToggleSelectGroup) {
      props.onToggleSelectGroup(props.group!)
    }
  })

  const _onToggleSelectGroupClick = usePersistFn(
    (ev: React.MouseEvent) =&gt; {
      if (props.onToggleSelectGroup) {
        props.onToggleSelectGroup(props.group!)
      }
      ev.preventDefault()
      ev.stopPropagation()
    }
  )

  const _onToggleCollapse = usePersistFn(
    (ev: React.MouseEvent) =&gt; {
      if (props.onToggleCollapse) {
        props.onToggleCollapse(props.group!)
      }
      ev.stopPropagation()
      ev.preventDefault()
    }
  )

  return (
    <div style="{props.viewport">
      </div>
github pingcap / tidb-dashboard / ui / lib / apps / Configuration / InlineEditor.tsx View on Github external
setInputVal(displayValue)
      setIsVisible(false)
    } finally {
      setIsPosting(false)
    }
  })

  const handleInputValueChange = useCallback((e) =&gt; {
    setInputVal(e.target.value)
  }, [])

  useEffect(() =&gt; {
    setInputVal(displayValue)
  }, [displayValue])

  const renderPopover = usePersistFn(() =&gt; {
    return (
      
        <div>
          <input disabled="{isPosting}" size="small" value="{inputVal}">
        </div>
        <div>
          
            </div>
github pingcap / tidb-dashboard / ui / lib / apps / SlowQuery / components / SlowQueriesTable.tsx View on Github external
function SlowQueriesTable({ controller, ...restProps }: Props) {
  const {
    loadingSlowQueries,
    tableColumns,
    slowQueries,
    orderOptions: { orderBy, desc },
    changeOrder,
    errors,
    visibleColumnKeys,
  } = controller

  const navigate = useNavigate()
  const handleRowClick = usePersistFn(
    (rec, _idx, ev: React.MouseEvent) =&gt; {
      const qs = DetailPage.buildQuery({
        digest: rec.digest,
        connectId: rec.connection_id,
        timestamp: rec.timestamp,
      })
      openLink(`/slow_query/detail?${qs}`, ev, navigate)
    }
  )

  const getKey = useCallback((row) =&gt; `${row.digest}_${row.timestamp}`, [])

  return (
github pingcap / tidb-dashboard / ui / lib / apps / Statement / components / StatementsTable.tsx View on Github external
export default function StatementsTable({ controller, ...restPrpos }: Props) {
  const {
    orderOptions,
    changeOrder,
    validTimeRange: { begin_time, end_time },
    loadingStatements,
    statements,
    errors,
    tableColumns,
    visibleColumnKeys,
  } = controller

  const navigate = useNavigate()
  const handleRowClick = usePersistFn(
    (rec, _idx, ev: React.MouseEvent) =&gt; {
      const qs = DetailPage.buildQuery({
        digest: rec.digest,
        schema: rec.schema_name,
        beginTime: begin_time,
        endTime: end_time,
      })
      openLink(`/statement/detail?${qs}`, ev, navigate)
    }
  )

  const getKey = useCallback((row) =&gt; `${row.digest}_${row.schema_name}`, [])

  return (