-
Notifications
You must be signed in to change notification settings - Fork 2
Add useResourceModal hook
#1141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pfferrari
wants to merge
7
commits into
main
Choose a base branch
from
feat/430-resource-modal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
05e58f5
feat: add code icon
pfferrari 5477d2f
feat: improve callback state management of VisibilityTrigger
pfferrari bb0dff4
chore: improve VisibilityTrigger tests
pfferrari ff3750a
feat: add useResourceModal
pfferrari 55ba659
chore: enforce resource modal implementation
pfferrari 6c07f6e
fix: manage event stores error
pfferrari e4c6561
chore: set the modal to be dismissible
pfferrari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
packages/app-elements/src/ui/resources/useResourceModal/ResourceAttributes.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import type { Order } from "@commercelayer/sdk" | ||
| import capitalize from "lodash-es/capitalize" | ||
| import lowerCase from "lodash-es/lowerCase" | ||
| import { type FC, useState } from "react" | ||
| import type { JsonObject } from "type-fest" | ||
| import { useCoreApi } from "#providers/CoreSdkProvider" | ||
| import { Button } from "#ui/atoms/Button" | ||
| import { Card } from "#ui/atoms/Card" | ||
| import { CodeBlock } from "#ui/atoms/CodeBlock" | ||
| import { Icon } from "#ui/atoms/Icon" | ||
| import { | ||
| SkeletonTemplate, | ||
| withSkeletonTemplate, | ||
| } from "#ui/atoms/SkeletonTemplate" | ||
| import { Spacer } from "#ui/atoms/Spacer" | ||
| import { Text } from "#ui/atoms/Text" | ||
| import { ListItem } from "#ui/composite/ListItem" | ||
| import type { UseResourceModalConfig } from "./useResourceModal" | ||
|
|
||
| type Props = Pick<UseResourceModalConfig, "resourceType" | "resourceId"> | ||
|
|
||
| export const ResourceAttributes: FC<Props> = ({ resourceType, resourceId }) => { | ||
| const { | ||
| data: resourceData, | ||
| isLoading, | ||
| error, | ||
| } = useCoreApi( | ||
| resourceType, | ||
| "retrieve", | ||
| resourceId != null ? [resourceId] : null, | ||
| { | ||
| fallbackData: mockedResource, | ||
| }, | ||
|
pfferrari marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| const { id, type, ...rest } = resourceData ?? {} | ||
| const attributes = Object.keys(rest) | ||
|
|
||
| return error ? ( | ||
| <Spacer top="4"> | ||
| <Text variant="info" align="center"> | ||
| Resource not found. | ||
| </Text> | ||
| </Spacer> | ||
| ) : ( | ||
| <SkeletonTemplate isLoading={isLoading || resourceData == null}> | ||
| <Spacer bottom="6" top="4"> | ||
| <Card gap="none" className="rounded!"> | ||
| <ListItem borderStyle="solid" className="flex justify-between px-4"> | ||
| <Text variant="info">ID</Text> | ||
| <Text weight="medium" className="font-mono"> | ||
| {id} | ||
| </Text> | ||
| </ListItem> | ||
| <ListItem borderStyle="none" className="flex justify-between px-4"> | ||
| <Text variant="info">Type</Text> | ||
| <Text weight="medium" className="font-mono"> | ||
| {type} | ||
| </Text> | ||
| </ListItem> | ||
| </Card> | ||
| </Spacer> | ||
| <div className="pb-6"> | ||
| <Card gap="none" className="rounded!"> | ||
| {attributes.map((attribute, idx) => { | ||
| return ( | ||
| <ResourceAttributeItem | ||
| key={["attributes", resourceData?.id, attribute].join("-")} | ||
| attribute={attribute} | ||
| value={resourceData?.[attribute as keyof typeof resourceData]} | ||
| hasBorderBottom={idx !== attributes.length - 1} | ||
| /> | ||
| ) | ||
| })} | ||
| </Card> | ||
| </div> | ||
| </SkeletonTemplate> | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Mocked resource used as fallback data while the actual resource is being fetched. | ||
| */ | ||
| const mockedResource = { | ||
| id: "fake-resource-id", | ||
| type: "orders", | ||
| created_at: "2025-01-01T00:00:00.000Z", | ||
| updated_at: "2025-01-01T00:00:00.000Z", | ||
| reference: "FAKE-REF-1234", | ||
| metadata: { | ||
| source: "mock", | ||
| }, | ||
| } as unknown as Order | ||
|
|
||
| const ResourceAttributeItem = withSkeletonTemplate<{ | ||
| attribute: unknown | ||
| value: unknown | ||
| hasBorderBottom: boolean | ||
| }>(({ attribute, value, hasBorderBottom }) => { | ||
| /** | ||
| * State to manage the content and the visibility of the code block used to expand | ||
| * the attribute row when its value is an object. | ||
| * This state is toggled when the Json button is clicked. | ||
| */ | ||
| const [codeBlockContent, setCodeBlockContent] = useState< | ||
| string | JsonObject | null | ||
| >(null) | ||
|
|
||
| /** | ||
| * Component to render the value of an attribute. | ||
| * If the value is an object, a button is rendered to toggle the visibility of a | ||
| * code block that shows the JSON representation of the value. | ||
| * If the value is a primitive, it is rendered as text. | ||
| * If the value is null or an empty string, a dash is rendered. | ||
| * The code block content and visibility are managed by the `codeBlockContent` state. | ||
| */ | ||
| const AttributeValue = withSkeletonTemplate<{ | ||
| value: unknown | ||
| }>(({ value }) => { | ||
| if (value == null || value === "") { | ||
| return <Text variant="disabled">—</Text> | ||
| } | ||
|
|
||
| if (typeof value === "object") { | ||
| return ( | ||
| <Button | ||
| variant="secondary" | ||
| disabled={Object.keys(value as JsonObject).length === 0} | ||
| size="mini" | ||
| onClick={() => { | ||
| if (codeBlockContent != null) { | ||
| setCodeBlockContent(null) | ||
| } else { | ||
| setCodeBlockContent(value as JsonObject) | ||
| } | ||
| }} | ||
| > | ||
| <Icon name="bracketsCurly" size={14} /> | ||
| <Text weight="medium" size="x-small"> | ||
| Json | ||
| </Text> | ||
| </Button> | ||
| ) | ||
| } else { | ||
| return ( | ||
| <Text | ||
| weight="medium" | ||
| className="break-all whitespace-pre overflow-x-auto tabular-nums font-mono" | ||
| > | ||
| {value.toString()} | ||
| </Text> | ||
| ) | ||
| } | ||
| }) | ||
|
|
||
| return ( | ||
| <> | ||
| <ListItem | ||
| borderStyle={ | ||
| hasBorderBottom && codeBlockContent == null ? "solid" : "none" | ||
| } | ||
| className="flex justify-between px-4 h-13.5" | ||
|
pfferrari marked this conversation as resolved.
|
||
| > | ||
| <Text variant="info"> | ||
| {capitalize(lowerCase(attribute?.toString() ?? ""))} | ||
| </Text> | ||
| <AttributeValue value={value} /> | ||
| </ListItem> | ||
| {codeBlockContent != null && ( | ||
| <ListItem | ||
| borderStyle={hasBorderBottom ? "solid" : "none"} | ||
| className="px-4 pt-0" | ||
| > | ||
| <CodeBlock>{codeBlockContent}</CodeBlock> | ||
| </ListItem> | ||
| )} | ||
| </> | ||
| ) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.