@blocklet/ui-react 3.4.15 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/common/org-switch/use-org.d.ts +4 -4
- package/package.json +9 -6
- package/.aigne/doc-smith/.local/afs-storage.sqlite3 +0 -0
- package/.aigne/doc-smith/config.yaml +0 -78
- package/.aigne/doc-smith/history.yaml +0 -14
- package/.aigne/doc-smith/media-description.yaml +0 -11
- package/.aigne/doc-smith/output/structure-plan.json +0 -255
- package/.aigne/doc-smith/translation-cache.yaml +0 -11
- package/.aigne/doc-smith/upload-cache.yaml +0 -528
- package/build.config.ts +0 -24
- package/docs/_sidebar.md +0 -19
- package/docs/assets/diagram/component-installer-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/component-installer-diagram-0.jpg +0 -0
- package/docs/assets/diagram/component-installer-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/component-installer-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/component-management-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/component-management-diagram-0.jpg +0 -0
- package/docs/assets/diagram/component-management-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/component-management-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/core-concepts-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/core-concepts-diagram-0.jpg +0 -0
- package/docs/assets/diagram/core-concepts-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/core-concepts-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/dashboard-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/dashboard-diagram-0.jpg +0 -0
- package/docs/assets/diagram/dashboard-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/dashboard-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/header-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/header-diagram-0.jpg +0 -0
- package/docs/assets/diagram/header-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/header-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/layout-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/layout-diagram-0.jpg +0 -0
- package/docs/assets/diagram/layout-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/layout-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/notifications-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/notifications-diagram-0.jpg +0 -0
- package/docs/assets/diagram/notifications-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/notifications-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/overview-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/overview-diagram-0.jpg +0 -0
- package/docs/assets/diagram/overview-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/overview-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/user-center-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/user-center-diagram-0.jpg +0 -0
- package/docs/assets/diagram/user-center-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/user-center-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/user-management-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/user-management-diagram-0.jpg +0 -0
- package/docs/assets/diagram/user-management-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/user-management-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/user-sessions-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/user-sessions-diagram-0.jpg +0 -0
- package/docs/assets/diagram/user-sessions-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/user-sessions-diagram-0.zh.jpg +0 -0
- package/docs/components-component-management-blocklet-studio.ja.md +0 -194
- package/docs/components-component-management-blocklet-studio.md +0 -194
- package/docs/components-component-management-blocklet-studio.zh-TW.md +0 -194
- package/docs/components-component-management-blocklet-studio.zh.md +0 -194
- package/docs/components-component-management-component-installer.ja.md +0 -182
- package/docs/components-component-management-component-installer.md +0 -182
- package/docs/components-component-management-component-installer.zh-TW.md +0 -182
- package/docs/components-component-management-component-installer.zh.md +0 -182
- package/docs/components-component-management.ja.md +0 -30
- package/docs/components-component-management.md +0 -30
- package/docs/components-component-management.zh-TW.md +0 -30
- package/docs/components-component-management.zh.md +0 -30
- package/docs/components-layout-dashboard.ja.md +0 -185
- package/docs/components-layout-dashboard.md +0 -187
- package/docs/components-layout-dashboard.zh-TW.md +0 -185
- package/docs/components-layout-dashboard.zh.md +0 -185
- package/docs/components-layout-footer.ja.md +0 -165
- package/docs/components-layout-footer.md +0 -165
- package/docs/components-layout-footer.zh-TW.md +0 -165
- package/docs/components-layout-footer.zh.md +0 -165
- package/docs/components-layout-header.ja.md +0 -183
- package/docs/components-layout-header.md +0 -183
- package/docs/components-layout-header.zh-TW.md +0 -183
- package/docs/components-layout-header.zh.md +0 -183
- package/docs/components-layout.ja.md +0 -31
- package/docs/components-layout.md +0 -31
- package/docs/components-layout.zh-TW.md +0 -31
- package/docs/components-layout.zh.md +0 -31
- package/docs/components-notifications.ja.md +0 -125
- package/docs/components-notifications.md +0 -125
- package/docs/components-notifications.zh-TW.md +0 -125
- package/docs/components-notifications.zh.md +0 -125
- package/docs/components-user-management-user-center.ja.md +0 -148
- package/docs/components-user-management-user-center.md +0 -147
- package/docs/components-user-management-user-center.zh-TW.md +0 -148
- package/docs/components-user-management-user-center.zh.md +0 -148
- package/docs/components-user-management-user-sessions.ja.md +0 -121
- package/docs/components-user-management-user-sessions.md +0 -123
- package/docs/components-user-management-user-sessions.zh-TW.md +0 -121
- package/docs/components-user-management-user-sessions.zh.md +0 -121
- package/docs/components-user-management.ja.md +0 -49
- package/docs/components-user-management.md +0 -51
- package/docs/components-user-management.zh-TW.md +0 -49
- package/docs/components-user-management.zh.md +0 -49
- package/docs/components-utilities-icon.ja.md +0 -106
- package/docs/components-utilities-icon.md +0 -106
- package/docs/components-utilities-icon.zh-TW.md +0 -106
- package/docs/components-utilities-icon.zh.md +0 -106
- package/docs/components-utilities.ja.md +0 -136
- package/docs/components-utilities.md +0 -136
- package/docs/components-utilities.zh-TW.md +0 -136
- package/docs/components-utilities.zh.md +0 -136
- package/docs/components.ja.md +0 -27
- package/docs/components.md +0 -27
- package/docs/components.zh-TW.md +0 -27
- package/docs/components.zh.md +0 -27
- package/docs/core-concepts.ja.md +0 -134
- package/docs/core-concepts.md +0 -135
- package/docs/core-concepts.zh-TW.md +0 -134
- package/docs/core-concepts.zh.md +0 -134
- package/docs/getting-started.ja.md +0 -132
- package/docs/getting-started.md +0 -132
- package/docs/getting-started.zh-TW.md +0 -132
- package/docs/getting-started.zh.md +0 -132
- package/docs/hooks-api.ja.md +0 -214
- package/docs/hooks-api.md +0 -214
- package/docs/hooks-api.zh-TW.md +0 -214
- package/docs/hooks-api.zh.md +0 -214
- package/docs/how-to-guides.ja.md +0 -413
- package/docs/how-to-guides.md +0 -413
- package/docs/how-to-guides.zh-TW.md +0 -413
- package/docs/how-to-guides.zh.md +0 -413
- package/docs/overview.ja.md +0 -51
- package/docs/overview.md +0 -51
- package/docs/overview.zh-TW.md +0 -51
- package/docs/overview.zh.md +0 -51
- package/glossary.md +0 -12
- package/src/@types/index.ts +0 -230
- package/src/@types/shims.d.ts +0 -18
- package/src/BlockletStudio/README.md +0 -116
- package/src/BlockletStudio/index.tsx +0 -145
- package/src/ComponentInstaller/ComponentInstaller.stories.jsx +0 -16
- package/src/ComponentInstaller/index.jsx +0 -207
- package/src/ComponentInstaller/installer-item.jsx +0 -129
- package/src/ComponentInstaller/locales.js +0 -22
- package/src/ComponentInstaller/use-component-installed.js +0 -88
- package/src/ComponentManager/components/add-component.tsx +0 -136
- package/src/ComponentManager/components/check-component.tsx +0 -3
- package/src/ComponentManager/components/publish-component.tsx +0 -90
- package/src/ComponentManager/components/resource-dialog.tsx +0 -91
- package/src/ComponentManager/index.tsx +0 -3
- package/src/ComponentManager/libs/locales.ts +0 -15
- package/src/Dashboard/Dashboard.stories.jsx +0 -20
- package/src/Dashboard/app-shell/app-badge.stories.tsx +0 -64
- package/src/Dashboard/app-shell/app-badge.tsx +0 -94
- package/src/Dashboard/app-shell/app-header.tsx +0 -104
- package/src/Dashboard/app-shell/app-info-context.tsx +0 -182
- package/src/Dashboard/app-shell/badges/app-badge-default.tsx +0 -130
- package/src/Dashboard/app-shell/badges/app-badge-did.tsx +0 -28
- package/src/Dashboard/app-shell/badges/app-badge-state.tsx +0 -40
- package/src/Dashboard/app-shell/badges/app-badge-switch.tsx +0 -72
- package/src/Dashboard/app-shell/badges/app-badge-version.tsx +0 -60
- package/src/Dashboard/app-shell/index.ts +0 -5
- package/src/Dashboard/index.jsx +0 -184
- package/src/Footer/Footer.stories.jsx +0 -33
- package/src/Footer/brand.jsx +0 -81
- package/src/Footer/copyright.jsx +0 -22
- package/src/Footer/index.jsx +0 -111
- package/src/Footer/internal-footer.jsx +0 -139
- package/src/Footer/layout/plain.jsx +0 -55
- package/src/Footer/layout/row.jsx +0 -43
- package/src/Footer/layout/standard.jsx +0 -114
- package/src/Footer/links.jsx +0 -321
- package/src/Footer/social-media.jsx +0 -55
- package/src/Header/Header.stories.jsx +0 -30
- package/src/Header/index.tsx +0 -259
- package/src/Icon/Icon.stories.jsx +0 -12
- package/src/Icon/index.tsx +0 -87
- package/src/Notifications/Snackbar.tsx +0 -261
- package/src/Notifications/hooks/use-title.tsx +0 -254
- package/src/Notifications/hooks/use-width.tsx +0 -16
- package/src/Notifications/utils.ts +0 -246
- package/src/UserCenter/assets/banner.png +0 -0
- package/src/UserCenter/components/config-inviter.tsx +0 -48
- package/src/UserCenter/components/config-profile.tsx +0 -99
- package/src/UserCenter/components/danger-zone.tsx +0 -82
- package/src/UserCenter/components/editable-field.tsx +0 -273
- package/src/UserCenter/components/fallback.tsx +0 -57
- package/src/UserCenter/components/nft-preview.tsx +0 -84
- package/src/UserCenter/components/nft.tsx +0 -279
- package/src/UserCenter/components/notification.tsx +0 -319
- package/src/UserCenter/components/passport.tsx +0 -107
- package/src/UserCenter/components/privacy.tsx +0 -120
- package/src/UserCenter/components/settings.tsx +0 -170
- package/src/UserCenter/components/status-dialog/date-picker.tsx +0 -77
- package/src/UserCenter/components/status-dialog/index.tsx +0 -293
- package/src/UserCenter/components/status-selector/duration-menu.tsx +0 -90
- package/src/UserCenter/components/status-selector/index.tsx +0 -58
- package/src/UserCenter/components/status-selector/menu-item.tsx +0 -56
- package/src/UserCenter/components/storage/action.tsx +0 -49
- package/src/UserCenter/components/storage/connected.tsx +0 -61
- package/src/UserCenter/components/storage/delete.tsx +0 -72
- package/src/UserCenter/components/storage/disconnect.tsx +0 -40
- package/src/UserCenter/components/storage/icons/empty-spaces-nft.svg +0 -1
- package/src/UserCenter/components/storage/icons/long-arrow.svg +0 -5
- package/src/UserCenter/components/storage/icons/space-connected.svg +0 -3
- package/src/UserCenter/components/storage/icons/space-disconnect.svg +0 -3
- package/src/UserCenter/components/storage/index.tsx +0 -41
- package/src/UserCenter/components/storage/preview-nft.tsx +0 -72
- package/src/UserCenter/components/third-party-login/index.tsx +0 -199
- package/src/UserCenter/components/third-party-login/third-party-item.tsx +0 -296
- package/src/UserCenter/components/user-center.tsx +0 -787
- package/src/UserCenter/components/user-info/address.tsx +0 -143
- package/src/UserCenter/components/user-info/index.tsx +0 -4
- package/src/UserCenter/components/user-info/link-preview-input.tsx +0 -274
- package/src/UserCenter/components/user-info/metadata.tsx +0 -658
- package/src/UserCenter/components/user-info/social-actions/chat.tsx +0 -43
- package/src/UserCenter/components/user-info/social-actions/follow.tsx +0 -23
- package/src/UserCenter/components/user-info/social-actions/index.tsx +0 -17
- package/src/UserCenter/components/user-info/switch-role.tsx +0 -42
- package/src/UserCenter/components/user-info/timezone-select.tsx +0 -119
- package/src/UserCenter/components/user-info/user-basic-info.tsx +0 -292
- package/src/UserCenter/components/user-info/user-info-item.tsx +0 -54
- package/src/UserCenter/components/user-info/user-info.tsx +0 -91
- package/src/UserCenter/components/user-info/user-status.tsx +0 -234
- package/src/UserCenter/components/user-info/utils.ts +0 -320
- package/src/UserCenter/components/webhook-item.tsx +0 -248
- package/src/UserCenter/index.tsx +0 -1
- package/src/UserCenter/libs/locales.ts +0 -378
- package/src/UserCenter/libs/utils.ts +0 -30
- package/src/UserSessions/components/user-session-info.tsx +0 -78
- package/src/UserSessions/components/user-sessions.tsx +0 -545
- package/src/UserSessions/index.tsx +0 -1
- package/src/UserSessions/libs/locales.ts +0 -60
- package/src/UserSessions/libs/utils.ts +0 -82
- package/src/blocklets.js +0 -195
- package/src/common/domain-warning.jsx +0 -178
- package/src/common/header-addons.jsx +0 -119
- package/src/common/link-blocker.jsx +0 -20
- package/src/common/notification-addon.jsx +0 -135
- package/src/common/org-switch/avatar-uploader.jsx +0 -271
- package/src/common/org-switch/create.jsx +0 -267
- package/src/common/org-switch/index.jsx +0 -407
- package/src/common/org-switch/locales.js +0 -52
- package/src/common/org-switch/use-org.jsx +0 -79
- package/src/common/overridable-theme-provider.jsx +0 -17
- package/src/common/wallet-hidden-topbar.js +0 -14
- package/src/common/wizard-modal.jsx +0 -200
- package/src/common/ws.js +0 -68
- package/src/contexts/config-user-space.tsx +0 -88
- package/src/contexts/user-followers.tsx +0 -54
- package/src/hooks/use-follow.tsx +0 -75
- package/src/hooks/use-mobile.tsx +0 -6
- package/src/index.ts +0 -16
- package/src/libs/constant.ts +0 -1
- package/src/libs/spaces.tsx +0 -18
- package/src/libs/with-hide-when-embed.tsx +0 -24
- package/src/types.js +0 -45
- package/src/utils.js +0 -161
- package/vite.config.mjs +0 -34
package/docs/hooks-api.md
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
# Hooks API
|
|
2
|
-
|
|
3
|
-
This section provides a detailed reference for the custom React Hooks available within the library. These hooks are designed to encapsulate and reuse stateful logic, simplifying common tasks and interactions within a Blocklet environment.
|
|
4
|
-
|
|
5
|
-
## useComponentInstalled
|
|
6
|
-
|
|
7
|
-
This hook checks whether one or more specified optional components (identified by their DIDs) are installed. It is essential for implementing features that rely on other Blocklets as dependencies. The hook provides the installation status and necessary URLs to prompt the user for installation if required.
|
|
8
|
-
|
|
9
|
-
### Parameters
|
|
10
|
-
|
|
11
|
-
The hook accepts a single object with the following properties:
|
|
12
|
-
|
|
13
|
-
<x-field-group>
|
|
14
|
-
<x-field data-name="did" data-type="string | string[]" data-required="true">
|
|
15
|
-
<x-field-desc markdown>The Decentralized Identifier (DID) or an array of DIDs for the component(s) to check. A single string can contain multiple DIDs separated by `;;`.</x-field-desc>
|
|
16
|
-
</x-field>
|
|
17
|
-
<x-field data-name="onInstalled" data-type="function" data-required="false">
|
|
18
|
-
<x-field-desc markdown>An optional callback function that is triggered when all specified components are already installed.</x-field-desc>
|
|
19
|
-
</x-field>
|
|
20
|
-
<x-field data-name="onError" data-type="function" data-required="false">
|
|
21
|
-
<x-field-desc markdown>An optional callback function that is triggered when one or more specified components are not installed.</x-field-desc>
|
|
22
|
-
</x-field>
|
|
23
|
-
</x-field-group>
|
|
24
|
-
|
|
25
|
-
### Returns
|
|
26
|
-
|
|
27
|
-
It returns an object containing the installation state and related data:
|
|
28
|
-
|
|
29
|
-
<x-field-group>
|
|
30
|
-
<x-field data-name="optComponents" data-type="array">
|
|
31
|
-
<x-field-desc markdown>An array of component objects that are not installed. Each object includes metadata such as `meta.did`, `storeUrl`, and `installUrl`.</x-field-desc>
|
|
32
|
-
</x-field>
|
|
33
|
-
<x-field data-name="installed" data-type="boolean">
|
|
34
|
-
<x-field-desc markdown>A boolean value that is `true` if all specified components are installed and defined in the `blocklet.yml`, and `false` otherwise.</x-field-desc>
|
|
35
|
-
</x-field>
|
|
36
|
-
<x-field data-name="installStatus" data-type="object">
|
|
37
|
-
<x-field-desc markdown>An object where keys are component DIDs and values are their current installation status (e.g., 'waiting', 'installing'). This state is updated via `window.postMessage` events.</x-field-desc>
|
|
38
|
-
</x-field>
|
|
39
|
-
<x-field data-name="setInstallStatus" data-type="function">
|
|
40
|
-
<x-field-desc markdown>The state setter function to manually update the `installStatus` object.</x-field-desc>
|
|
41
|
-
</x-field>
|
|
42
|
-
<x-field data-name="definedInBlockletYML" data-type="boolean">
|
|
43
|
-
<x-field-desc markdown>A boolean indicating whether the component is defined in the blocklet's configuration (`blocklet.yml`).</x-field-desc>
|
|
44
|
-
</x-field>
|
|
45
|
-
</x-field-group>
|
|
46
|
-
|
|
47
|
-
### Example Usage
|
|
48
|
-
|
|
49
|
-
The following example demonstrates how to use `useComponentInstalled` to conditionally render a feature or a `ComponentInstaller` component if a dependency is missing.
|
|
50
|
-
|
|
51
|
-
```javascript "ComponentFeature.js" icon=logos:javascript
|
|
52
|
-
import React from 'react';
|
|
53
|
-
import { useComponentInstalled, ComponentInstaller } from '@blocklet/ui-react';
|
|
54
|
-
|
|
55
|
-
const REQUIRED_DID = 'z8ia24z55nve2TSF5m1aZ5322d9f48a43D4a'; // Example DID
|
|
56
|
-
|
|
57
|
-
function ComponentFeature() {
|
|
58
|
-
const { installed, optComponents } = useComponentInstalled({ did: REQUIRED_DID });
|
|
59
|
-
|
|
60
|
-
if (!installed) {
|
|
61
|
-
// Render the installer UI if the component is not present
|
|
62
|
-
return <ComponentInstaller components={optComponents} />;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Render the feature that depends on the installed component
|
|
66
|
-
return (
|
|
67
|
-
<div>
|
|
68
|
-
<h2>My Feature</h2>
|
|
69
|
-
<p>This feature requires the component with DID: {REQUIRED_DID}</p>
|
|
70
|
-
{/* ... feature implementation ... */}
|
|
71
|
-
</div>
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export default ComponentFeature;
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
## useFollow
|
|
79
|
-
|
|
80
|
-
This hook manages the following relationship between the currently authenticated user and another user specified by their DID. It handles the API calls for following and unfollowing, and provides the current follow status.
|
|
81
|
-
|
|
82
|
-
### Parameters
|
|
83
|
-
|
|
84
|
-
<x-field-group>
|
|
85
|
-
<x-field data-name="userDid" data-type="string" data-required="true">
|
|
86
|
-
<x-field-desc markdown>The DID of the user profile to check the follow status against.</x-field-desc>
|
|
87
|
-
</x-field>
|
|
88
|
-
<x-field data-name="t" data-type="function" data-required="true">
|
|
89
|
-
<x-field-desc markdown>A translation function used for displaying success or error messages (e.g., from `react-i18next`).</x-field-desc>
|
|
90
|
-
</x-field>
|
|
91
|
-
<x-field data-name="isMySelf" data-type="boolean" data-required="true">
|
|
92
|
-
<x-field-desc markdown>A boolean that should be set to `true` if the `userDid` belongs to the currently logged-in user.</x-field-desc>
|
|
93
|
-
</x-field>
|
|
94
|
-
</x-field-group>
|
|
95
|
-
|
|
96
|
-
### Returns
|
|
97
|
-
|
|
98
|
-
Returns an object with the following properties:
|
|
99
|
-
|
|
100
|
-
<x-field-group>
|
|
101
|
-
<x-field data-name="followed" data-type="boolean">
|
|
102
|
-
<x-field-desc markdown>Indicates if the current user is following the user specified by `userDid`. `true` if following, `false` otherwise.</x-field-desc>
|
|
103
|
-
</x-field>
|
|
104
|
-
<x-field data-name="followUser" data-type="function">
|
|
105
|
-
<x-field-desc markdown>A stable function to call to follow the user. It handles the API request and updates the `followed` state on success.</x-field-desc>
|
|
106
|
-
</x-field>
|
|
107
|
-
<x-field data-name="unfollowUser" data-type="function">
|
|
108
|
-
<x-field-desc markdown>A stable function to call to unfollow the user. It handles the API request and updates the `followed` state on success.</x-field-desc>
|
|
109
|
-
</x-field>
|
|
110
|
-
</x-field-group>
|
|
111
|
-
|
|
112
|
-
### Example Usage
|
|
113
|
-
|
|
114
|
-
This example shows how to create a `FollowButton` component that displays a "Follow" or "Unfollow" action based on the relationship status.
|
|
115
|
-
|
|
116
|
-
```javascript "FollowButton.js" icon=logos:javascript
|
|
117
|
-
import React from 'react';
|
|
118
|
-
import Button from '@mui/material/Button';
|
|
119
|
-
import { useTranslation } from 'react-i18next';
|
|
120
|
-
import { useFollow } from '@blocklet/ui-react/hooks';
|
|
121
|
-
import { useSession } from '@blocklet/did-connect-react';
|
|
122
|
-
|
|
123
|
-
function FollowButton({ profileDid }) {
|
|
124
|
-
const { session } = useSession();
|
|
125
|
-
const { t } = useTranslation();
|
|
126
|
-
const isMySelf = session?.user?.did === profileDid;
|
|
127
|
-
|
|
128
|
-
const { followed, followUser, unfollowUser } = useFollow({
|
|
129
|
-
userDid: profileDid,
|
|
130
|
-
t,
|
|
131
|
-
isMySelf,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
if (isMySelf) {
|
|
135
|
-
return null; // Do not show button on your own profile
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const handleClick = () => {
|
|
139
|
-
if (followed) {
|
|
140
|
-
unfollowUser();
|
|
141
|
-
} else {
|
|
142
|
-
followUser();
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
return (
|
|
147
|
-
<Button variant="contained" onClick={handleClick}>
|
|
148
|
-
{followed ? t('profile.unfollow') : t('profile.follow')}
|
|
149
|
-
</Button>
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export default FollowButton;
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
## useMobile
|
|
157
|
-
|
|
158
|
-
A simple utility hook for creating responsive components. It leverages Material-UI's `useMediaQuery` to determine if the current viewport width is below a specified breakpoint.
|
|
159
|
-
|
|
160
|
-
### Parameters
|
|
161
|
-
|
|
162
|
-
<x-field-group>
|
|
163
|
-
<x-field data-name="key" data-type="number | Breakpoint" data-default="'sm'" data-required="false">
|
|
164
|
-
<x-field-desc markdown>The Material-UI breakpoint key (e.g., `'xs'`, `'sm'`, `'md'`) or a pixel value to check against. The hook returns `true` if the screen width is smaller than this value.</x-field-desc>
|
|
165
|
-
</x-field>
|
|
166
|
-
</x-field-group>
|
|
167
|
-
|
|
168
|
-
### Returns
|
|
169
|
-
|
|
170
|
-
Returns a `boolean` value: `true` if the viewport is smaller than the specified breakpoint, `false` otherwise.
|
|
171
|
-
|
|
172
|
-
### Example Usage
|
|
173
|
-
|
|
174
|
-
This hook must be used within a component tree that is wrapped by a Material-UI `ThemeProvider`.
|
|
175
|
-
|
|
176
|
-
```javascript "ResponsiveComponent.js" icon=logos:javascript
|
|
177
|
-
import React from 'react';
|
|
178
|
-
import Typography from '@mui/material/Typography';
|
|
179
|
-
import { ThemeProvider, createTheme } from '@mui/material/styles';
|
|
180
|
-
import { useMobile } from '@blocklet/ui-react/hooks';
|
|
181
|
-
|
|
182
|
-
// A component that uses the hook
|
|
183
|
-
function ResponsiveComponent() {
|
|
184
|
-
const isMobile = useMobile({ key: 'md' }); // Check against the 'md' breakpoint
|
|
185
|
-
|
|
186
|
-
return (
|
|
187
|
-
<div>
|
|
188
|
-
{isMobile ? (
|
|
189
|
-
<Typography variant="h6">Mobile View</Typography>
|
|
190
|
-
) : (
|
|
191
|
-
<Typography variant="h4">Desktop View</Typography>
|
|
192
|
-
)}
|
|
193
|
-
<p>Resize your browser window to see the content change.</p>
|
|
194
|
-
</div>
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// The component must be wrapped in a ThemeProvider
|
|
199
|
-
const theme = createTheme();
|
|
200
|
-
|
|
201
|
-
function App() {
|
|
202
|
-
return (
|
|
203
|
-
<ThemeProvider theme={theme}>
|
|
204
|
-
<ResponsiveComponent />
|
|
205
|
-
</ThemeProvider>
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export default App;
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
---
|
|
213
|
-
|
|
214
|
-
By using these hooks, you can efficiently implement complex features with minimal boilerplate code. For more information on the components that might use these hooks, please see the [Components](./components.md) documentation.
|
package/docs/hooks-api.zh-TW.md
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
# Hooks API
|
|
2
|
-
|
|
3
|
-
本節提供該函式庫中可用的自訂 React Hooks 的詳細參考。這些 Hooks 旨在封裝和重用有狀態的邏輯,簡化 Blocklet 環境中的常見任務和互動。
|
|
4
|
-
|
|
5
|
-
## useComponentInstalled
|
|
6
|
-
|
|
7
|
-
此 Hook 檢查一個或多個指定的選擇性元件(透過其 DID 識別)是否已安裝。對於實作依賴其他 Blocklet 作為相依項的功能至關重要。此 Hook 提供安裝狀態和必要的 URL,以便在需要時提示使用者進行安裝。
|
|
8
|
-
|
|
9
|
-
### 參數
|
|
10
|
-
|
|
11
|
-
此 Hook 接受一個包含以下屬性的物件:
|
|
12
|
-
|
|
13
|
-
<x-field-group>
|
|
14
|
-
<x-field data-name="did" data-type="string | string[]" data-required="true">
|
|
15
|
-
<x-field-desc markdown>要檢查的元件的去中心化識別碼(DID)或 DID 陣列。單一字串可以包含多個以 `;;` 分隔的 DID。</x-field-desc>
|
|
16
|
-
</x-field>
|
|
17
|
-
<x-field data-name="onInstalled" data-type="function" data-required="false">
|
|
18
|
-
<x-field-desc markdown>一個選擇性的回呼函式,當所有指定的元件都已安裝時觸發。</x-field-desc>
|
|
19
|
-
</x-field>
|
|
20
|
-
<x-field data-name="onError" data-type="function" data-required="false">
|
|
21
|
-
<x-field-desc markdown>一個選擇性的回呼函式,當一個或多個指定的元件未安裝時觸發。</x-field-desc>
|
|
22
|
-
</x-field>
|
|
23
|
-
</x-field-group>
|
|
24
|
-
|
|
25
|
-
### 回傳值
|
|
26
|
-
|
|
27
|
-
它回傳一個包含安裝狀態和相關資料的物件:
|
|
28
|
-
|
|
29
|
-
<x-field-group>
|
|
30
|
-
<x-field data-name="optComponents" data-type="array">
|
|
31
|
-
<x-field-desc markdown>一個未安裝的元件物件陣列。每個物件包含元資料,如 `meta.did`、`storeUrl` 和 `installUrl`。</x-field-desc>
|
|
32
|
-
</x-field>
|
|
33
|
-
<x-field data-name="installed" data-type="boolean">
|
|
34
|
-
<x-field-desc markdown>一個布林值,如果所有指定的元件都已安裝且在 `blocklet.yml` 中定義,則為 `true`,否則為 `false`。</x-field-desc>
|
|
35
|
-
</x-field>
|
|
36
|
-
<x-field data-name="installStatus" data-type="object">
|
|
37
|
-
<x-field-desc markdown>一個物件,其中鍵是元件的 DID,值是其目前的安裝狀態(例如「waiting」、「installing」)。此狀態透過 `window.postMessage` 事件更新。</x-field-desc>
|
|
38
|
-
</x-field>
|
|
39
|
-
<x-field data-name="setInstallStatus" data-type="function">
|
|
40
|
-
<x-field-desc markdown>用於手動更新 `installStatus` 物件的狀態設定函式。</x-field-desc>
|
|
41
|
-
</x-field>
|
|
42
|
-
<x-field data-name="definedInBlockletYML" data-type="boolean">
|
|
43
|
-
<x-field-desc markdown>一個布林值,表示該元件是否在 blocklet 的設定檔(`blocklet.yml`)中定義。</x-field-desc>
|
|
44
|
-
</x-field>
|
|
45
|
-
</x-field-group>
|
|
46
|
-
|
|
47
|
-
### 使用範例
|
|
48
|
-
|
|
49
|
-
以下範例示範如何使用 `useComponentInstalled` 來條件性地渲染一個功能,或在缺少相依項時渲染一個 `ComponentInstaller` 元件。
|
|
50
|
-
|
|
51
|
-
```javascript "ComponentFeature.js" icon=logos:javascript
|
|
52
|
-
import React from 'react';
|
|
53
|
-
import { useComponentInstalled, ComponentInstaller } from '@blocklet/ui-react';
|
|
54
|
-
|
|
55
|
-
const REQUIRED_DID = 'z8ia24z55nve2TSF5m1aZ5322d9f48a43D4a'; // 範例 DID
|
|
56
|
-
|
|
57
|
-
function ComponentFeature() {
|
|
58
|
-
const { installed, optComponents } = useComponentInstalled({ did: REQUIRED_DID });
|
|
59
|
-
|
|
60
|
-
if (!installed) {
|
|
61
|
-
// 如果元件不存在,則渲染安裝程式 UI
|
|
62
|
-
return <ComponentInstaller components={optComponents} />;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// 渲染依賴於已安裝元件的功能
|
|
66
|
-
return (
|
|
67
|
-
<div>
|
|
68
|
-
<h2>My Feature</h2>
|
|
69
|
-
<p>This feature requires the component with DID: {REQUIRED_DID}</p>
|
|
70
|
-
{/* ... 功能實作 ... */}
|
|
71
|
-
</div>
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export default ComponentFeature;
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
## useFollow
|
|
79
|
-
|
|
80
|
-
此 Hook 管理目前已驗證的使用者與另一位由其 DID 指定的使用者之間的追蹤關係。它處理追蹤和取消追蹤的 API 呼叫,並提供目前的追蹤狀態。
|
|
81
|
-
|
|
82
|
-
### 參數
|
|
83
|
-
|
|
84
|
-
<x-field-group>
|
|
85
|
-
<x-field data-name="userDid" data-type="string" data-required="true">
|
|
86
|
-
<x-field-desc markdown>要檢查追蹤狀態的使用者設定檔的 DID。</x-field-desc>
|
|
87
|
-
</x-field>
|
|
88
|
-
<x-field data-name="t" data-type="function" data-required="true">
|
|
89
|
-
<x-field-desc markdown>用於顯示成功或錯誤訊息的翻譯函式(例如,來自 `react-i18next`)。</x-field-desc>
|
|
90
|
-
</x-field>
|
|
91
|
-
<x-field data-name="isMySelf" data-type="boolean" data-required="true">
|
|
92
|
-
<x-field-desc markdown>如果 `userDid` 屬於目前登入的使用者,則應設定為 `true` 的布林值。</x-field-desc>
|
|
93
|
-
</x-field>
|
|
94
|
-
</x-field-group>
|
|
95
|
-
|
|
96
|
-
### 回傳值
|
|
97
|
-
|
|
98
|
-
回傳一個包含以下屬性的物件:
|
|
99
|
-
|
|
100
|
-
<x-field-group>
|
|
101
|
-
<x-field data-name="followed" data-type="boolean">
|
|
102
|
-
<x-field-desc markdown>表示目前使用者是否正在追蹤由 `userDid` 指定的使用者。如果正在追蹤則為 `true`,否則為 `false`。</x-field-desc>
|
|
103
|
-
</x-field>
|
|
104
|
-
<x-field data-name="followUser" data-type="function">
|
|
105
|
-
<x-field-desc markdown>一個穩定的函式,用於呼叫以追蹤使用者。它處理 API 請求並在成功時更新 `followed` 狀態。</x-field-desc>
|
|
106
|
-
</x-field>
|
|
107
|
-
<x-field data-name="unfollowUser" data-type="function">
|
|
108
|
-
<x-field-desc markdown>一個穩定的函式,用於呼叫以取消追蹤使用者。它處理 API 請求並在成功時更新 `followed` 狀態。</x-field-desc>
|
|
109
|
-
</x-field>
|
|
110
|
-
</x-field-group>
|
|
111
|
-
|
|
112
|
-
### 使用範例
|
|
113
|
-
|
|
114
|
-
此範例展示如何建立一個 `FollowButton` 元件,根據關係狀態顯示「追蹤」或「取消追蹤」操作。
|
|
115
|
-
|
|
116
|
-
```javascript "FollowButton.js" icon=logos:javascript
|
|
117
|
-
import React from 'react';
|
|
118
|
-
import Button from '@mui/material/Button';
|
|
119
|
-
import { useTranslation } from 'react-i18next';
|
|
120
|
-
import { useFollow } from '@blocklet/ui-react/hooks';
|
|
121
|
-
import { useSession } from '@blocklet/did-connect-react';
|
|
122
|
-
|
|
123
|
-
function FollowButton({ profileDid }) {
|
|
124
|
-
const { session } = useSession();
|
|
125
|
-
const { t } = useTranslation();
|
|
126
|
-
const isMySelf = session?.user?.did === profileDid;
|
|
127
|
-
|
|
128
|
-
const { followed, followUser, unfollowUser } = useFollow({
|
|
129
|
-
userDid: profileDid,
|
|
130
|
-
t,
|
|
131
|
-
isMySelf,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
if (isMySelf) {
|
|
135
|
-
return null; // 不在自己的個人資料頁面上顯示按鈕
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const handleClick = () => {
|
|
139
|
-
if (followed) {
|
|
140
|
-
unfollowUser();
|
|
141
|
-
} else {
|
|
142
|
-
followUser();
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
return (
|
|
147
|
-
<Button variant="contained" onClick={handleClick}>
|
|
148
|
-
{followed ? t('profile.unfollow') : t('profile.follow')}
|
|
149
|
-
</Button>
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export default FollowButton;
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
## useMobile
|
|
157
|
-
|
|
158
|
-
一個用於建立響應式元件的簡單工具 Hook。它利用 Material-UI 的 `useMediaQuery` 來判斷目前的視窗寬度是否低於指定的斷點。
|
|
159
|
-
|
|
160
|
-
### 參數
|
|
161
|
-
|
|
162
|
-
<x-field-group>
|
|
163
|
-
<x-field data-name="key" data-type="number | Breakpoint" data-default="'sm'" data-required="false">
|
|
164
|
-
<x-field-desc markdown>Material-UI 的斷點鍵(例如 `'xs'`、`'sm'`、`'md'`)或一個像素值用於檢查。如果螢幕寬度小於此值,此 Hook 回傳 `true`。</x-field-desc>
|
|
165
|
-
</x-field>
|
|
166
|
-
</x-field-group>
|
|
167
|
-
|
|
168
|
-
### 回傳值
|
|
169
|
-
|
|
170
|
-
回傳一個 `boolean` 值:如果視窗小於指定的斷點,則為 `true`,否則為 `false`。
|
|
171
|
-
|
|
172
|
-
### 使用範例
|
|
173
|
-
|
|
174
|
-
此 Hook 必須在被 Material-UI `ThemeProvider` 包裹的元件樹中使用。
|
|
175
|
-
|
|
176
|
-
```javascript "ResponsiveComponent.js" icon=logos:javascript
|
|
177
|
-
import React from 'react';
|
|
178
|
-
import Typography from '@mui/material/Typography';
|
|
179
|
-
import { ThemeProvider, createTheme } from '@mui/material/styles';
|
|
180
|
-
import { useMobile } from '@blocklet/ui-react/hooks';
|
|
181
|
-
|
|
182
|
-
// 一個使用此 Hook 的元件
|
|
183
|
-
function ResponsiveComponent() {
|
|
184
|
-
const isMobile = useMobile({ key: 'md' }); // 根據 'md' 斷點進行檢查
|
|
185
|
-
|
|
186
|
-
return (
|
|
187
|
-
<div>
|
|
188
|
-
{isMobile ? (
|
|
189
|
-
<Typography variant="h6">Mobile View</Typography>
|
|
190
|
-
) : (
|
|
191
|
-
<Typography variant="h4">Desktop View</Typography>
|
|
192
|
-
)}
|
|
193
|
-
<p>Resize your browser window to see the content change.</p>
|
|
194
|
-
</div>
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// 該元件必須被 ThemeProvider 包裹
|
|
199
|
-
const theme = createTheme();
|
|
200
|
-
|
|
201
|
-
function App() {
|
|
202
|
-
return (
|
|
203
|
-
<ThemeProvider theme={theme}>
|
|
204
|
-
<ResponsiveComponent />
|
|
205
|
-
</ThemeProvider>
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export default App;
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
---
|
|
213
|
-
|
|
214
|
-
透過使用這些 Hooks,您可以用最少的樣板程式碼高效地實作複雜的功能。有關可能使用這些 Hooks 的元件的更多資訊,請參閱[元件](./components.md)文件。
|
package/docs/hooks-api.zh.md
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
# Hooks API
|
|
2
|
-
|
|
3
|
-
本节为库中可用的自定义 React Hooks 提供了详细参考。这些 hooks 旨在封装和复用有状态逻辑,从而简化 Blocklet 环境中的常见任务和交互。
|
|
4
|
-
|
|
5
|
-
## useComponentInstalled
|
|
6
|
-
|
|
7
|
-
此 hook 检查一个或多个指定的可选组件(通过其 DID 识别)是否已安装。它对于实现依赖其他 Blocklet 的功能至关重要。该 hook 提供安装状态和必要的 URL,以便在需要时提示用户进行安装。
|
|
8
|
-
|
|
9
|
-
### 参数
|
|
10
|
-
|
|
11
|
-
该 hook 接受一个包含以下属性的对象:
|
|
12
|
-
|
|
13
|
-
<x-field-group>
|
|
14
|
-
<x-field data-name="did" data-type="string | string[]" data-required="true">
|
|
15
|
-
<x-field-desc markdown>要检查的组件的去中心化标识符(DID)或 DID 数组。单个字符串可包含多个由 `;;` 分隔的 DID。</x-field-desc>
|
|
16
|
-
</x-field>
|
|
17
|
-
<x-field data-name="onInstalled" data-type="function" data-required="false">
|
|
18
|
-
<x-field-desc markdown>一个可选的回调函数,当所有指定组件都已安装时触发。</x-field-desc>
|
|
19
|
-
</x-field>
|
|
20
|
-
<x-field data-name="onError" data-type="function" data-required="false">
|
|
21
|
-
<x-field-desc markdown>一个可选的回调函数,当一个或多个指定组件未安装时触发。</x-field-desc>
|
|
22
|
-
</x-field>
|
|
23
|
-
</x-field-group>
|
|
24
|
-
|
|
25
|
-
### 返回值
|
|
26
|
-
|
|
27
|
-
它返回一个包含安装状态和相关数据的对象:
|
|
28
|
-
|
|
29
|
-
<x-field-group>
|
|
30
|
-
<x-field data-name="optComponents" data-type="array">
|
|
31
|
-
<x-field-desc markdown>一个包含未安装组件对象的数组。每个对象都包含元数据,如 `meta.did`、`storeUrl` 和 `installUrl`。</x-field-desc>
|
|
32
|
-
</x-field>
|
|
33
|
-
<x-field data-name="installed" data-type="boolean">
|
|
34
|
-
<x-field-desc markdown>一个布尔值,如果所有指定组件都已安装并在 `blocklet.yml` 中定义,则为 `true`,否则为 `false`。</x-field-desc>
|
|
35
|
-
</x-field>
|
|
36
|
-
<x-field data-name="installStatus" data-type="object">
|
|
37
|
-
<x-field-desc markdown>一个对象,其中键是组件的 DID,值是其当前的安装状态(例如,'waiting'、'installing')。此状态通过 `window.postMessage` 事件更新。</x-field-desc>
|
|
38
|
-
</x-field>
|
|
39
|
-
<x-field data-name="setInstallStatus" data-type="function">
|
|
40
|
-
<x-field-desc markdown>用于手动更新 `installStatus` 对象的状态设置函数。</x-field-desc>
|
|
41
|
-
</x-field>
|
|
42
|
-
<x-field data-name="definedInBlockletYML" data-type="boolean">
|
|
43
|
-
<x-field-desc markdown>一个布尔值,指示组件是否在 blocklet 的配置(`blocklet.yml`)中定义。</x-field-desc>
|
|
44
|
-
</x-field>
|
|
45
|
-
</x-field-group>
|
|
46
|
-
|
|
47
|
-
### 示例用法
|
|
48
|
-
|
|
49
|
-
以下示例演示了如何在缺少依赖项时,使用 `useComponentInstalled` 来条件性地渲染一个功能或一个 `ComponentInstaller` 组件。
|
|
50
|
-
|
|
51
|
-
```javascript "ComponentFeature.js" icon=logos:javascript
|
|
52
|
-
import React from 'react';
|
|
53
|
-
import { useComponentInstalled, ComponentInstaller } from '@blocklet/ui-react';
|
|
54
|
-
|
|
55
|
-
const REQUIRED_DID = 'z8ia24z55nve2TSF5m1aZ5322d9f48a43D4a'; // 示例 DID
|
|
56
|
-
|
|
57
|
-
function ComponentFeature() {
|
|
58
|
-
const { installed, optComponents } = useComponentInstalled({ did: REQUIRED_DID });
|
|
59
|
-
|
|
60
|
-
if (!installed) {
|
|
61
|
-
// 如果组件不存在,则渲染安装器界面
|
|
62
|
-
return <ComponentInstaller components={optComponents} />;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// 渲染依赖于已安装组件的功能
|
|
66
|
-
return (
|
|
67
|
-
<div>
|
|
68
|
-
<h2>My Feature</h2>
|
|
69
|
-
<p>This feature requires the component with DID: {REQUIRED_DID}</p>
|
|
70
|
-
{/* ... 功能实现 ... */}
|
|
71
|
-
</div>
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export default ComponentFeature;
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
## useFollow
|
|
79
|
-
|
|
80
|
-
此 hook 管理当前认证用户与另一位由其 DID 指定的用户之间的关注关系。它处理关注和取消关注的 API 调用,并提供当前的关注状态。
|
|
81
|
-
|
|
82
|
-
### 参数
|
|
83
|
-
|
|
84
|
-
<x-field-group>
|
|
85
|
-
<x-field data-name="userDid" data-type="string" data-required="true">
|
|
86
|
-
<x-field-desc markdown>要检查关注状态的用户配置文件的 DID。</x-field-desc>
|
|
87
|
-
</x-field>
|
|
88
|
-
<x-field data-name="t" data-type="function" data-required="true">
|
|
89
|
-
<x-field-desc markdown>用于显示成功或错误消息的翻译函数(例如,来自 `react-i18next`)。</x-field-desc>
|
|
90
|
-
</x-field>
|
|
91
|
-
<x-field data-name="isMySelf" data-type="boolean" data-required="true">
|
|
92
|
-
<x-field-desc markdown>一个布尔值,如果 `userDid` 属于当前登录的用户,则应设置为 `true`。</x-field-desc>
|
|
93
|
-
</x-field>
|
|
94
|
-
</x-field-group>
|
|
95
|
-
|
|
96
|
-
### 返回值
|
|
97
|
-
|
|
98
|
-
返回一个包含以下属性的对象:
|
|
99
|
-
|
|
100
|
-
<x-field-group>
|
|
101
|
-
<x-field data-name="followed" data-type="boolean">
|
|
102
|
-
<x-field-desc markdown>指示当前用户是否正在关注由 `userDid` 指定的用户。如果正在关注,则为 `true`,否则为 `false`。</x-field-desc>
|
|
103
|
-
</x-field>
|
|
104
|
-
<x-field data-name="followUser" data-type="function">
|
|
105
|
-
<x-field-desc markdown>一个稳定的函数,用于调用以关注用户。它处理 API 请求并在成功后更新 `followed` 状态。</x-field-desc>
|
|
106
|
-
</x-field>
|
|
107
|
-
<x-field data-name="unfollowUser" data-type="function">
|
|
108
|
-
<x-field-desc markdown>一个稳定的函数,用于调用以取消关注用户。它处理 API 请求并在成功后更新 `followed` 状态。</x-field-desc>
|
|
109
|
-
</x-field>
|
|
110
|
-
</x-field-group>
|
|
111
|
-
|
|
112
|
-
### 示例用法
|
|
113
|
-
|
|
114
|
-
此示例展示了如何创建一个 `FollowButton` 组件,该组件根据关系状态显示“关注”或“取消关注”操作。
|
|
115
|
-
|
|
116
|
-
```javascript "FollowButton.js" icon=logos:javascript
|
|
117
|
-
import React from 'react';
|
|
118
|
-
import Button from '@mui/material/Button';
|
|
119
|
-
import { useTranslation } from 'react-i18next';
|
|
120
|
-
import { useFollow } from '@blocklet/ui-react/hooks';
|
|
121
|
-
import { useSession } from '@blocklet/did-connect-react';
|
|
122
|
-
|
|
123
|
-
function FollowButton({ profileDid }) {
|
|
124
|
-
const { session } = useSession();
|
|
125
|
-
const { t } = useTranslation();
|
|
126
|
-
const isMySelf = session?.user?.did === profileDid;
|
|
127
|
-
|
|
128
|
-
const { followed, followUser, unfollowUser } = useFollow({
|
|
129
|
-
userDid: profileDid,
|
|
130
|
-
t,
|
|
131
|
-
isMySelf,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
if (isMySelf) {
|
|
135
|
-
return null; // 不在自己的个人资料页显示按钮
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const handleClick = () => {
|
|
139
|
-
if (followed) {
|
|
140
|
-
unfollowUser();
|
|
141
|
-
} else {
|
|
142
|
-
followUser();
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
return (
|
|
147
|
-
<Button variant="contained" onClick={handleClick}>
|
|
148
|
-
{followed ? t('profile.unfollow') : t('profile.follow')}
|
|
149
|
-
</Button>
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export default FollowButton;
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
## useMobile
|
|
157
|
-
|
|
158
|
-
一个用于创建响应式组件的简单实用工具 hook。它利用 Material-UI 的 `useMediaQuery` 来确定当前视口宽度是否低于指定的断点。
|
|
159
|
-
|
|
160
|
-
### 参数
|
|
161
|
-
|
|
162
|
-
<x-field-group>
|
|
163
|
-
<x-field data-name="key" data-type="number | Breakpoint" data-default="'sm'" data-required="false">
|
|
164
|
-
<x-field-desc markdown>Material-UI 断点键(例如,`'xs'`、`'sm'`、`'md'`)或用于比较的像素值。如果屏幕宽度小于此值,该 hook 返回 `true`。</x-field-desc>
|
|
165
|
-
</x-field>
|
|
166
|
-
</x-field-group>
|
|
167
|
-
|
|
168
|
-
### 返回值
|
|
169
|
-
|
|
170
|
-
返回一个 `boolean` 值:如果视口小于指定的断点,则为 `true`,否则为 `false`。
|
|
171
|
-
|
|
172
|
-
### 示例用法
|
|
173
|
-
|
|
174
|
-
此 hook 必须在由 Material-UI `ThemeProvider` 包裹的组件树中使用。
|
|
175
|
-
|
|
176
|
-
```javascript "ResponsiveComponent.js" icon=logos:javascript
|
|
177
|
-
import React from 'react';
|
|
178
|
-
import Typography from '@mui/material/Typography';
|
|
179
|
-
import { ThemeProvider, createTheme } from '@mui/material/styles';
|
|
180
|
-
import { useMobile } from '@blocklet/ui-react/hooks';
|
|
181
|
-
|
|
182
|
-
// 一个使用该 hook 的组件
|
|
183
|
-
function ResponsiveComponent() {
|
|
184
|
-
const isMobile = useMobile({ key: 'md' }); // 根据 'md' 断点进行检查
|
|
185
|
-
|
|
186
|
-
return (
|
|
187
|
-
<div>
|
|
188
|
-
{isMobile ? (
|
|
189
|
-
<Typography variant="h6">Mobile View</Typography>
|
|
190
|
-
) : (
|
|
191
|
-
<Typography variant="h4">Desktop View</Typography>
|
|
192
|
-
)}
|
|
193
|
-
<p>Resize your browser window to see the content change.</p>
|
|
194
|
-
</div>
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// 该组件必须被包裹在 ThemeProvider 中
|
|
199
|
-
const theme = createTheme();
|
|
200
|
-
|
|
201
|
-
function App() {
|
|
202
|
-
return (
|
|
203
|
-
<ThemeProvider theme={theme}>
|
|
204
|
-
<ResponsiveComponent />
|
|
205
|
-
</ThemeProvider>
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export default App;
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
---
|
|
213
|
-
|
|
214
|
-
通过使用这些 hooks,您可以用最少的样板代码高效地实现复杂功能。有关可能使用这些 hooks 的组件的更多信息,请参阅 [组件](./components.md) 文档。
|