@airtable/blocks/ui#useRecordById JavaScript Examples
The following examples show how to use
@airtable/blocks/ui#useRecordById.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.js From apps-url-preview with MIT License | 4 votes |
// Shows a preview, or a message about what the user should do to see a preview.
function RecordPreview({
activeTable,
selectedRecordId,
selectedFieldId,
setIsDialogOpen,
setIsSettingsOpen,
}) {
const {
settings: {isEnforced, urlField, urlTable},
} = useSettings();
const table = (isEnforced && urlTable) || activeTable;
// We use getFieldByIdIfExists because the field might be deleted.
const selectedField = selectedFieldId ? table.getFieldByIdIfExists(selectedFieldId) : null;
// When using a specific field for previews is enabled and that field exists,
// use the selectedField
const previewField = (isEnforced && urlField) || selectedField;
// Triggers a re-render if the record changes. Preview URL cell value
// might have changed, or record might have been deleted.
const selectedRecord = useRecordById(table, selectedRecordId ? selectedRecordId : '', {
fields: [previewField],
});
// Triggers a re-render if the user switches table or view.
// RecordPreview may now need to render a preview, or render nothing at all.
useWatchable(cursor, ['activeTableId', 'activeViewId']);
// This button is re-used in two states so it's pulled out in a constant here.
const viewSupportedURLsButton = (
<TextButton size="small" marginTop={3} onClick={() => setIsDialogOpen(true)}>
View supported URLs
</TextButton>
);
if (
// If there is/was a specified table enforced, but the cursor
// is not presently in the specified table, display a message to the user.
// Exception: selected record is from the specified table (has been opened
// via button field or other means while cursor is on a different table.)
isEnforced &&
cursor.activeTableId !== table.id &&
!(selectedRecord && selectedRecord.parentTable.id === table.id)
) {
return (
<Fragment>
<Text paddingX={3}>Switch to the “{table.name}” table to see previews.</Text>
<TextButton size="small" marginTop={3} onClick={() => setIsSettingsOpen(true)}>
Settings
</TextButton>
</Fragment>
);
} else if (
// activeViewId is briefly null when switching views
selectedRecord === null &&
(cursor.activeViewId === null ||
table.getViewById(cursor.activeViewId).type !== ViewType.GRID)
) {
return <Text>Switch to a grid view to see previews</Text>;
} else if (
// selectedRecord will be null on app initialization, after
// the user switches table or view, or if it was deleted.
selectedRecord === null ||
// The preview field may have been deleted.
previewField === null
) {
return (
<Fragment>
<Text>Select a cell to see a preview</Text>
{viewSupportedURLsButton}
</Fragment>
);
} else {
// Using getCellValueAsString guarantees we get a string back. If
// we use getCellValue, we might get back numbers, booleans, or
// arrays depending on the field type.
const cellValue = selectedRecord.getCellValueAsString(previewField);
if (!cellValue) {
return (
<Fragment>
<Text>The “{previewField.name}” field is empty</Text>
{viewSupportedURLsButton}
</Fragment>
);
} else {
const previewUrl = getPreviewUrlForCellValue(cellValue);
// In this case, the FIELD_NAME field of the currently selected
// record either contains no URL, or contains a that cannot be
// resolved to a supported preview.
if (!previewUrl) {
return (
<Fragment>
<Text>No preview</Text>
{viewSupportedURLsButton}
</Fragment>
);
} else {
return (
<iframe
// Using `key=previewUrl` will immediately unmount the
// old iframe when we're switching to a new
// preview. Otherwise, the old iframe would be reused,
// and the old preview would stay onscreen while the new
// one was loading, which would be a confusing user
// experience.
key={previewUrl}
style={{flex: 'auto', width: '100%'}}
src={previewUrl}
frameBorder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
/>
);
}
}
}
}