@availity/mui-spaces 0.2.6 → 0.3.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.
@@ -0,0 +1,239 @@
1
+ import dayjs from 'dayjs';
2
+ import { Card, CardContent, CardHeader } from '@availity/mui-card';
3
+ import { Typography } from '@availity/mui-typography';
4
+ import { NavigateTopIcon, FileIcon } from '@availity/mui-icon';
5
+ import { useMemo, cloneElement } from 'react';
6
+ import { StatusChip, Chip } from '@availity/mui-chip';
7
+ import { CircularProgress } from '@availity/mui-progress';
8
+ import { Link } from '@availity/mui-link';
9
+ import { FavoriteHeart } from '@availity/mui-favorites';
10
+ import { Grid, Box } from '@availity/mui-layout';
11
+ import { ListItem, ListItemText } from '@availity/mui-list';
12
+ import ReactMarkdown from 'react-markdown';
13
+ import { useSpacesContext } from '../Spaces';
14
+ import { useLink } from './useLink';
15
+ import type { SpacesLinkWithSpace, SpacesLinkWithSpaceId, SpacesLinkVariants } from './spaces-link-types';
16
+ import { isFunction } from '../helpers';
17
+
18
+ const getDisplayDate = (date: string | null | undefined) => dayjs(date).format('MM/DD/YYYY');
19
+
20
+ const getContainerTag = (propTag: string | undefined, variant: SpacesLinkVariants) => {
21
+ if (variant && variant !== 'default') return { card: Card, list: ListItem }[variant];
22
+ return propTag || 'div';
23
+ };
24
+
25
+ const getBodyTag = (propTag: string | undefined, variant: SpacesLinkVariants) => {
26
+ if (variant && variant !== 'default') return { card: CardContent, list: 'div' }[variant];
27
+ return propTag || 'div';
28
+ };
29
+
30
+ const getTitleTag = (propTag: string | undefined, variant: SpacesLinkVariants) => {
31
+ if (variant && variant !== 'default') return { card: CardHeader, list: Typography }[variant];
32
+ return propTag || 'div';
33
+ };
34
+
35
+ const getTextTag = (propTag: string | undefined, variant: SpacesLinkVariants) => {
36
+ if (variant && variant !== 'default') return { card: Typography, list: ListItemText }[variant];
37
+ return propTag || 'div';
38
+ };
39
+
40
+ export const SpacesLink = ({
41
+ spaceId,
42
+ space: propSpace,
43
+ className,
44
+ children,
45
+ favorite,
46
+ icon,
47
+ showName = true,
48
+ showNew,
49
+ showDate,
50
+ stacked,
51
+ body = true,
52
+ description: showDescription,
53
+ tag,
54
+ bodyTag,
55
+ titleTag,
56
+ textTag,
57
+ titleClassName,
58
+ variant = 'default',
59
+ loading: propsLoading,
60
+ clientId: propsClientId,
61
+ maxDescriptionWidth,
62
+ style,
63
+ linkAttributes,
64
+ role,
65
+ analytics,
66
+ customBadgeText,
67
+ customBadgeColor,
68
+ idPrefix = '',
69
+ ...rest
70
+ }: SpacesLinkWithSpace | SpacesLinkWithSpaceId) => {
71
+ const { loading } = useSpacesContext();
72
+ const isLoading = loading || propsLoading;
73
+
74
+ const [linkSpace, props] = useLink(propSpace || spaceId, { clientId: propsClientId, linkAttributes });
75
+
76
+ const showUrl = !linkSpace?.isGhosted && linkSpace?.link?.url;
77
+
78
+ const favoriteIcon = useMemo(
79
+ () =>
80
+ linkSpace?.configurationId &&
81
+ favorite && (
82
+ <FavoriteHeart
83
+ id={`${idPrefix}${linkSpace?.configurationId}`}
84
+ name={linkSpace?.name}
85
+ onChange={(_, e) => e.stopPropagation()}
86
+ />
87
+ ),
88
+ [favorite, linkSpace?.configurationId, linkSpace?.name, idPrefix]
89
+ );
90
+
91
+ const dateInfo = useMemo(
92
+ () =>
93
+ (showNew || showDate) && (
94
+ <Grid textAlign={stacked ? 'center' : 'inherit'}>
95
+ {showNew && linkSpace?.isNew && (
96
+ <Chip id={`${idPrefix}app-new-badge-${linkSpace?.configurationId}`} label="New!" />
97
+ )}
98
+ {showDate && (
99
+ <Typography
100
+ id={`${idPrefix}app-date-badge-${linkSpace?.configurationId}`}
101
+ variant="caption"
102
+ color="textSecondary"
103
+ >
104
+ {getDisplayDate(linkSpace?.activeDate)}
105
+ </Typography>
106
+ )}
107
+ </Grid>
108
+ ),
109
+ [linkSpace?.activeDate, linkSpace?.isNew, showDate, showNew, stacked, linkSpace?.configurationId, idPrefix]
110
+ );
111
+
112
+ const customBadgeDisplay = useMemo(
113
+ () =>
114
+ customBadgeText && (
115
+ <Box
116
+ textAlign={stacked ? 'center' : 'inherit'}
117
+ marginRight={variant !== 'card' && (showDate || (showNew && linkSpace?.isNew)) ? 2 : undefined}
118
+ >
119
+ <StatusChip
120
+ color={customBadgeColor || 'info'}
121
+ id={`${idPrefix}app-custom-badge-${linkSpace?.configurationId}-${customBadgeText}`}
122
+ label={customBadgeText}
123
+ />
124
+ </Box>
125
+ ),
126
+ [
127
+ customBadgeColor,
128
+ customBadgeText,
129
+ showDate,
130
+ showNew,
131
+ stacked,
132
+ variant,
133
+ linkSpace?.isNew,
134
+ idPrefix,
135
+ linkSpace?.configurationId,
136
+ ]
137
+ );
138
+
139
+ if (isLoading) {
140
+ return <CircularProgress id={`${idPrefix}app-${linkSpace?.configurationId}-loading`} {...rest} />;
141
+ }
142
+
143
+ const Tag = getContainerTag(tag, variant);
144
+ const BodyTag = getBodyTag(bodyTag, variant);
145
+ const TitleTag = getTitleTag(titleTag, variant);
146
+ const TextTag = getTextTag(textTag, variant);
147
+
148
+ const renderChildren = () => {
149
+ if (children) {
150
+ return isFunction(children)
151
+ ? (() =>
152
+ children({
153
+ ...linkSpace,
154
+ ...analytics,
155
+ ...props,
156
+ }))()
157
+ : cloneElement(children, {
158
+ role: 'link',
159
+ tabIndex: 0,
160
+ style: { cursor: showUrl ? 'pointer' : 'not-allowed' },
161
+ 'aria-label': linkSpace?.name,
162
+ ...analytics,
163
+ ...props,
164
+ });
165
+ }
166
+ };
167
+ return (
168
+ <Tag
169
+ title={linkSpace?.name}
170
+ className={className}
171
+ {...rest}
172
+ style={{ ...style }}
173
+ role={variant === 'list' ? 'listitem' : role}
174
+ >
175
+ <BodyTag>
176
+ <Grid alignItems={!showDescription || stacked ? 'center' : 'start'} direction={stacked ? 'column' : 'row'}>
177
+ {!stacked && favoriteIcon}
178
+ {icon && linkSpace?.url && linkSpace?.type?.toUpperCase() === 'FILE' ? (
179
+ <Link target="_blank" href={linkSpace?.url}>
180
+ <FileIcon data-testid="icon" />
181
+ </Link>
182
+ ) : (
183
+ <NavigateTopIcon data-testid="icon" />
184
+ )}
185
+ {children
186
+ ? renderChildren()
187
+ : body && (
188
+ <Grid id={`${idPrefix}${linkSpace?.type}-${linkSpace?.configurationId}`}>
189
+ <Box
190
+ marginBottom={!customBadgeDisplay && (!showDescription || !linkSpace?.description) ? 0 : undefined}
191
+ paddingTop={stacked ? 3 : undefined}
192
+ textAlign={stacked ? 'center' : undefined}
193
+ >
194
+ <TitleTag
195
+ id={`${idPrefix}app-title-${linkSpace?.configurationId}`}
196
+ className={titleClassName}
197
+ tabIndex={0}
198
+ style={{
199
+ cursor: showUrl ? 'pointer' : 'not-allowed',
200
+ }}
201
+ {...analytics}
202
+ {...props}
203
+ role={showUrl ? 'link' : role}
204
+ aria-label={linkSpace?.name}
205
+ aria-describedby={
206
+ showNew && linkSpace?.isNew
207
+ ? `${idPrefix}app-new-badge-${linkSpace?.configurationId}`
208
+ : undefined
209
+ }
210
+ variant={variant === 'list' ? 'h5' : 'h6'}
211
+ title={showName ? linkSpace?.name : undefined}
212
+ >
213
+ {showName ? linkSpace?.name : undefined}
214
+ </TitleTag>
215
+ </Box>
216
+ {stacked && dateInfo}
217
+ {showDescription && linkSpace?.description && (
218
+ <TextTag
219
+ marginTop={1}
220
+ textAlign={stacked ? 'center' : undefined}
221
+ overflow="hidden"
222
+ whiteSpace={maxDescriptionWidth ? 'nowrap' : undefined}
223
+ width={maxDescriptionWidth}
224
+ textOverflow="ellipsis"
225
+ id={`${idPrefix}app-description-${linkSpace?.configurationId}`}
226
+ >
227
+ <ReactMarkdown className="Card-text">{linkSpace?.description}</ReactMarkdown>
228
+ </TextTag>
229
+ )}
230
+ {variant === 'card' && customBadgeDisplay}
231
+ </Grid>
232
+ )}
233
+ {variant !== 'card' && customBadgeDisplay}
234
+ {!stacked && dateInfo}
235
+ </Grid>
236
+ </BodyTag>
237
+ </Tag>
238
+ );
239
+ };
@@ -0,0 +1,45 @@
1
+ import { getUrl, getTarget, updateUrl } from '../helpers';
2
+ import nativeForm from '@availity/native-form';
3
+ import { isAbsoluteUrl } from '@availity/resolve-url';
4
+ import { updateTopApps } from '../topApps';
5
+ import { OpenLink, OpenLinkWithSso } from './spaces-link-types';
6
+
7
+ export const openLink: OpenLink = async (space, params) => {
8
+ if (!space?.link?.url) {
9
+ return;
10
+ }
11
+
12
+ if (params?.akaname) await updateTopApps(space, params.akaname);
13
+
14
+ const url = !isAbsoluteUrl(space.link.url)
15
+ ? getUrl(updateUrl(space.link.url, 'spaceId', params?.payerSpaceId || space.parents?.[0]?.id), false, false)
16
+ : space.link.url;
17
+
18
+ const target = getTarget(space.link.target);
19
+
20
+ window.open(url, target);
21
+ };
22
+
23
+ export const openLinkWithSso: OpenLinkWithSso = async (space, { akaname, clientId, payerSpaceId, ssoParams }) => {
24
+ if (space.meta && space.meta.ssoId) {
25
+ const options = space.link?.target ? { target: getTarget(space.link.target) } : undefined;
26
+
27
+ const attributes = {
28
+ X_Client_ID: clientId,
29
+ X_XSRF_TOKEN: document.cookie.replace(/(?:(?:^|.*;\s*)XSRF-TOKEN\s*=\s*([^;]*).*$)|^.*$/, '$1'),
30
+ spaceId: payerSpaceId,
31
+ ...ssoParams,
32
+ };
33
+
34
+ try {
35
+ if (akaname) await updateTopApps(space, akaname);
36
+ await nativeForm(space.meta.ssoId, attributes, options, space.type);
37
+ } catch {
38
+ console.warn('Something went wrong');
39
+ }
40
+
41
+ return false;
42
+ }
43
+
44
+ return false;
45
+ };
@@ -0,0 +1,143 @@
1
+ import type { StatusChipProps } from '@availity/mui-chip';
2
+ import type { Space } from '../spaces-types';
3
+
4
+ export type SpacesLinkVariants = 'card' | 'list' | 'default' | undefined;
5
+
6
+ export type SpacesLinkWithSpace = {
7
+ /** If no spaceId is provided, the first space in the spaces array is used.
8
+ * Note: This is only to be used when the Spaces provider should only ever contain a single space.
9
+ */
10
+ spaceId?: string;
11
+ /** Use to directly pass a space to the component rather than have it fetched from the spaces API.
12
+ * This component does not have to be a child of SpacesProvider.
13
+ * Note: If you are wanting to take advantage of the sso links you will additionally need to pass the clientId in.
14
+ */
15
+ space: Space | SsoTypeSpace;
16
+ } & SpacesLinkProps;
17
+
18
+ export type SpacesLinkWithSpaceId = {
19
+ /** If no spaceId is provided, the first space in the spaces array is used.
20
+ * Note: This is only to be used when the Spaces provider should only ever contain a single space.
21
+ */
22
+ spaceId: string;
23
+ /** Use to directly pass a space to the component rather than have it fetched from the spaces API.
24
+ * This component does not have to be a child of SpacesProvider.
25
+ * Note: If you are wanting to take advantage of the sso links you will additionally need to pass the clientId in.
26
+ */
27
+ space?: Space | SsoTypeSpace;
28
+ } & SpacesLinkProps;
29
+
30
+ export type SpacesLinkProps = {
31
+ /** Children can be a react child or render prop. */
32
+ children?: JSX.Element | ((props: any | undefined) => JSX.Element);
33
+ /** Tag to overwrite the root component rendered. */
34
+ tag?: string;
35
+ /** Tag to overwrite the body component that renders the title, description and data values.
36
+ * It defaults to CardBody or div depending on the value of the variant prop.
37
+ */
38
+ bodyTag?: string;
39
+ /** Tag to overwrite the title component. If variant prop is set to "card", defaults to CardTitle.
40
+ * If variant is set to "list", defaults to ListItemHeading. Overwise, defaults to div.
41
+ */
42
+ titleTag?: string;
43
+ /** Tag to overwrite the text component. If variant prop is set to "card", defaults to Card Text.
44
+ * If variant is set to "list", defaults to ListItemText. Otherwise, defaults to div.
45
+ */
46
+ textTag?: string;
47
+ titleClassName?: string;
48
+ /** When true, utilizes the Card component for styling. */
49
+ card?: boolean;
50
+ /** When true, renders an @availity/mui-icon next to the title if present on the Space. */
51
+ icon?: boolean;
52
+ /** When true, renders the Spaces description beneath the title. */
53
+ description?: boolean;
54
+ /** When passed in, provides predefined styles for the component.
55
+ * @default 'default'
56
+ */
57
+ variant?: SpacesLinkVariants;
58
+ /** When true, renders the FavoriteHeart component to the left of the Component.
59
+ * Note, this does require you to have wrapped your component somewhere in the Favorites Provider.
60
+ * This also requires the peerDependency @tanstack/react-query.
61
+ */
62
+ favorite?: boolean;
63
+ /** When true, renders the tyitle, and allow for the description and date info to be added on.
64
+ * @default true
65
+ */
66
+ body?: boolean;
67
+ /** When true, renders the activeDate of the space. */
68
+ showDate?: boolean;
69
+ /** When true, renders the name of the space.
70
+ * @default true
71
+ */
72
+ showName?: boolean;
73
+ /** When true, renders a "New!" badge if the activeDate is less than 30 days old. */
74
+ showNew?: boolean;
75
+ /** When true, renders the component vertically. */
76
+ stacked?: boolean;
77
+ /** Optionally pass in your own landing state for the component if you are managing the state yourself. */
78
+ loading?: boolean;
79
+ /** Required when space is not provided, or space is provided and space contains an sso link. */
80
+ clientId?: string;
81
+ style?: object;
82
+ className?: string;
83
+ /** Allows the description length to be truncated. */
84
+ maxDescriptionWidth?: string;
85
+ /** Additional attributes you may want to tack onto the native-form when submitting a SAML sso.
86
+ * i.e. spaceId or sourceApplicationId
87
+ */
88
+ linkAttributes?: object;
89
+ /** Allows the role of the root component to be overwritten.
90
+ * If variant prop is set to "list", defaults to "listitem".
91
+ */
92
+ role?: string;
93
+ /** When Analytics props are passed inside the analytics props, they will be passed down to the click item.
94
+ * For more information on Analytics props see: Autotrack Logged Events
95
+ */
96
+ analytics?: object;
97
+ customBadgeText?: string;
98
+ customBadgeColor?: StatusChipProps['color'];
99
+ /** prefix for ids to prevent duplicates when the same config link is displayed on the page more than once
100
+ * @default ''
101
+ */
102
+ idPrefix?: string;
103
+ [key: string]: any;
104
+ };
105
+
106
+ export type MediaProps = {
107
+ role: string;
108
+ onClick?: (event: React.MouseEvent) => void;
109
+ onKeyDown?: (event: React.KeyboardEvent) => void;
110
+ };
111
+
112
+ export interface SsoTypeSpace extends Space {
113
+ type: 'saml' | 'openid';
114
+ }
115
+
116
+ export type UseLink = {
117
+ (
118
+ spaceId?: Space | SsoTypeSpace | string,
119
+ options?: { clientId?: string; linkAttributes?: Record<string, any> }
120
+ ): [Space | undefined, MediaProps | undefined];
121
+ };
122
+
123
+ export type OpenLink = {
124
+ (
125
+ space?: Space,
126
+ params?: {
127
+ akaname?: string;
128
+ payerSpaceId?: string;
129
+ }
130
+ ): Promise<void>;
131
+ };
132
+
133
+ export type OpenLinkWithSso = {
134
+ (
135
+ space: SsoTypeSpace,
136
+ params: {
137
+ akaname?: string;
138
+ clientId: string;
139
+ payerSpaceId: string;
140
+ ssoParams: Record<string, string>;
141
+ }
142
+ ): Promise<boolean>;
143
+ };